在本文,主要修改main和monitor数据库中的表。
admin> show tables from main; +--------------------------------------------+ | tables | +--------------------------------------------+ | global_variables | | mysql_collations | | mysql_group_replication_hostgroups | | mysql_query_rules | | mysql_query_rules_fast_routing | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | proxysql_servers | | runtime_checksums_values | | runtime_global_variables | | runtime_mysql_group_replication_hostgroups | | runtime_mysql_query_rules | | runtime_mysql_query_rules_fast_routing | | runtime_mysql_replication_hostgroups | | runtime_mysql_servers | | runtime_mysql_users | | runtime_proxysql_servers | | runtime_scheduler | | scheduler | +--------------------------------------------+ admin> show tables from monitor; +------------------------------------+ | tables | +------------------------------------+ | mysql_server_connect_log | | mysql_server_group_replication_log | | mysql_server_ping_log | | mysql_server_read_only_log | | mysql_server_replication_lag_log | +------------------------------------+runtime_开头的是运行时的配置,这些是不能修改的。要修改ProxySQL的配置,需要修改了非runtime_表,修改后必须执行LOAD ... TO RUNTIME才能加载到RUNTIME生效,执行save ... to disk才能将配置持久化保存到磁盘。具体操作见后文。
insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.100.22',3306); insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.100.23',3306); insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.100.24',3306);注:上面语句中没有先切换到main库也执行成功了,因为ProxySQL内部使用的SQLite3数据库引擎,和MySQL的解析方式是不一样的。即使执行了USE main语句也是无任何效果的,但不会报错。
查看这3个节点是否插入成功,以及它们的状态。请认真读一读每个字段的名称,混个眼熟。
admin> select * from mysql_servers\G *************************** 1. row *************************** hostgroup_id: 10 hostname: 192.168.100.22 port: 3306 status: ONLINE weight: 1 compression: 0 max_connections: 1000 max_replication_lag: 0 use_ssl: 0 max_latency_ms: 0 comment: *************************** 2. row *************************** hostgroup_id: 10 hostname: 192.168.100.23 port: 3306 status: ONLINE weight: 1 compression: 0 max_connections: 1000 max_replication_lag: 0 use_ssl: 0 max_latency_ms: 0 comment: *************************** 3. row *************************** hostgroup_id: 10 hostname: 192.168.100.24 port: 3306 status: ONLINE weight: 1 compression: 0 max_connections: 1000 max_replication_lag: 0 use_ssl: 0 max_latency_ms: 0 comment: 3 rows in set (0.00 sec)修改后,加载到RUNTIME,并保存到disk。
load mysql servers to runtime; save mysql servers to disk; 1.3 监控后端MySQL节点添加节点之后,还需要监控后端节点。对于后端是主从复制的环境来说,这是必须的,因为ProxySQL需要通过每个节点的read_only值来自动调整它们是属于读组还是写组。
首先在后端master节点上创建一个用于监控的用户名(只需在master上创建即可,因为会复制到slave上),这个用户名只需具有USAGE权限即可。如果还需要监控复制结构中slave是否严重延迟于master(先混个眼熟:这个俗语叫做"拖后腿",术语叫做"replication lag"),则还需具备replication client权限。这里直接赋予这个权限。
# 在master上执行: mysql> create user monitor@'192.168.100.%' identified by 'P@ssword1!'; mysql> grant replication client on *.* to monitor@'192.168.100.%';然后回到ProxySQL上配置监控。
set mysql-monitor_username='monitor'; set mysql-monitor_password='P@ssword1!';以上设置实际上是在修改global_variables表,它和下面两个语句是等价的:
UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username'; UPDATE global_variables SET variable_value='P@ssword1!' WHERE variable_name='mysql-monitor_password';修改后,加载到RUNTIME,并保存到disk。
load mysql variables to runtime; save mysql variables to disk;验证监控结果:ProxySQL监控模块的指标都保存在monitor库的log表中。
以下是连接是否正常的监控(对connect指标的监控):(在前面可能会有很多connect_error,这是因为没有配置监控信息时的错误,配置后如果connect_error的结果为NULL则表示正常)
admin> select * from mysql_server_connect_log; +----------------+------+------------------+-------------------------+---------------+ | hostname | port | time_start_us | connect_success_time_us | connect_error | +----------------+------+------------------+-------------------------+---------------+ | 192.168.100.22 | 3306 | 1530968712977867 | 4174 | NULL | | 192.168.100.23 | 3306 | 1530968712988986 | 4908 | NULL | | 192.168.100.24 | 3306 | 1530968713000074 | 3044 | NULL | | 192.168.100.22 | 3306 | 1530968772978982 | 3407 | NULL | | 192.168.100.23 | 3306 | 1530968772989627 | 3404 | NULL | | 192.168.100.24 | 3306 | 1530968773000778 | 3444 | NULL | +----------------+------+------------------+-------------------------+---------------+