MySQL中间件之ProxySQL(2):初试读写分离 (3)

以下是对心跳信息的监控(对ping指标的监控):

admin> select * from mysql_server_ping_log; +----------------+------+------------------+----------------------+-------------+ | hostname | port | time_start_us | ping_success_time_us | ping_error | +----------------+------+------------------+----------------------+-------------+ | 192.168.100.22 | 3306 | 1530968712666540 | 452 | NULL | | 192.168.100.23 | 3306 | 1530968712668779 | 458 | NULL | | 192.168.100.24 | 3306 | 1530968712671541 | 324 | NULL | | 192.168.100.22 | 3306 | 1530968722667071 | 1190 | NULL | | 192.168.100.23 | 3306 | 1530968722669574 | 1162 | NULL | | 192.168.100.24 | 3306 | 1530968722673162 | 1380 | NULL | | 192.168.100.22 | 3306 | 1530968732668840 | 1065 | NULL | | 192.168.100.23 | 3306 | 1530968732670709 | 1054 | NULL | | 192.168.100.24 | 3306 | 1530968732672703 | 1040 | NULL | +----------------+------+------------------+----------------------+-------------+

但是,read_only和replication_lag的监控日志都为空。

admin> select * from mysql_server_read_only_log; Empty set (0.00 sec) admin> select * from mysql_server_replication_lag_log; Empty set (0.00 sec)

这是因为还没有对ProxySQL中的节点分组:writer_hostgroup、reader_hostgroup。设置分组信息,需要修改的是main库中的mysql_replication_hostgroups表,该表只有3个字段:第一个字段名为writer_hostgroup,第二个字段为reader_hostgroup,第三个字段为注释字段,可随意写。

例如,指定写组的id为10,读组的id为20。

insert into mysql_replication_hostgroups values(10,20);

在该配置加载到RUNTIME生效之前,先查看下各mysql server所在的组。

admin> select hostgroup_id,hostname,port,status,weight from mysql_servers; +--------------+----------------+------+--------+--------+ | hostgroup_id | hostname | port | status | weight | +--------------+----------------+------+--------+--------+ | 10 | 192.168.100.22 | 3306 | ONLINE | 1 | | 10 | 192.168.100.23 | 3306 | ONLINE | 1 | | 10 | 192.168.100.24 | 3306 | ONLINE | 1 | +--------------+----------------+------+--------+--------+

3个节点都在hostgroup_id=10的组中。

现在,将刚才mysql_replication_hostgroups表的修改加载到RUNTIME生效。

load mysql servers to runtime; save mysql servers to disk;

一加载,Monitor模块就会开始监控后端的read_only值,当监控到read_only值后,就会按照read_only的值将某些节点自动移动到读/写组。

例如,此处所有节点都在id=10的写组,slave1和slave2都是slave,它们的read_only=1,这两个节点将会移动到id=20的组。如果一开始这3节点都在id=20的读组,那么移动的将是Master节点,会移动到id=10的写组。

看结果:

admin> select hostgroup_id,hostname,port,status,weight from mysql_servers; +--------------+----------------+------+--------+--------+ | hostgroup_id | hostname | port | status | weight | +--------------+----------------+------+--------+--------+ | 10 | 192.168.100.22 | 3306 | ONLINE | 1 | | 20 | 192.168.100.23 | 3306 | ONLINE | 1 | | 20 | 192.168.100.24 | 3306 | ONLINE | 1 | +--------------+----------------+------+--------+--------+ admin> select * from mysql_server_read_only_log; +----------------+------+------------------+-----------------+-----------+--------+ | hostname | port | time_start_us | success_time_us | read_only | error | +----------------+------+------------------+-----------------+-----------+--------+ | 192.168.100.22 | 3306 | 1530970372197917 | 8487 | 0 | NULL | | 192.168.100.23 | 3306 | 1530970372198992 | 7907 | 1 | NULL | | 192.168.100.24 | 3306 | 1530970372199835 | 8064 | 1 | NULL | | 192.168.100.22 | 3306 | 1530970373698824 | 10078 | 0 | NULL | | 192.168.100.23 | 3306 | 1530970373699825 | 9845 | 1 | NULL | | 192.168.100.24 | 3306 | 1530970373700786 | 10745 | 1 | NULL | +----------------+------+------------------+-----------------+-----------+--------+

1.4 配置mysql_users

上面的所有配置都是关于后端MySQL节点的,现在可以配置关于SQL语句的,包括:发送SQL语句的用户、SQL语句的路由规则、SQL查询的缓存、SQL语句的重写等等。

本小节是SQL请求所使用的用户配置,例如root用户。这要求我们需要先在后端MySQL节点添加好相关用户。这里以root和sqlsender两个用户名为例。

首先,在master节点上执行:(只需master执行即可,会复制给两个slave)

grant all on *.* to root@'192.168.100.%' identified by 'P@ssword1!'; grant all on *.* to sqlsender@'192.168.100.%' identified by 'P@ssword1!';

然后回到ProxySQL,配置mysql_users表,将刚才的两个用户添加到该表中。

insert into mysql_users(username,password,default_hostgroup) values('root','P@ssword1!',10); insert into mysql_users(username,password,default_hostgroup) values('sqlsender','P@ssword1!',10); load mysql users to runtime; save mysql users to disk;

mysql_users表有不少字段,最主要的三个字段为username、password和default_hostgroup:

username:前端连接ProxySQL,以及ProxySQL将SQL语句路由给MySQL所使用的用户名。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zzsygy.html