mysql数据读写分离:
把客户端访问数据库服务时的查询请求和写数据的请求给不同的数据库服务器处理。
实现的效果:
在代理服务器上使用中间件maxscale,实现读写分离的相关的配置,从而使得客户端连接代理服务器的时候,在
实现查询的时候都在从库中查询,在写入数据的时候在主库中写入。
client43
mysql -h192.168.4.100 -u -p
MySQL>select ------->10
mysql>insert/delete/update ---->20
100 ----单点故障
代理服务器(服务) ----->配置i文件
读 写
select insert/delete/update
slave master
mysql42 mysql41
部署MySQL数据读写分离架构
主机41主库
从库42从库
代理服务器46
1 配置MySQL主从同步
要求把42配置为41的从库
配置主库41
启用binlog日志
用户授权
查看日志信息
配置从库42
验证授权
指定server_id
指定主库信息
查看从库状态
客户端验证主从同步配置
在主库添加访问数据的连接用户并设置密码
create database db12;
grant all on db12.* to yaya@"%" identified by "123456";
客户端连接主机 执行sql命令
在从库主机上可以看到同样的数据
mysql中间件:mysql-proxy mycat maxscale
在主机100上部署代理服务,实现数据读写分离
装包
[root@host46 ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm
修改配置文件,并根据配置文件的设置在数据库服务器上添加对应的授权用户
在主库上进行
grant replication slave,replication client on *.* to scalemon@"%" identified by "123456";
39 user=scalemon
40 passwd=123456
grant select on mysql.* to maxscale@"%" identified by "123456";
67 user=maxscale
68 passwd=123456
[root@host41 ~]# mysql -uroot -p123456
mysql> grant replication slave,replication client on *.* to scalemon@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> grant select on mysql.* to maxscale@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.04 sec)
在从库上进行
select user from mysql.user where user in ("scalemon","maxscale");
[root@host42 ~]# mysql -u root -p123456
mysql> select user from mysql.user where user in ("scalemon","maxscale");
+----------+
| user |
+----------+
| maxscale |
| scalemon |
+----------+
2 rows in set (0.00 sec)
在代理服务器上进行
[root@host46 ~]# cp /etc/maxscale.cnf /etc/maxscale.cnf.bak
[root@host46 ~]# vim /etc/maxscale.cnf
9 [maxscale]
10 threads=auto
18 [server1] ----指定数据库的服务器
19 type=server
20 address=192.168.4.41
21 port=3306
22 protocol=MySQLBackend
23
24 [server2]
25 type=server
26 address=192.168.4.42
27 port=3306
28 protocol=MySQLBackend
35 [MySQL Monitor] -----监控谁是主库谁是从库
36 type=monitor
37 module=mysqlmon
38 servers=server1,server2
39 user=scalemon
40 passwd=123456
41 monitor_interval=10000
63 [Read-Write Service] -----读写分离在哪进行
64 type=service
65 router=readwritesplit ---脚本去进行读写分离
66 servers=server1,server2
67 user=maxscale ----取验证是否在主从数据库中存在
68 passwd=123456
69 max_slave_connections=100%
75 [MaxAdmin Service] ----定义管理服务
76 type=service
77 router=cli
91 [Read-Write Listener] ----提供读写分离的端口
92 type=listener
93 service=Read-Write Service
94 protocol=MySQLClient
95 port=4006
97 [MaxAdmin Listener] -----主机的管理服务需要访问的端口
98 type=listener
99 service=MaxAdmin Service
100 protocol=maxscaled
101 socket=default
102 port=4099
测试连接
[root@host46 ~]# mysql -uscalemon -p123456 -h192.168.4.41
[root@host46 ~]# mysql -uscalemon -p123456 -h192.168.4.42
[root@host46 ~]# mysql -umaxscale -p123456 -h192.168.4.42
[root@host46 ~]# mysql -umaxscale -p123456 -h192.168.4.41
启动服务
[root@host46 ~]# max
maxadmin maxbinlogcheck maxpasswd
maxavrocheck maxkeys maxscale
[root@host46 ~]# maxscale -f /etc/maxscale.cnf 启动服务命令
查看服务
[root@host46 ~]# ps -C maxscale ----查看进程
PID TTY TIME CMD
2891 ? 00:00:00 maxscale
[root@host46 ~]# ss -utnlp | grep maxscale --查看端口
tcp LISTEN 0 128 :::4099 :::* users:(("maxscale",pid=2891,fd=12))
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=2891,fd=11))
测试配置
在主机46上连接管理服务查看监控信息
[root@host46 ~]# maxadmin -uadmin -pmariadb -P4099
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.4.41 | 3306 | 0 | Master, Running
server2 | 192.168.4.42 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+-----------------
MaxScale> exit
在客户端连接46主机,访问数据时能否实现数据读写分离
实现查询的时候都在从库中查询,在写入数据的时候在主库中写入
[root@host41 ~]# mysql -uroot -p123456
mysql> select * from zhy;
+------+------+
| name | age |
+------+------+
| zhu | 12 |
+------+------+
1 row in set (0.00 sec)
[root@host42 ~]# mysql -u root -p123456
mysql> select * from zhy;
+------+------+
| name | age |
+------+------+
| zhu | 12 |
| hai | 22 |
+------+------+
2 rows in set (0.00 sec)
[root@room9pc01 ~]# mysql -h192.168.4.46 -P4006 -uyaya -p123456
MySQL [db12]> insert into zhy values("yan",12);
Query OK, 1 row affected (0.10 sec)
MySQL [db12]> select * from zhy;
+------+------+
| name | age |
+------+------+
| zhu | 12 |
| hai | 22 |
| yan | 12 |
+------+------+
把客户端访问数据库服务时的查询请求和写数据的请求给不同的数据库服务器处理。
实现的效果:
在代理服务器上使用中间件maxscale,实现读写分离的相关的配置,从而使得客户端连接代理服务器的时候,在
实现查询的时候都在从库中查询,在写入数据的时候在主库中写入。
client43
mysql -h192.168.4.100 -u -p
MySQL>select ------->10
mysql>insert/delete/update ---->20
100 ----单点故障
代理服务器(服务) ----->配置i文件
读 写
select insert/delete/update
slave master
mysql42 mysql41
部署MySQL数据读写分离架构
主机41主库
从库42从库
代理服务器46
要求把42配置为41的从库
配置主库41
启用binlog日志
用户授权
查看日志信息
配置从库42
验证授权
指定server_id
指定主库信息
查看从库状态
客户端验证主从同步配置
在主库添加访问数据的连接用户并设置密码
create database db12;
grant all on db12.* to yaya@"%" identified by "123456";
客户端连接主机 执行sql命令
在从库主机上可以看到同样的数据
mysql中间件:mysql-proxy mycat maxscale
在主机100上部署代理服务,实现数据读写分离
装包
[root@host46 ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm
修改配置文件,并根据配置文件的设置在数据库服务器上添加对应的授权用户
在主库上进行
grant replication slave,replication client on *.* to scalemon@"%" identified by "123456";
39 user=scalemon
40 passwd=123456
grant select on mysql.* to maxscale@"%" identified by "123456";
67 user=maxscale
68 passwd=123456
[root@host41 ~]# mysql -uroot -p123456
mysql> grant replication slave,replication client on *.* to scalemon@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.04 sec)
mysql> grant select on mysql.* to maxscale@"%" identified by "123456";
Query OK, 0 rows affected, 1 warning (0.04 sec)
在从库上进行
select user from mysql.user where user in ("scalemon","maxscale");
[root@host42 ~]# mysql -u root -p123456
mysql> select user from mysql.user where user in ("scalemon","maxscale");
+----------+
| user |
+----------+
| maxscale |
| scalemon |
+----------+
2 rows in set (0.00 sec)
在代理服务器上进行
[root@host46 ~]# cp /etc/maxscale.cnf /etc/maxscale.cnf.bak
[root@host46 ~]# vim /etc/maxscale.cnf
9 [maxscale]
10 threads=auto
18 [server1] ----指定数据库的服务器
19 type=server
20 address=192.168.4.41
21 port=3306
22 protocol=MySQLBackend
23
24 [server2]
25 type=server
26 address=192.168.4.42
27 port=3306
28 protocol=MySQLBackend
35 [MySQL Monitor] -----监控谁是主库谁是从库
36 type=monitor
37 module=mysqlmon
38 servers=server1,server2
39 user=scalemon
40 passwd=123456
41 monitor_interval=10000
63 [Read-Write Service] -----读写分离在哪进行
64 type=service
65 router=readwritesplit ---脚本去进行读写分离
66 servers=server1,server2
67 user=maxscale ----取验证是否在主从数据库中存在
68 passwd=123456
69 max_slave_connections=100%
75 [MaxAdmin Service] ----定义管理服务
76 type=service
77 router=cli
91 [Read-Write Listener] ----提供读写分离的端口
92 type=listener
93 service=Read-Write Service
94 protocol=MySQLClient
95 port=4006
97 [MaxAdmin Listener] -----主机的管理服务需要访问的端口
98 type=listener
99 service=MaxAdmin Service
100 protocol=maxscaled
101 socket=default
102 port=4099
测试连接
[root@host46 ~]# mysql -uscalemon -p123456 -h192.168.4.41
[root@host46 ~]# mysql -uscalemon -p123456 -h192.168.4.42
[root@host46 ~]# mysql -umaxscale -p123456 -h192.168.4.42
[root@host46 ~]# mysql -umaxscale -p123456 -h192.168.4.41
启动服务
[root@host46 ~]# max
maxadmin maxbinlogcheck maxpasswd
maxavrocheck maxkeys maxscale
[root@host46 ~]# maxscale -f /etc/maxscale.cnf 启动服务命令
查看服务
[root@host46 ~]# ps -C maxscale ----查看进程
PID TTY TIME CMD
2891 ? 00:00:00 maxscale
[root@host46 ~]# ss -utnlp | grep maxscale --查看端口
tcp LISTEN 0 128 :::4099 :::* users:(("maxscale",pid=2891,fd=12))
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=2891,fd=11))
测试配置
在主机46上连接管理服务查看监控信息
[root@host46 ~]# maxadmin -uadmin -pmariadb -P4099
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.4.41 | 3306 | 0 | Master, Running
server2 | 192.168.4.42 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+-----------------
MaxScale> exit
在客户端连接46主机,访问数据时能否实现数据读写分离
实现查询的时候都在从库中查询,在写入数据的时候在主库中写入
[root@host41 ~]# mysql -uroot -p123456
mysql> select * from zhy;
+------+------+
| name | age |
+------+------+
| zhu | 12 |
+------+------+
1 row in set (0.00 sec)
[root@host42 ~]# mysql -u root -p123456
mysql> select * from zhy;
+------+------+
| name | age |
+------+------+
| zhu | 12 |
| hai | 22 |
+------+------+
2 rows in set (0.00 sec)
[root@room9pc01 ~]# mysql -h192.168.4.46 -P4006 -uyaya -p123456
MySQL [db12]> insert into zhy values("yan",12);
Query OK, 1 row affected (0.10 sec)
MySQL [db12]> select * from zhy;
+------+------+
| name | age |
+------+------+
| zhu | 12 |
| hai | 22 |
| yan | 12 |
+------+------+