可参考https://blog.csdn.net/yabingshi_tech/article/details/47341705
[root@slave2 ~]# rpm -ivh proxysql-2.0.5-1-centos7.x86_64.rpm
点击下载
[root@slave2 ~]# proxysql --version
[root@slave2 ~]# systemctl start proxysql
[root@slave2 ~]# 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 -uadmin -padmin -h 127.0.0.1 -P 6032
insert into mysql_servers(hostgroup_id,hostname,port) values (10,'192.168.144.242',3307); #hostname里指定MHA的写浮动ip
insert into mysql_servers(hostgroup_id,hostname,port) values (20,'192.168.144.245',3307); #mysql的从节点1
insert into mysql_servers(hostgroup_id,hostname,port) values (20,'192.168.144.246',3307); #mysql的从节点2
load mysql servers to runtime;
save mysql servers to disk;
select * from mysql_servers;
#在数据库主库上创建
grant usage on *.* to 'monitor'@'192.168.144.247' identified by '123456';
#在proxysql配置:
set mysql-monitor_username='monitor';
set mysql-monitor_password='123456';
load mysql variables to runtime;
save mysql variables to disk;
注:SQL请求所使用的用户配置,都需要在 MySQL 节点创建上。
#在master上创建
grant all privileges on *.* to 'proxysql'@'192.168.144.%' identified by '666777' with grant option;
配置proxysql的mysql_users 表,将proxysql用户添加到该表中。
admin@itpux22:26:[none]>insert into mysql_users (username,password,default_hostgroup) values ('proxysql','666777',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.144.247 -uproxysql -p666777 -P 6033 -e "show databases;"
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;
+---------+--------+----------------------+-----------------------+-------+
| rule_id | active | match_pattern | destination_hostgroup | apply |
+---------+--------+----------------------+-----------------------+-------+
| 1 | 1 | ^select.*for update$ | 10 | 1 |
| 2 | 1 | ^select | 20 | 1 |
[root@manager mha]# cat a.sh
for i in {1..200}
do
mysql -h 192.168.144.247 -uproxysql -p666777 -P 6033 -e "insert into dan.t2(id,name) values($i,@@hostname);"
done
mysql -h 192.168.144.247 -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集群相关配置