ProxySQL
设置主机名 && 安装 MySQL Client && 启动 ProxySQL && 添加 MySQL 节点
$ hostnamectl --static set-hostname proxysql
$ cat >> /etc/hosts <<EOF
192.168.213.25 proxysql
192.168.213.26 mysql-master
192.168.213.27 mysql-slave-01
192.168.213.28 mysql-slave-02
EOF
# https://dev.mysql.com/downloads/mysql/5.7.html
$ rpm -ivh mysql-community-client-5.7.34-1.el7.x86_64.rpm mysql-community-common-5.7.34-1.el7.x86_64.rpm mysql-community-libs-5.7.34-1.el7.x86_64.rpm
$ tree
.
├── datadir
├── proxysql.cnf
└── start.sh
$ cat proxysql.cnf
datadir="/var/lib/proxysql"
admin_variables=
{
admin_credentials="admin:admin;radmin:radmin"
mysql_ifaces="0.0.0.0:6032"
}
$ cat start.sh
#!/bin/sh
docker run --rm -d -v $PWD/datadir:/var/lib/proxysql -v $PWD/proxysql.cnf:/etc/proxysql.cnf -p 6032:6032 -p 6033:6033 -p 6080:6080 docker.io/proxysql/proxysql
mysql -h127.0.0.1 -P6032 -uradmin -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select * from mysql_servers;
Empty set (0.00 sec)
mysql> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.213.26',3306),(10,'192.168.213.27',3306),(10,'192.168.213.28',3306);
Query OK, 3 rows affected (0.00 sec)
mysql> load mysql servers to runtime;
Query OK, 0 rows affected (0.01 sec)
mysql> save mysql servers to disk;
Query OK, 0 rows affected (0.03 sec)
MySQL-Master
添加监控账号
mysql -h mysql-master -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.34-log MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create user monitor@'192.168.213.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication client on *.* to monitor@'192.168.213.%' ;
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
ProxySQL
配置监测账号 && 读写分组
mysql -h127.0.0.1 -P6032 -uradmin -p
Enter password:
mysql> update global_variables set variable_value = 'monitor' where variable_name = 'mysql-monitor_username';
Query OK, 1 row affected (0.00 sec)
mysql> update global_variables set variable_value = '123456' where variable_name = 'mysql-monitor_password';
Query OK, 1 row affected (0.00 sec)
mysql> load mysql variables to runtime;
Query OK, 0 rows affected (0.01 sec)
mysql> save mysql variables to disk;
Query OK, 140 rows affected (0.01 sec)
mysql> select * from mysql_server_connect_log;
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| hostname | port | time_start_us | connect_success_time_us | connect_error |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
| 192.168.213.26 | 3306 | 1624772997036566 | 0 | Access denied for user 'monitor'@'192.168.213.25' (using password: YES) |
| 192.168.213.27 | 3306 | 1624773116084709 | 0 | Access denied for user 'monitor'@'192.168.213.25' (using password: YES) |
| 192.168.213.28 | 3306 | 1624773117060762 | 0 | Access denied for user 'monitor'@'192.168.213.25' (using password: YES) |
| 192.168.213.26 | 3306 | 1624773122005170 | 1844 | NULL |
| 192.168.213.27 | 3306 | 1624773122976601 | 3108 | NULL |
| 192.168.213.28 | 3306 | 1624773123947164 | 1855 | NULL |
+----------------+------+------------------+-------------------------+-------------------------------------------------------------------------+
18 rows in set (0.00 sec)
mysql> select * from mysql_server_ping_log;
+----------------+------+------------------+----------------------+-------------------------------------------------------------------------+
| hostname | port | time_start_us | ping_success_time_us | ping_error |
+----------------+------+------------------+----------------------+-------------------------------------------------------------------------+
| 192.168.213.26 | 3306 | 1624772948655044 | 0 | Access denied for user 'monitor'@'192.168.213.25' (using password: YES) |
| 192.168.213.27 | 3306 | 1624772956494083 | 0 | Access denied for user 'monitor'@'192.168.213.25' (using password: YES) |
| 192.168.213.28 | 3306 | 1624772964512596 | 0 | Access denied for user 'monitor'@'192.168.213.25' (using password: YES) |
| 192.168.213.26 | 3306 | 1624773170228688 | 1552 | NULL |
| 192.168.213.27 | 3306 | 1624773170443246 | 1557 | NULL |
| 192.168.213.28 | 3306 | 1624773178317173 | 570 | NULL |
+----------------+------+------------------+----------------------+-------------------------------------------------------------------------+
228 rows in set (0.00 sec)
mysql> select * from mysql_server_read_only_log;
Empty set (0.00 sec)
mysql> select * from mysql_server_replication_lag_log;
Empty set (0.00 sec)
mysql> select * from mysql_servers\G
*************************** 1. row ***************************
hostgroup_id: 10
hostname: 192.168.213.26
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:
*************************** 2. row ***************************
hostgroup_id: 10
hostname: 192.168.213.27
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:
*************************** 3. row ***************************
hostgroup_id: 10
hostname: 192.168.213.28
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:
3 rows in set (0.00 sec)
mysql> insert into mysql_replication_hostgroups(writer_hostgroup, reader_hostgroup) values(10,20);
Query OK, 1 row affected (0.00 sec)
mysql> load mysql servers to runtime;
Query OK, 0 rows affected (0.00 sec)
mysql> save mysql servers to disk;
Query OK, 0 rows affected (0.03 sec)
mysql> select * from mysql_servers\G
*************************** 1. row ***************************
hostgroup_id: 10
hostname: 192.168.213.26
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:
*************************** 2. row ***************************
hostgroup_id: 20
hostname: 192.168.213.28
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:
*************************** 3. row ***************************
hostgroup_id: 20
hostname: 192.168.213.27
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:
3 rows in set (0.00 sec)
mysql> select * from mysql_server_read_only_log;
+----------------+------+------------------+-----------------+-----------+-------+
| hostname | port | time_start_us | success_time_us | read_only | error |
+----------------+------+------------------+-----------------+-----------+-------+
| 192.168.213.26 | 3306 | 1624773780357419 | 1237 | 0 | NULL |
| 192.168.213.27 | 3306 | 1624773780369101 | 1215 | 1 | NULL |
| 192.168.213.28 | 3306 | 1624773780379679 | 1243 | 1 | NULL |
+----------------+------+------------------+-----------------+-----------+-------+
81 rows in set (0.00 sec)
MySQL-Master
添加代理账号
$ mysql -h mysql-master -uroot -p
mysql> grant all on *.* to proxysql@'192.168.213.%' identified by '123456';
ProxySQL
配置代理账号 && 测试读写分离 && Web UI && 定时调度
$ mysql -h127.0.0.1 -uradmin -P6032 --prompt='proxysql>' -pradmin
proxysql>insert into mysql_users(username, password, default_hostgroup) values('proxysql', '123456', 10);
proxysql>select * from mysql_users\G
*************************** 1. row ***************************
username: proxysql
password: 123456
active: 1
use_ssl: 0
default_hostgroup: 10
default_schema: NULL
schema_locked: 0
transaction_persistent: 1
fast_forward: 0
backend: 1
frontend: 1
max_connections: 10000
attributes:
comment:
1 row in set (0.00 sec)
proxysql>update mysql_users set transaction_persistent=1 where username='proxysql';
proxysql> load mysql users to runtime;
proxysql> save mysql users to disk;
$ mysql -h127.0.0.1 -uproxysql -P6033 -p123456 -e 'SELECT @@server_id;'
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
$ mysql -h127.0.0.1 -uproxysql -P6033 -p123456 -e 'SELECT * FROM demo_proxysql.table01'
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
| 6 | F |
+----+------+
$ mysql -h127.0.0.1 -uproxysql -P6033 -p123456 -e 'INSERT INTO demo_proxysql.table01(name)values("G"),("H");'
$ mysql -h127.0.0.1 -uproxysql -P6033 -p123456 -e 'SELECT * FROM demo_proxysql.table01'
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | D |
| 5 | E |
| 6 | F |
| 7 | G |
| 8 | H |
+----+------+
$ mysql -h127.0.0.1 -uradmin -P6032 -pradmin --prompt='proxysql>'
proxysql>insert into mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply)values(1,1,'SELECT.*FOR UPDATE$',10,1),(2,1,'^SELECT',20,1);
proxysql>load mysql query rules to runtime;
proxysql>save mysql query rules to disk;
$ mysql -h127.0.01 -P6033 -uproxysql -p123456 -e 'SELECT @@server_id;'
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
$ mysql -h127.0.01 -P6033 -uproxysql -p123456 -e 'SELECT @@server_id;'
+-------------+
| @@server_id |
+-------------+
| 3 |
+-------------+
$ mysql -h127.0.01 -P6033 -uproxysql -p123456 -e 'START TRANSACTION; SELECT @@server_id; COMMIT; SELECT @@server_id;'
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
+-------------+
| @@server_id |
+-------------+
| 2 |
+-------------+
$ mysql -h127.0.01 -P6033 -uproxysql -p123456 -e 'START TRANSACTION; SELECT @@server_id; COMMIT; SELECT @@server_id;'
+-------------+
| @@server_id |
+-------------+
| 1 |
+-------------+
+-------------+
| @@server_id |
+-------------+
| 3 |
+-------------+
$ mysql -h127.0.0.1 -P6032 -uproxysql -p123456 --prompt='proxysql>'
proxysql>select hostgroup, count_star, digest_text from stats_mysql_query_digest;
+-----------+------------+------------------------------------------------------+
| hostgroup | count_star | digest_text |
+-----------+------------+------------------------------------------------------+
| 10 | 4 | START TRANSACTION |
| 10 | 4 | COMMIT |
| 20 | 18 | SELECT @@server_id |
| 10 | 1 | INSERT INTO demo_proxysql.table01(name)values(?),(?) |
| 10 | 9 | SELECT @@server_id |
| 10 | 1 | SELECT * FORM demo_proxysql.table01 |
| 10 | 27 | select @@version_comment limit ? |
| 10 | 2 | SELECT * FROM demo_proxysql.table01 |
+-----------+------------+------------------------------------------------------+
8 rows in set (0.00 sec)
proxysql>SELECT 1 FROM stats_mysql_query_digest_reset LIMIT 1;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
proxysql>select hostgroup, count_star, digest_text from stats_mysql_query_digest;
Empty set (0.01 sec)
$ mysql -h 127.0.0.1 -P6033 -uproxysql -p123456 -e 'INSERT INTO demo_proxysql.table01(name)VALUES("H"),("I"),("J");'
$ mysql -h 127.0.0.1 -P6033 -uproxysql -p123456 -e 'DELETE FROM demo_proxysql WHERE id = 4;'
$ mysql -h mysql-master -uroot -p123456 -e 'SELECT * FROM demo_proxysql.table01;'
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 5 | E |
| 6 | F |
| 7 | G |
| 8 | H |
| 9 | I |
| 10 | J |
+----+------+
$ mysql -h mysql-slave-01 -uroot -p123456 -e 'SELECT * FROM demo_proxysql.table01;'
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 5 | E |
| 6 | F |
| 7 | G |
| 8 | H |
| 9 | I |
| 10 | J |
+----+------+
$ mysql -h mysql-slave-02 -uroot -p123456 -e 'SELECT * FROM demo_proxysql.table01;'
+----+------+
| id | name |
+----+------+
| 1 | A |
| 2 | B |
| 3 | C |
| 5 | E |
| 6 | F |
| 7 | G |
| 8 | H |
| 9 | I |
| 10 | J |
+----+------+
$ mysql -h 127.0.0.1 -P6032 -uradmin -pradmin --prompt='proxysql>'
*************************** 3. row ***************************
hostgroup: 20
count_star: 2
digest_text: SELECT * FROM `demo_proxysql`.`table01` WHERE `id` = ?
*************************** 4. row ***************************
hostgroup: 10
count_star: 3
digest_text: SHOW CREATE TABLE `demo_proxysql`.`table01`
*************************** 5. row ***************************
hostgroup: 10
count_star: 3
digest_text: SHOW COLUMNS FROM `demo_proxysql`.`table01`
*************************** 7. row ***************************
hostgroup: 10
count_star: 3
digest_text: SET NAMES utf8mb4
*************************** 8. row ***************************
hostgroup: 20
count_star: 1
digest_text: select * from hostgroup,count_star,digest_text from stats_mysql_query_digest
*************************** 9. row ***************************
hostgroup: 20
count_star: 1
digest_text: SELECT SCHEMA_NAME,DEFAULT_CHARACTER_SET_NAME,DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA
*************************** 10. row ***************************
hostgroup: 10
count_star: 2
digest_text: SHOW VARIABLES LIKE ?
*************************** 11. row ***************************
hostgroup: 20
count_star: 1
digest_text: SELECT TABLE_SCHEMA,TABLE_NAME,TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? ORDER BY TABLE_SCHEMA,TABLE_TYPE
*************************** 12. row ***************************
hostgroup: 20
count_star: 1
digest_text: SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? UNION SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? UNION SELECT COUNT(*) FROM information_schema.ROUTINES WHERE ROUTINE_SCHEMA = ?
*************************** 13. row ***************************
hostgroup: 20
count_star: 3
digest_text: SELECT * FROM `demo_proxysql`.`table01` LIMIT ?,?
*************************** 14. row ***************************
hostgroup: 20
count_star: 1
digest_text: SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,COLUMN_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? ORDER BY TABLE_SCHEMA,TABLE_NAME
*************************** 15. row ***************************
hostgroup: 10
count_star: 1
digest_text: DELETE FROM `demo_proxysql`.`table01` WHERE `id` = ?
*************************** 17. row ***************************
hostgroup: 10
count_star: 2
digest_text: INSERT INTO `demo_proxysql`.`table01`(`name`) VALUES (?)
19 rows in set (0.00 sec)
proxysql>update global_variables set variable_value='true' where variable_name='admin-web_enabled';
proxysql>update global_variables set variable_value = '6080' where variable_name = 'admin-web_port';
proxysql>load admin variables to runtime;
proxysql>save admin varibles to disk;
proxysql>select * from global_variables where variable_name LIKE 'admin-web%' or variable_name LIKE 'admin-stats%';
+----------------------------------------+----------------+
| variable_name | variable_value |
+----------------------------------------+----------------+
| admin-stats_credentials | stats:stats |
| admin-stats_mysql_connections | 60 |
| admin-stats_mysql_connection_pool | 60 |
| admin-stats_mysql_query_cache | 60 |
| admin-stats_mysql_query_digest_to_disk | 0 |
| admin-stats_system_cpu | 60 |
| admin-stats_system_memory | 60 |
| admin-web_enabled | true |
| admin-web_port | 6080 |
| admin-web_verbosity | 0 |
+----------------------------------------+----------------+
10 rows in set (0.00 sec)
proxysql>LOAD ADMIN VARIABLES TO RUNTIME;
proxysql>SAVE ADMIN VARIABLES TO DISK;
[root@proxysql proxysql]# firewall-cmd --query-port=6080/tcp
[root@proxysql proxysql]# firewall-cmd --add-port=6080/tcp --permanent
[root@proxysql proxysql]# firewall-cmd --reload
# https://192.168.213.25:6080/
$ mysql -h127.0.0.1 -P6032 -uradmin -pradmin --prompt='proxysql>'
proxysql> save mysql servers to disk;
proxysql> save mysql users to disk;
proxysql> save mysql query rules to disk;
proxysql> save mysql variables to disk;
proxysql> save global variables to disk;
proxysql> save admin variables to disk;
proxysql> save scheduler to disk;
$ vim health-check.sh
#!/bin/sh
DATE=`date "+%Y-%m-%d %H:%M:%S"`
echo "{\"dateTime\":\"$DATE\",\"status\":\"running\"}" >> /var/lib/proxysql/proxysql-health.log
$ chmod +x heath-check.sh
$ tree
.
├── datadir
│ ├── proxysql-ca.pem
│ ├── proxysql-cert.pem
│ ├── proxysql.db
│ ├── proxysql-key.pem
│ └── proxysql_stats.db
├── health-check.sh
├── proxysql.cnf
└── start.sh
2 directories, 9 files
$ docker stop fbc2
$ sh start.sh
$ mysql -h127.0.0.1 -uradmin -pradmin -P6032 --prompt='proxysql>'
proxysql> insert into scheduler(active,interval_ms,filename) values (1,5000,'/proxysql/health-check.sh');
proxysql> load scheduler to runtime;
proxysql> save scheduler to disk;
$ tail -f ./datadir/proxysql-health.log
{"dateTime":"2021-06-27 07:40:19","status":"running"}
{"dateTime":"2021-06-27 07:40:24","status":"running"}
{"dateTime":"2021-06-27 07:40:29","status":"running"}
{"dateTime":"2021-06-27 07:40:34","status":"running"}
参考
MySQL ProxySQL介绍
https://www.modb.pro/db/28841ProxySQL 配置详解及读写分离(+GTID)等功能说明 (完整篇)
https://www.cnblogs.com/kevingrace/p/10329714.html