Master :172.25.4.1(server1)
Slave: 172.25.4.3(server3)
Proxy:172.25.4.2(server2)
实验前提搭建好主从复制,搭建详情查看我上片博客:https://blog.csdn.net/y_yang666/article/details/87891869
Server2配置(proxy):
1.下载mysl-proxy包,并解压,并将目录名改为mysql
[root@server2 ~]# ls
mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@server2 ~]# tar zxf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C /usr/local
[root@server2 ~]# cd /usr/local/
[root@server2 local]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit/ mysql-proxy
2.创建主配置文件目录conf和日志目录logs
[root@server2 mysql-proxy]# mkdir conf
[root@server2 mysql-proxy]# mkdir logs
3.编辑配置文件
[root@server2 conf]# pwd
/usr/local/mysql-proxy/conf
[root@server2 conf]# vim mysql-proxy.conf
[mysql-proxy]
proxy-address=172.25.4.2:3306 ##及监听本机接口
proxy-read-only-backend-addresses=172.25.4.3:3306 ##只读(slave:server3)
proxy-backend-addresses=172.25.4.1:3306 ##读写(master:server1)
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua ##lua脚本位置
daemon=true
user=root ##proxy运行用户身份
log-level=debug ##日志类型为debug
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log
keepalive=true
admin-username=admin ##管理员
admin-password=westos
admin-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/lua/admin.lua
3.给予配置文件权限
[root@server2 conf]# chmod 660 mysql-proxy.conf
4.修改lua脚本,修改最小连接数为1和最大连接数为2,当连接数超过2个时一定发生读写分离,方便实验
[root@server2conf]#vim/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
min_idle_connections = 1, ##默认为4
max_idle_connections = 2, ##默认为8
5.修改环境变量并刷新
[root@server2 conf]# vim ~/.bash_profile
[root@server2 conf]# source ~/.bash_profile ##刷新
6.运行mysql-proxy,查看3306端口和4041端口
[root@server2 conf]# mysql-proxy --plugins=admin --plugins=proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@server2 conf]# netstat -tnlp
[root@server2 ] mysql -p
mysql> grant select, update, insert on *.* to proxy@'172.25.4.%' identified by 'Yang+0428';
8.真机上远程以管理员登陆mysql-proxy调度器,查看读写状态,此时ro与rw端状态都为unknown
[root@foundation4 ~]# mysql -uadmin -pwestos -P 4041 -h 172.25.4.2
-P:查看的进程端口号;admin:管理员
9.在打开一个终端,远程登陆调度器,在表中插入信息
[kiosk@foundation4 ~]$ mysql -h 172.25.4.2 -uproxy -pYang+0428
MySQL [(none)]> use westos;
MySQL [westos]> desc usertb;
MySQL [westos]> insert into usertb values ('user1','11111');
10.连续打开两个终端,都远程登陆调度器mysql-proxy,查看信息
[kiosk@foundation4 ~]$ mysql -h 172.25.4.2 -uproxy -pYang+0428
MySQL [westos]> insert into usertb values ('user2','22222');
MySQL [westos]> select * from usertb;
再次查看读写状态,当第三个终端远程登陆时一定会发生读写分离,此时ro与rw状态都为up
关掉slave从库(server3),第三个终端远程登陆调度器,插入信息,在查看,发现没有刚才插入的信息,因为slave端还没有同步到主库的信息