【20180523】ProxySQL+MHA的配置以及一些问题描述

ProxySQL+MHA的各自搭建在这里不再描述了。具体的搭建可以自行谷歌,但是需要注意的是MHA在进行故障转移的时候不需要做VIP切换操作即可。

环境配置信息

  1. master: 172.16.3.5:3307
  2. slave:
    • 172.16.3.6:3307
    • 172.16.3.7:3306
    • 172.16.3.6:3307
  3. proxysql: 172.16.3.15

ProxySQL配置

  1. 配置链接后端MySQL实例的用户。这个用户一定要真实存在,并且这个用户会以rpl@172.16.3.15的身份去访问后端的MySQL实例。
    admin@mysqldb 10:51: [(none)]> select username,password,active,default_hostgroup from mysql_users;+----------+-------------+--------+-------------------+| username | password | active | default_hostgroup |+----------+-------------+--------+-------------------+| rpl | Redhat_2018 | 1 | 10 |+----------+-------------+--------+-------------------+1 row in set (0.01 sec)
    • active: 表示启用这个用户,这个用户是活动用户
    • default_hostgroup: 默认的MySQL组,若是在mysql_query_rules没有匹配到,则所有的操作则会默认在default_hostgroup这个组中执行。
    • transaction_persistent: 同一个事务在同一个实例执行
  2. 配置链接后端的MySQL实例。
    admin@mysqldb 11:48: [(none)]> select hostgroup_id,hostname,port,weight from mysql_servers;+--------------+------------+------+--------+| hostgroup_id | hostname | port | weight |+--------------+------------+------+--------+| 11 | 172.16.3.6 | 3307 | 100 || 10 | 172.16.3.5 | 3307 | 100 || 10 | 172.16.3.7 | 3307 | 100 || 11 | 172.16.3.7 | 3306 | 100 || 11 | 172.16.3.7 | 3307 | 100 |+--------------+------------+------+--------+5 rows in set (0.00 sec)
    • hostgroup_id: 根据组ID分成不同类型的
  3. 配置mysql_replication_hostgroups。主要目的是监控后端的MySQL实例是master还是slave。

    admin@mysqldb 11:50: [(none)]> select * from mysql_replication_hostgroups;+------------------+------------------+---------+| writer_hostgroup | reader_hostgroup | comment |+------------------+------------------+---------+| 10 | 11 | |+------------------+------------------+---------+1 row in set (0.00 sec)
    • writer_hostgroup: 专门进行写操作,会监控MySQL实例是否关闭了super_read_only和read_only。
    • reader_hostgroup: 专门进行读操作,会监控MySQL实例是否开启了super_read_only和read_only。
  4. 配置mysql_query_rules,这个是重中之重。
    admin@mysqldb 14:28: [(none)]> select rule_id,active,match_pattern,apply from mysql_query_rules;+---------+--------+-----------------------+-------+| rule_id | active | match_pattern | apply |+---------+--------+-----------------------+-------+| 1 | 1 | ^SELECT .*FOR UPDATE$ | 1 || 2 | 1 | ^SELECT | 1 |+---------+--------+-----------------------+-------+2 rows in set (0.00 sec)admin@mysqldb 14:29: [(none)]>
    • rule_id: 过滤规则的ID,一般都是从小往大进行匹配的,所以建议不要从1开始,最好是从中间的值开始,方便后续出现紧急情况可以插入在前面。
  5. 配置监控用户。这个用户也必须真实存在,并且要有super,replication client的权限。
    set global mysql-monitor_username=‘username‘;set global mysql-monitor_password=‘new_password‘

注意点

  1. 路由规则越多,性能越差。
  2. 通过ProxySQL链接后端的MySQL的时候,使用use schema切换数据库的时候会花费比较长的时间。
    rpl@mysqldb 14:58: [(none)]> use yeahDatabase changedrpl@mysqldb 14:58: [yeah]> use zabbix
    • 在使用mysql登陆的时候加上参数-A,就是每次链接不会自动刷新table的缓存信息,就是不会将最新的table信息缓存在本地。
    • 遇到这种情况前提就是在配置mysql_users表的时候插入的时候没有指定默认的default_hostgroup,导致默认的default_hostgroup为0,是information_schema,若是选择了一个已经存在的hostgroup_id则不会出现这种情况。
  3. 在proxysql的表mysql_servers中插入server信息,将master的hostgroup_id设置为10,slave的hostgroup_id设置为11,但是发现hostgroup_id为10这个分组出现了slave的信息。
    admin@mysqldb 15:02: [(none)]> select * from mysql_servers;+--------------+------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+| hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |+--------------+------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+| 11 | 172.16.3.6 | 3307 | ONLINE | 100 | 0 | 1000 | 0 | 0 | 0 | slave || 10 | 172.16.3.5 | 3307 | ONLINE | 100 | 0 | 1000 | 0 | 0 | 0 | master || 10 | 172.16.3.7 | 3307 | ONLINE | 100 | 0 | 1000 | 0 | 0 | 0 | slave || 11 | 172.16.3.7 | 3306 | ONLINE | 100 | 0 | 1000 | 0 | 0 | 0 | slave || 11 | 172.16.3.7 | 3307 | ONLINE | 100 | 0 | 1000 | 0 | 0 | 0 | slave |+--------------+------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+5 rows in set (0.00 sec)admin@mysqldb 15:05: [(none)]>

    可以很明显的看得到在第三行172.16.3.7:3307这个实例出现了俩次,但是实际情况是我只insert了1次,并且将其insert 到hostgroup_id为11的组上面,但是实际情况却在10,11俩组分别出现了。

    • 这个是因为proxysql会主动去检测后端的参数read_only和super_read_only,当发现这俩个参数是OFF的时候它会认为这个MySQL实例是master,所以需要将这俩个参数开启,在proxysql中就发现slave的信息不回出现在hostgroup_id为10的分组里面了。
  4. 登陆之后发现无法执行show tables,create table 之类的操作。
    rpl@mysqldb 15:19: [yeah]> show tables;ERROR 9001 (HY000): Max connect timeout reached while reaching hostgroup 0 after 10000msrpl@mysqldb 15:20: [yeah]>
    • 主要是因为在表mysql_users插入用户信息的时候没有设置default_hostgroup,那么在路由规则不匹配的时候它会默认去匹配hostgroup_id为0,default_hostgroup为information_schema。这个时候只需要将default_hostgroup设置为10就可以了。

相关文章