备注:文章编写时间201904-201905期间,后续官方在github的更新没有被写入
MySQL服务器配置 [MySQL Server Configuration]
一、在ProxySQL中配置后端MySQL服务器[Configuring Backend MySQL Servers in ProxySQL]
MySQL服务器的配置在mysql_servers表和mysql_replication_hostgroups表(可选)中。
友情提醒:在阅读下部分之前,请确保了解了ProxySQL使用的多层配置系统。
特别注意:
1)如果没有运行LOAD MYSQL SERVERS TO RUNTIME,那么mysql_servers和mysql_replication_hostgroups表中的更改不会生效;
2)如果没有运行SAVE MYSQL SERVERS TO DISK 保存配置到的磁盘,那么mysql_servers和mysql_replication_hostgroups表中的更改
将在重启/崩溃后丢失。
对mysql_servers和mysql_replication_hostgroups的更改将被视为编辑配置文件而不会被保存或让服务重新加载。
二、将mysql服务器配置从MEMORY层复制到RUNTIME层 [Copying mysql servers from memory to runtime]
将MySQL服务器和复制主机组配置信息从MEMORY层数据库加载到RUNTIME层的数据结构在中:
Admin> LOAD MYSQL SERVERS TO RUNTIME;
其他可被接受的别名操作:
Admin> LOAD MYSQL SERVERS TO RUN;
Admin> LOAD MYSQL SERVERS FROM MEM;
Admin> LOAD MYSQL SERVERS FROM MEMORY;
三、将mysql服务器配置从MEMORY层持久化到DISK层[Copying mysql servers from memory to disk]
将MySQL服务器和复制主机组配置信息从MEMORY层数据库保留到DISK的数据库上:
Admin> SAVE MYSQL SERVERS TO DISK;
其他可被接受的别名操作:
Admin> SAVE MYSQL SERVERS FROM MEM;
Admin> SAVE MYSQL SERVERS FROM MEMORY;
四、将mysql服务器配置从RUNTIME层拷贝到MEMORY层[Copying mysql servers from runtime to memory]
将MySQL服务器和复制主机组配置信息从RUNTIME层数据结构保留到MEMORY数据库:
Admin> SAVE MYSQL SERVERS TO MEMORY;
其他可被接受的别名操作:
Admin> SAVE MYSQL SERVERS TO MEM;
Admin> SAVE MYSQL SERVERS FROM RUN;
Admin> SAVE MYSQL SERVERS FROM RUNTIME;
五、将mysql服务器从DISK层复制到MEMORY层[Copying mysql servers from disk to memory]
将MySQL服务器和复制主机组配置信息从DISK层数据库加载到MEMORY层数据库:
Admin> LOAD MYSQL SERVERS TO MEMORY;
其他可被接受的别名操作:
Admin> LOAD MYSQL SERVERS TO MEM;
Admin> LOAD MYSQL SERVERS FROM DISK;
六、新增服务器[Adding a new server]
如果要添加新服务器,必须在mysql_servers表中插入新行以记录相关配置信息。注意,该表有几列是有默认值的。
以下添加了一个具有所有默认配置的新后端(服务器):
Admin> SELECT * FROM mysql_servers;
Empty set (0.00 sec)
Admin> INSERT INTO mysql_servers (hostname) VALUES ('127.0.0.1');
Query OK, 1 row affected (0.00 sec)
Admin> SELECT * FROM mysql_servers\G
*************************** 1. row ***************************
hostgroup_id: 0
hostname: 127.0.0.1
port: 3306
gtid_port: 0
status: ONLINE
weight: 1
compression: 0
max_connections: 1000
max_replication_lag: 0
use_ssl: 0
max_latency_ms: 0
comment:
1 row in set (0.00 sec)
七、将新服务器添加到主机组[Adding new servers to a hostgroup]
Admin> SELECT hostgroup_id,hostname,port FROM mysql_servers;
+--------------+-----------+------+
| hostgroup_id | hostname | port |
+--------------+-----------+------+
| 0 | 127.0.0.1 | 3306 |
+--------------+-----------+------+
1 row in set (0.00 sec)
INSERT INTO mysql_servers (hostgroup_id, hostname ,port)
VALUES (1, '127.0.0.1' ,24801), (1, '127.0.0.1' ,24802), (1,'127.0.0.1' ,24803);
Query OK, 3 rows affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port FROM mysql_servers;
+--------------+-----------+-------+
| hostgroup_id | hostname | port |
+--------------+-----------+-------+
| 0 | 127.0.0.1 | 3306 |
| 1 | 127.0.0.1 | 24801 |
| 1 | 127.0.0.1 | 24802 |
| 1 | 127.0.0.1 | 24803 |
+--------------+-----------+-------+
4 rows in set (0.00 sec)
八、配置与后端的连接数限制[Limiting the number of connections to a backend]
Admin> SELECT hostgroup_id,hostname,port,max_connections FROM mysql_servers;
+--------------+-----------+-------+-----------------+
| hostgroup_id | hostname | port | max_connections |
+--------------+-----------+-------+-----------------+
| 0 | 127.0.0.1 | 3306 | 1000 |
| 1 | 127.0.0.1 | 24801 | 1000 |
| 1 | 127.0.0.1 | 24802 | 1000 |
| 1 | 127.0.0.1 | 24803 | 1000 |
+--------------+-----------+-------+-----------------+
4 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET max_connections=10 WHERE hostname='127.0.0.1' AND port=24802;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,max_connections FROM mysql_servers;
+--------------+-----------+-------+-----------------+
| hostgroup_id | hostname | port | max_connections |
+--------------+-----------+-------+-----------------+
| 0 | 127.0.0.1 | 3306 | 1000 |
| 1 | 127.0.0.1 | 24801 | 1000 |
| 1 | 127.0.0.1 | 24802 | 10 |
| 1 | 127.0.0.1 | 24803 | 1000 |
+--------------+-----------+-------+-----------------+
4 rows in set (0.00 sec)
九、通过改变后端的权重来确定流量的优先级[Prioritizing traffic by changing the weight of a backend]
特别注意:权重仅在主机组内起作用,值越大被分到的流量的几率越多!!
Admin> SELECT hostgroup_id,hostname,port,weight FROM mysql_servers;
+--------------+-----------+-------+--------+
| hostgroup_id | hostname | port | weight |
+--------------+-----------+-------+--------+
| 0 | 127.0.0.1 | 3306 | 1 |
| 1 | 127.0.0.1 | 24801 | 1 |
| 1 | 127.0.0.1 | 24802 | 1 |
| 1 | 127.0.0.1 | 24803 | 1 |
+--------------+-----------+-------+--------+
4 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET weight=1000 WHERE port IN (24803) AND hostgroup_id=1;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,weight FROM mysql_servers;
+--------------+-----------+-------+--------+
| hostgroup_id | hostname | port | weight |
+--------------+-----------+-------+--------+
| 0 | 127.0.0.1 | 3306 | 1 |
| 1 | 127.0.0.1 | 24801 | 1 |
| 1 | 127.0.0.1 | 24802 | 1 |
| 1 | 127.0.0.1 | 24803 | 1000 |
+--------------+-----------+-------+--------+
4 rows in set (0.00 sec)
十、对特定后端使用SSL连接[Using SSL connections for a specific backend]
此示例显示如何为后端配置SSL(一个状态值而已),(但这不是全局配置SSL,全局请移步SSL configuration)。
Admin> SELECT hostgroup_id,hostname,port,use_ssl FROM mysql_servers;
+--------------+-----------+-------+---------+
| hostgroup_id | hostname | port | use_ssl |
+--------------+-----------+-------+---------+
| 0 | 127.0.0.1 | 3306 | 0 |
| 1 | 127.0.0.1 | 24801 | 0 |
| 1 | 127.0.0.1 | 24802 | 0 |
| 1 | 127.0.0.1 | 24803 | 0 |
+--------------+-----------+-------+---------+
4 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET use_ssl=1 WHERE hostname='127.0.0.1' AND port=3306;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,use_ssl FROM mysql_servers;
+--------------+-----------+-------+---------+
| hostgroup_id | hostname | port | use_ssl |
+--------------+-----------+-------+---------+
| 0 | 127.0.0.1 | 3306 | 1 |
| 1 | 127.0.0.1 | 24801 | 0 |
| 1 | 127.0.0.1 | 24802 | 0 |
| 1 | 127.0.0.1 | 24803 | 0 |
+--------------+-----------+-------+---------+
4 rows in set (0.00 sec)
十一、配置自动忽略(有复制延时的SLAVE)的延时阀值 [Automatically shunning slaves with replication lag]
Admin> SELECT hostgroup_id,hostname,port,max_replication_lag FROM mysql_servers;
+--------------+-----------+-------+---------------------+
| hostgroup_id | hostname | port | max_replication_lag |
+--------------+-----------+-------+---------------------+
| 0 | 127.0.0.1 | 3306 | 0 |
| 1 | 127.0.0.1 | 24801 | 0 |
| 1 | 127.0.0.1 | 24802 | 0 |
| 1 | 127.0.0.1 | 24803 | 0 |
+--------------+-----------+-------+---------------------+
4 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET max_replication_lag=30 WHERE hostname='127.0.0.1' AND port=24803;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,max_replication_lag FROM mysql_servers;
+--------------+-----------+-------+---------------------+
| hostgroup_id | hostname | port | max_replication_lag |
+--------------+-----------+-------+---------------------+
| 0 | 127.0.0.1 | 3306 | 0 |
| 1 | 127.0.0.1 | 24801 | 0 |
| 1 | 127.0.0.1 | 24802 | 0 |
| 1 | 127.0.0.1 | 24803 | 30 |
+--------------+-----------+-------+---------------------+
4 rows in set (0.00 sec)
使用此配置后,只有在主从复制延迟超过30秒的情况下,SLAVE才会被自动的暂时忽略。
如果max_replication_lag列设置为零(SET max_replication_lag = 0),则Monitor模块根本不会检查复制延迟,
即使复制延迟超过了阈值。更多信息请参阅参数mysql-monitor_slave_lag_when_null。
十二、将同一个服务器添加到两个不同的主机组[Adding a server to two different hostgroups]
mysql_servers表上的主键为(hostgroup_id,hostname,port);这意味着同一服务器可以存在于两个不同的主机组中。
Admin> SELECT hostgroup_id,hostname,port,weight,max_replication_lag FROM mysql_servers;
+--------------+-----------+-------+--------+---------------------+
| hostgroup_id | hostname | port | weight | max_replication_lag |
+--------------+-----------+-------+--------+---------------------+
| 0 | 127.0.0.1 | 3306 | 1 | 0 |
| 1 | 127.0.0.1 | 24801 | 1 | 0 |
| 1 | 127.0.0.1 | 24802 | 1 | 0 |
| 1 | 127.0.0.1 | 24803 | 1000 | 30 |
+--------------+-----------+-------+--------+---------------------+
4 rows in set (0.00 sec)
Admin> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (1,'127.0.0.1',3306);
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,weight,max_replication_lag FROM mysql_servers;
+--------------+-----------+-------+--------+---------------------+
| hostgroup_id | hostname | port | weight | max_replication_lag |
+--------------+-----------+-------+--------+---------------------+
| 0 | 127.0.0.1 | 3306 | 1 | 0 |
| 1 | 127.0.0.1 | 24801 | 1 | 0 |
| 1 | 127.0.0.1 | 24802 | 1 | 0 |
| 1 | 127.0.0.1 | 24803 | 1000 | 30 |
| 1 | 127.0.0.1 | 3306 | 1 | 0 |
+--------------+-----------+-------+--------+---------------------+
5 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET max_replication_lag=30, weight=1000;
Query OK, 5 rows affected (0.00 sec)
Admin> UPDATE mysql_servers SET weight=1 WHERE hostname='127.0.0.1' AND hostgroup_id=1 AND port=3306;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,weight,max_replication_lag FROM mysql_servers;
+--------------+-----------+-------+--------+---------------------+
| hostgroup_id | hostname | port | weight | max_replication_lag |
+--------------+-----------+-------+--------+---------------------+
| 0 | 127.0.0.1 | 3306 | 1000 | 30 |
| 1 | 127.0.0.1 | 24801 | 1000 | 30 |
| 1 | 127.0.0.1 | 24802 | 1000 | 30 |
| 1 | 127.0.0.1 | 24803 | 1000 | 30 |
| 1 | 127.0.0.1 | 3306 | 1 | 30 |
+--------------+-----------+-------+--------+---------------------+
5 rows in set (0.00 sec)
在上面的示例中,对于hostgroup为1的组来说,如果配置为提供读取流量,则99.95%的流量将发送到权重为1000的24801、24802和24803
这3台服务器上,而0.05%的流量将正常发送到3306上。如果24801、24802和24803这3台服务器都不可用,那么所有的读取流量都将
发往3306上。
注意:max_replication_lag仅适用于SLAVE节点。如果服务器未启用复制功能,则Monitor不会执行任何操作。
十三、在后端服务器上启用压缩[Enable compression on a backend server]
要启用压缩,只需将mysql_servers.compression列设置为非零值就足够了。
请注意:
1、压缩功能仅在将该设置加载到RUNTIME层后新建立的连接上生效,之前已有连接不生效。
2、这里的压缩是指ProxySQL作为Client与后端MySQL进行通信时,使用的对数据进行压缩的功能,类似MySQL客户端的–compress选项。
MySQL 压缩协议适合的场景是 MySQL 的服务器端和客户端之间传输的数据量很大,或者可用带宽不高的情况,典型的场景有如下两个 :
1> 查询大量的数据,带宽不够(比如导出数据的时候)
2> 复制的时候 binlog 量太大,启用 slave_compressed_protocol 参数进行日志压缩复制。
Admin> SELECT hostgroup_id,hostname,port,compression FROM mysql_servers;
+--------------+-----------+-------+-------------+
| hostgroup_id | hostname | port | compression |
+--------------+-----------+-------+-------------+
| 0 | 127.0.0.1 | 3306 | 0 |
| 1 | 127.0.0.1 | 24801 | 0 |
| 1 | 127.0.0.1 | 24802 | 0 |
| 1 | 127.0.0.1 | 24803 | 0 |
| 1 | 127.0.0.1 | 3306 | 0 |
+--------------+-----------+-------+-------------+
5 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET compression=1 WHERE hostname='127.0.0.1' AND port=24802 AND hostgroup_id=1;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,compression FROM mysql_servers;
+--------------+-----------+-------+-------------+
| hostgroup_id | hostname | port | compression |
+--------------+-----------+-------+-------------+
| 0 | 127.0.0.1 | 3306 | 0 |
| 1 | 127.0.0.1 | 24801 | 0 |
| 1 | 127.0.0.1 | 24802 | 1 |
| 1 | 127.0.0.1 | 24803 | 0 |
| 1 | 127.0.0.1 | 3306 | 0 |
+--------------+-----------+-------+-------------+
5 rows in set (0.00 sec)
十四、安全的禁用后端服务器[Gracefully disabling a backend server]
要安全的禁用后端服务器,需要将其状态更改为 OFFLINE_SOFT 即可。其上活动事务和连接仍将可用,但此后不会向该节点发送新流量。
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
| 1 | 127.0.0.1 | 24802 | ONLINE |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------+
5 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET status='OFFLINE_SOFT' WHERE hostname='127.0.0.1' AND port=24802 AND hostgroup_id=1;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
| 1 | 127.0.0.1 | 24802 | OFFLINE_SOFT |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------------+
5 rows in set (0.00 sec)
十五、即刻禁用后端服务器[Immediately disabling a backend server]
要立即禁用后端服务器,需要将其状态更改为 OFFLINE_HARD 即可。此后,所有当前流量将立即终止,并且不会发送新流量到该节点。
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
| 1 | 127.0.0.1 | 24802 | OFFLINE_SOFT |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------------+
5 rows in set (0.01 sec)
Admin> UPDATE mysql_servers SET status='OFFLINE_HARD' WHERE hostname='127.0.0.1' AND port=24801 AND hostgroup_id=1;
Query OK, 1 row affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | OFFLINE_HARD |
| 1 | 127.0.0.1 | 24802 | OFFLINE_SOFT |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------------+
5 rows in set (0.00 sec)
十六、重新启用脱机/禁用的后端服务器[Re-enabling an offline / disabled backend server]
要将离线的后端重新启用,只需将其状态更改回 ONLINE 就足够了。
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | OFFLINE_HARD |
| 1 | 127.0.0.1 | 24802 | OFFLINE_SOFT |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------------+
5 rows in set (0.00 sec)
Admin> UPDATE mysql_servers SET status='ONLINE' WHERE status NOT IN ('ONLINE'); ==>这里用了NOT,可根据实际情况修改过滤条件
Query OK, 2 rows affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
| 1 | 127.0.0.1 | 24802 | ONLINE |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------+
5 rows in set (0.00 sec)
十七、删除后端服务器[Removing a backend server]
ProxySQL允许将一个后端服务器彻底的删除,只需要将其从mysql_servers表中完全删除即可。
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
| 1 | 127.0.0.1 | 24802 | ONLINE |
| 1 | 127.0.0.1 | 24803 | ONLINE |
| 1 | 127.0.0.1 | 3306 | ONLINE |
+--------------+-----------+-------+--------+
5 rows in set (0.00 sec)
注意:虽然可以直接执行删除操作,但是为了操作的平滑、安全,数据的完整、可控,还是建议先修改状态让其软下线,等待其上执行的所有SQL都结束后再执行后面的删除动作。
Admin> UPDATE mysql_servers SET status='OFFLINE_SOFT' WHERE hostgroup_id=1 AND port IN (3306,24802,24803);
Query OK, 3 rows affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
| 1 | 127.0.0.1 | 24802 | OFFLINE_SOFT |
| 1 | 127.0.0.1 | 24803 | OFFLINE_SOFT |
| 1 | 127.0.0.1 | 3306 | OFFLINE_SOFT |
+--------------+-----------+-------+--------------+
5 rows in set (0.00 sec)
Admin> DELETE FROM mysql_servers WHERE hostgroup_id=1 AND port IN (3306,24802,24803);
Query OK, 3 rows affected (0.00 sec)
Admin> SELECT hostgroup_id,hostname,port,status FROM mysql_servers;
+--------------+-----------+-------+--------+
| hostgroup_id | hostname | port | status |
+--------------+-----------+-------+--------+
| 0 | 127.0.0.1 | 3306 | ONLINE |
| 1 | 127.0.0.1 | 24801 | ONLINE |
+--------------+-----------+-------+--------+
2 rows in set (0.00 sec)
在ProxySQL内部,直接删除后端服务和将其设置为OFFLINE_HARD的方式相同。当执行LOAD MYSQL SERVERS TO RUNTIME时,
Hostgroup_Manager将检测到后端服务器已被删除,并在内部将其标记为OFFLINE_HARD。