读写分离

mysql读写分离
把客户端访问数据的 查询 和 写入 分别给不同的数据库服务器处理。


client50]# mysql -h57 -uyaya100 -p123qqq...A

mycat57(数据读写分离服务器)
insert写------>mysql51主
select读------>mysql52从
判断客户端是读还是写,然后分配给主或者从服务器
mysql51(insert写) mysql52(select读)
master主 slave从
------------------------------------------------------------------------------------------------------------------------
在主机192.168.4.57运行对数据读写分离服务,把写给51处理,把读访问给52处理。

配置步骤:
1 部署myslq 一主51 一从52 同步结构
51主库部署好后
mysql> show master status;
52从库部署好后
mysql> show slave status\G;

2.1 部署数据读写分离服务器192.168.4.57
mysql中间件: mycat,maxscale,mysql-proxy
----------------------------------------------------------------------------------------------------
2.2 装包
装包 maxscale-2.1.2-1.rhel.7.x86_64.rpm
# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm

# max 按2次tab会显示相关的命令
maxadmin maxavrocheck maxbinlogcheck maxkeys maxpasswd maxscale
# rpm -ql maxscale
--------------------------------------------------------------------------------------------------
2.3 修改配置文件
# cp /etc/maxscale.cnf /root/ 要先备份主配置文件,免得改错了,要养成这个好习惯

# vim /etc/maxscale.cnf
:set nu 显示行号
9 [maxscale] 定义服务运行后线程的数量。不用改这行,原本就有的,一个配置项对应一个[ ]。scale(测量,衡量)
10 threads=auto 改成auto。根据CPU的核数,服务启动后自动创建对应的线程数量。
... ...

18 [server1] 指定数据库服务器ip地址。不用改这行,原本就有的。代表这是一个服务器配置项
19 type=server
20 address=192.168.4.51 master数据库服务器,改成192.168.4.51
21 port=3306
22 protocol=MySQLBackend
... ...

24 [server2] 复制上面的5行,然后改成server2
25 type=server
26 address=192.168.4.52 salve数据库服务器,改成192.168.4.52
27 port=3306
28 protocol=MySQLBackend
... ...

36 [MySQL Monitor] 定义监视的数据库服务器。monitor(监视)
37 type=monitor
38 module=mysqlmon
39 servers=server1, server2 数据库服务器的主机名。要添加, server2(逗号,之后是有空格的!)。代表本机57要监视数据库服务器是server1,server2
40 user=scalemon 用户。改为scalemon,用户名57监视它们51和52的数据库的running运行状态,看主从是否正常,谁是主库,谁是从库。
41 passwd=123qqq...A 密码。改为123qqq...A,57连接51和52数据库时的密码。如果想要写123456,51和52必须设置密码策略检查长度等。
42 monitor_interval=10000
... ...

53 #[Read-Only Service] 可以47,53s/^/#/一次性加上注释!注释掉这一整个配置项!不指定只处理查询的服务器。
54 #type=service 注释掉
55 #router=readconnroute 注释掉
56 #servers=server1 注释掉
57 #user=myuser 注释掉
58 #passwd=mypwd 注释掉
59 #router_options=slave 要一直注释掉这里!
... ...

64 [Read-Write Service] 定义读写分离服务
65 type=service
66 router=readwritesplit 调用的插件功能,安装后自带的。
67 servers=server1, server2 指定数据库服务器。要添加, server2(逗号,之后是有空格的!)。
68 user=pljadmin 用户。改为pljadmin。是57验证客户端访问时访问的数据库用户名和密码是否存在
69 passwd=123qqq...A 密码。改为123qqq...A
70 max_slave_connections=100%
... ...

76 [MaxAdmin Service] 管理服务。不用动这个配置项
77 type=service
78 router=cli
... ...

86 #[Read-Only Listener] 可以86,90s/^/#/一次性加上注释!注释掉这一整个配置项!
87 #type=listener 注释掉
88 #service=Read-Only Service 注释掉
89 #protocol=MySQLClient 注释掉
90 #port=4008 要一直注释掉这里!
... ...

92 [Read-Write Listener] 定义数据读写分离服务使用的端口号。不用改这个配置项
93 type=listener
94 service=Read-Write Service 这里对应的是上面的配置项
95 protocol=MySQLClient
96 port=4006 57主机上的读写分离服务这个程序监听的默认端口号是4006
... ...

98 [MaxAdmin Listener] 定义管理服务使用的端口号。不用改这个配置项,只要在它最后添加一行指定端口号就行
99 type=listener
100 service=MaxAdmin Service
101 protocol=maxscaled
102 socket=default
103 port=4026 添加这行!指定此程序监听的端口号是4026
-------------------------------------------------------------------------------
2.4 根据配置文件的设置添加对应的用户

51 52(只要授权51就行,因为52会自动同步。)

