MySQL读写分离是指让master处理写操作,让slave处理读操作,非常适用于读操作量比较大的场景,可减轻master的压力。
使用mysql-proxy实现mysql的读写分离,mysql-proxy实际上是作为后端mysql主从服务器的代理,它直接接受客户端的请求,对SQL语句进行分析,判断出是读操作还是写操作,然后分发至对应的mysql服务器上。
因为数据库的写操作相对读操作是比较耗时的,所以数据库的读写分离,解决的是数据库的写入,影响了查询的效率。
rhel7.3 selinux and firewalld disabled
server1:172.25.79.1(master)
server2:172.25.79.2(slave)
server3:172.25.79.3(mysql-proxy)
#读写分离可以用很多软件实现:mysql-proxy 、MyCat 、Amoeba
这里介绍proxy
先做好server1和server2基于GTID的主从复制
server3上:
先关闭之前的mysql,因为proxy也用3306端口
systemctl stop mysqld
tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local
cd /usr/local/
ln -s mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy #软链接便于访问
这一步只是说明,可以不用进行操作
cd /usr/local/mysql-proxy/bin
./mysql-proxy --help ##查看帮助
./mysql-proxy --help-proxy ##查看proxy的帮助
./mysql-proxy --help-all ##查看所有帮助
cd /usr/local/mysql-proxy
mkdir conf ##建立配置文件目录
vim mysql-proxy.conf
[mysql-proxy]
proxy-address=0.0.0.0:3306
proxy-backend-addresses=172.25.65.1:3306
proxy-read-only-backend-addresses=172.25.65.2:3306
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
pid-file=/usr/local/mysql-proxy/log/mysql-proxy.pid
plugins=proxy
log-file=/usr/local/mysql-proxy/log/mysql-proxy.log
log-level=debug
keepalive=true
daemon=true
创建日志目录
mkdir /usr/local/mysql-proxy/log
min_idle_connections = 1, ##把原来的4和8改为1和2,默认超过4个连接才会启动读写分离,改为1个好测试
max_idle_connections = 2,
/usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
启动报错
2019-03-15 14:15:04: (critical) mysql-proxy-cli.c:326: loading config from '/usr/local/mysql-proxy/conf/mysql-proxy.conf' failed: permissions of /usr/local/mysql-proxy/conf/mysql-proxy.conf aren't secure (0660 or stricter required)
2019-03-15 14:15:04: (message) Initiating shutdown, requested from mysql-proxy-cli.c:328
2019-03-15 14:15:04: (message) shutting down normally, exit code is: 1
因为配置文件权限过大
chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf
再启动,正常
cat /usr/local/mysql-proxy/log/mysql-proxy.log
2019-03-15 15:07:22: (message) chassis-unix-daemon.c:136: [angel] we try to keep PID=2521 alive
2019-03-15 15:07:22: (debug) chassis-unix-daemon.c:157: waiting for 2521
2019-03-15 15:07:22: (debug) chassis-unix-daemon.c:121: we are the child: 2521
2019-03-15 15:07:22: (critical) plugin proxy 0.8.5 started
2019-03-15 15:07:22: (debug) max open file-descriptors = 1024
2019-03-15 15:07:22: (message) proxy listening on port 0.0.0.0:3306
2019-03-15 15:07:22: (message) added read/write backend: 172.25.136.1:3306
2019-03-15 15:07:22: (message) added read-only backend: 172.25.136.2:3306
mysql> grant insert,update,select on *.* to wsp@'%' identified by 'Wsp+123ld';
mysql> flush privileges;
mysql> use westos;
Database changed
mysql> create table usertb (
-> username varchar(10) not null,
-> password varchar(15) not null);
mysql> desc usertb;
[kiosk@foundation0 ~]$ mysql -h 172.25.136.3 -uwsp -pWsp+123ld
然后在server3上看到连接已经建立
[root@server3 log]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 2521 root 10u IPv4 28318 0t0 TCP *:mysql (LISTEN)
mysql-pro 2521 root 11u IPv4 28362 0t0 TCP server3:mysql->foundation0.ilt.example.com:55716 (ESTABLISHED)
mysql-pro 2521 root 12u IPv4 28363 0t0 TCP server3:47376->server1:mysql (ESTABLISHED
[root@server3 log]# lsof -i:3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
mysql-pro 2521 root 10u IPv4 28318 0t0 TCP *:mysql (LISTEN)
mysql-pro 2521 root 11u IPv4 28362 0t0 TCP server3:mysql->foundation0.ilt.example.com:55716 (ESTABLISHED)
mysql-pro 2521 root 12u IPv4 28363 0t0 TCP server3:47376->server1:mysql (ESTABLISHED)
mysql-pro 2521 root 13u IPv4 28709 0t0 TCP server3:mysql->foundation0.ilt.example.com:55722 (ESTABLISHED)
mysql-pro 2521 root 14u IPv4 28710 0t0 TCP server3:47382->server1:mysql (ESTABLISHED)
mysql-pro 2521 root 15u IPv4 28769 0t0 TCP server3:mysql->foundation0.ilt.example.com:55726 (ESTABLISHED)
mysql-pro 2521 root 16u IPv4 28770 0t0 TCP server3:55914->server2:mysql (ESTABLISHED)
'这次连接到了server2,说明读写分离启用
测试读写分离
MySQL [(none)]> use westos
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
MySQL [westos]> insert into usertb values('user1','123');
MySQL [westos]> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
+----------+----------+
1 row in set (0.00 sec)
发现server1和server2都能看到,看不出读写分离,因为存在主从复制
在server2上关闭复制
就是将本来读的从mysql关掉
这时再在物理机上插入数据
MySQL [westos]> insert into usertb values ('user4','123');
Query OK, 1 row affected (0.01 sec)
MySQL [westos]> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1 | 123 |
| user2 | 123 |
+----------+----------+
2 rows in set (0.00 sec)
发现插入的数据看不到,但是在server1上可以看到,这就说明了它在读的时候读的是server2上的数据,而写操作却写在了server1上