一、安装MHA
可参考https://blog.csdn.net/yabingshi_tech/article/details/47341705
二、安装ProxySQL
2.1 安装ProxySQL
[root@slave2 ~]# wget https://github.com/sysown/proxysql/releases/download/v2.1.1/proxysql-2.1.1-1-centos7.x86_64.rpm
rpm -ivh proxysql-2.1.1-1-centos7.x86_64.rpm
wget https://github.com/sysown/proxysql/releases/download/v2.0.18/proxysql-2.0.18-1-centos7.x86_64.rpm
rpm -i proxysql-2.0.18-1-centos7.x86_64.rpm
[root@slave2 ~]# proxysql --version
[root@slave2 ~]# systemctl start proxysql && systemctl status proxysql
[root@slave2 ~]# netstat -anlp | grep proxysql
tcp 0 0 0.0.0.0:6032 0.0.0.0:* LISTEN 26499/proxysql
tcp 0 0 0.0.0.0:6033 0.0.0.0:* LISTEN 26499/proxysql
启动后会监听两个端口,默认为6032和6033。6032端口是ProxySQL的管理端口,6033是ProxySQL对外提供服务的端口 (即连接到转发后端的真正数据库的转发端口)。
登录:
ProxySQL 的用户名和密码都是默认的admin(只能本机登录)
[root@slave2 ~]# mysql -h 127.0.0.1 -P 6032 -uadmin -padmin
2.2 配置ProxySQL
2.2.1 添加主从服务器节点
insert into mysql_servers(hostgroup_id,hostname,port) values (10,‘192.168.4.42‘,3307); #hostname里指定MHA的写浮动IP
insert into mysql_servers(hostgroup_id,hostname,port) values (20,‘192.168.4.45‘,3307); #mysql的从节点1
insert into mysql_servers(hostgroup_id,hostname,port) values (20,‘192.168.4.46‘,3307); #mysql的从节点2
load mysql servers to runtime;
save mysql servers to disk;
select * from mysql_servers;
2.2.2 创建监控账号
#在数据库主库上创建
grant usage on *.* to ‘monitor‘@‘192.168.4.47‘ identified by ‘mysql‘;
#在proxysql配置:
set mysql-monitor_username=‘monitor‘;
set mysql-monitor_password=‘mysql‘;
load mysql variables to runtime;
save mysql variables to disk;
2.2.3 为ProxySQL配置程序账号
注:SQL请求所使用的用户配置,都需要在MySQL节点创建上。
#在master上创建
grant all privileges on *.* to ‘proxysql‘@‘192.168.4.%‘ identified by ‘mysql‘ with grant option;
配置proxysql的mysql_users 表,将proxysql用户添加到该表中。
admin@itpux22:26:[none]> insert into mysql_users (username,password,default_hostgroup) values (‘proxysql‘,‘mysql‘,10);
admin@itpux 22:33: [none]> load mysql users to runtime;
admin@itpux 22:33: [none]> save mysql users to disk;
测试是否能访问数据库:
mysql -h 192.168.4.47 -uproxysql -pmysql -P 6033 -e "show databases;"
2.2.4 配置读写分离
1、把所有以select开头的语句全部分配到读组中,读组编号是20
insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,apply) values (2,1,‘^select‘,20,1);
2、把select .. for update语句,这是一个特殊的select语句,会产生一个写锁(排他锁),把他分到编号为10的写组中,其他所有操作都会默认路由到写组中
insert into mysql_query_rules(rule_id,active,match_pattern,destination_hostgroup,apply) values (1,1,‘^select.*for update$‘,10,1);
load mysql query rules to runtime;
save mysql query rules to disk;
MySQL [(none)]> select rule_id,active,match_pattern,destination_hostgroup,apply from mysql_query_rules;
三、验证读写分离
[root@manager mha]# cat a.sh
for i in {1..200}
do
mysql -h 192.168.4.47 -uproxysql -p666777 -P 6033 -e "insert into dan.t2(id,name) values($i,@@hostname);"
done
mysql -h 192.168.4.47 -uproxysql -p666777 -P 6033 -e "select count(*) from dan.t2 where name=‘主库的主机名‘"
看下数据量是200,说明全部写到了主库
[root@manager mha]# cat b.sh
for i in {1..200}
do
mysql -h 192.168.144.247 -uproxysql -p666777 -P 6033 -e "select @@hostname;"
done
sh b.sh > b.log
[root@manager mha]# cat b.log | grep slave1 | wc -l
89
[root@manager mha]# cat b.log | grep slave2 | wc -l
111
可以看到读均衡分散到了两个从节点上
四、验证读高可用
sh b.sh > b.log
[root@pc1 download]# cat b.log | grep pc3 | wc -l
0
[root@pc1 download]# cat b.log | grep pc2 | wc -l
200
说明slave2宕机后,查询全在slave1上进行。
注意:假如MHA切换过几次,如slave1是主库,slave1宕机后又恢复了,但是还没change master to现主库,此时ProxySQL从slave1上读数据,就还是延迟的。
ProxySQL还是单点,有时间待补充ProxySQL集群相关配置
原文链接:https://blog.csdn.net/yabingshi_tech/article/details/116030227