51操作:
mysql> grant replication slave,replication client on *.* 授权给监控用户pljadmin@"%",ip地址一定要写"%"
-> to scalemon@"%" identified by "123qqq...A";

mysql> grant select on mysql.* to pljadmin@"%" identified by "123qqq...A"; 授权给路由用户pljadmin@"%",ip地址一定要写"%"

mysql> select user,host from mysql.user
-> where user in ("scalemon","pljadmin");
+----------+------+
| user | host |
+----------+------+
| pljadmin | % |
| scalemon | % |
+----------+------+
--------------------------------------------------------------------------------------------------------------------------------
57验证
# mysql -h192.168.4.51 -uscalemon -p123qqq...A
# mysql -h192.168.4.51 -upljadmin -p123qqq...A
# mysql -h192.168.4.52 -upljadmin -p123qqq...A
# mysql -h192.168.4.52 -uscalemon -p123qqq...A
-----------------------------------------------------------------------------------------------------------------------------------
2.5 启动数据读写分离服务

57操作:
# maxscale -f /etc/maxscale.cnf

# ss -tunlp | grep :4006
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=5383,fd=11))

# ss -tunlp | grep :4026
tcp LISTEN 0 128 :::4026 :::* users:(("maxscale",pid=5383,fd=12))

# ss -tunlp | grep maxscale
tcp LISTEN 0 128 :::4026 :::* users:(("maxscale",pid=5383,fd=12))
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=5383,fd=11))


# killall -9 maxscale 通过杀进程,来停止maxscale服务
# ss -tunlp | grep maxscale
# ss -tunlp | grep :4006
# ss -tunlp | grep :4026

# maxscale -f /etc/maxscale.cnf
# ss -tunlp | grep maxscale
tcp LISTEN 0 128 :::4026 :::* users:(("maxscale",pid=5451,fd=12))
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=5451,fd=11))

# ss -tunlp | grep :4006
tcp LISTEN 0 128 :::4006 :::* users:(("maxscale",pid=5451,fd=11))

# ss -tunlp | grep :4026
tcp LISTEN 0 128 :::4026 :::* users:(("maxscale",pid=5451,fd=12))

如果3306端口也在启用的话,要停止数据库服务!
# ss -tunlp | grep :3306
tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",pid=1383,fd=14))

# systemctl stop mariadb
# ss -tunlp | grep :3306 查询不到了,说明3306端口已经停用了
-----------------------------------------------------------------------------------------------------------------------
3 在客户端验证数据读写分离服务的配置
3.1 在57本机访问管理服务查看监控信息
# maxadmin -uadmin -pmariadb -P4026 默认用户名是admin,密码是mariadb,指定端口号-P4026
出现页面如下
MaxScale> list 输入list,回车它会显示这个命令后面要跟的是什么
Unknown or missing option for the list command. Valid sub-commands are:
clients List all clients
dcbs List all DCBs
filters List all filters
listeners List all listeners
modules List all currently loaded modules
monitors List all monitors
services List all the services
servers List all servers
sessions List all sessions
threads List polling threads
commands List registered commands


MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.4.51 | 3306 | 0 | Master, Running 可以看到主库的状态
server2 | 192.168.4.52 | 3306 | 0 | Slave, Running 可以看到从库的状态
-------------------+-----------------+-------+-------------+--------------------

如果看不到主库或者从库的状态,说明配置监控的那个设置写错了,有问题。

MaxScale> exit
---------------------------------------------------------------------------------------------------
3.2客户端50 连接57,对数据做访问(查询和插入操作),测试能否实现读写分离。

50操作:
# mysql -h192.168.4.57 -P4006 -uyaya100 -p123qqq...A -P是指定端口4006是我们指定的读写分离端口

mysql> select * from db1.a;
+------+
| id |
+------+
| 666 |
+------+
--------------------------------------------------------------------------------------------------
52操作:
mysql> insert into db1.a values(333);

mysql> select * from db1.a;
+------+
| id |
+------+
| 666 |
| 777 |
| 333 |
+------+
-----------------------------------------------------------------------------------------------------
51操作:
mysql> select * from db1.a;
+------+
| id |
+------+
| 666 |
| 777 |
+------+
-----------------------------------------------------------------------------------------------------------
50操作:
mysql> insert into db1.a values(888);

mysql> select * from db1.a;
+------+
| id |
+------+
| 666 |
| 777 |
| 333 |
| 888 |
+------+
----------------------------------------------------------------------------------------------------------
如果客户端使用的用户不存在,则会报错。 是57用pljadmin这个用户登陆2台服务器查看是否有bob这个用户的。
# mysql -h192.168.4.57 -P4006 -ubob -p123qqq...A
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1045 (28000): Access denied for user 'bob'@'::ffff:192.168.4.50' (using password: YES)

 

posted @ 2019-04-30 22:31  安于夏  阅读(147)  评论(0编辑  收藏  举报