proxySQL 学习

-- 参考文档:

https://proxysql.com/documentation/step-by-step-installation-guide

mysql版本:8.4.1

proxysql版本:2.6.6-8-gc03c5ee (刚开是装的2.0版本,有问题)

mysql mgr信息,是同一台机器上的3个mysql组成了mgr 。(注意这里的MEMBER_HOST 是 innodb-cluster01 ,后面proxysql连接出现了一些问题)

复制代码
root@db:3306 16:53:40 [(none)]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST      | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBE|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| group_replication_applier | 12d96b6c-8256-11f1-94a0-000c2951f167 | innodb-cluster01 |        3308 | ONLINE       | SECONDARY   | 8.4.1|
| group_replication_applier | 7c52d66e-8251-11f1-a139-000c2951f167 | innodb-cluster01 |        3306 | ONLINE       | PRIMARY     | 8.4.1|
| group_replication_applier | c57f918e-8253-11f1-8af0-000c2951f167 | innodb-cluster01 |        3307 | ONLINE       | SECONDARY   | 8.4.1|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
3 rows in set (0.00 sec)

root@db:3306 16:53:42 [(none)]> 

-- install ,自动下载的,居然是2.0版本的proxysql,后面出问题了。出现的问题,可以在stats_mysql_query_digest中看到已经路由。但是通过Proxysql登录,无法查询。原因版本问题。

复制代码
sudo yum install epel-release
sudo yum install proxysql

--添加官方 2.6.x 仓库(根据你的系统版本选择):

复制代码
cat <<EOF | sudo tee /etc/yum.repos.d/proxysql.repo
[proxysql]
name=ProxySQL
baseurl=https://repo.proxysql.com/ProxySQL/proxysql-2.6.x/centos/\$releasever
gpgcheck=1
gpgkey=https://repo.proxysql.com/ProxySQL/repo_pub_key
EOF

--查看状态

复制代码
[root@19ctest yum.repos.d]# systemctl start proxysql
[root@19ctest yum.repos.d]# systemctl status proxysql
● proxysql.service - High Performance Advanced Proxy for MySQL
   Loaded: loaded (/usr/lib/systemd/system/proxysql.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2026-07-21 10:28:28 CST; 2s ago
 Main PID: 4680 (proxysql)
    Tasks: 19 (limit: 37320)
   Memory: 13.8M
   CGroup: /system.slice/proxysql.service
           └─4680 /usr/bin/proxysql -f

Jul 21 10:28:28 19ctest proxysql[4680]: | hostgroup_id | hostname | port | gtid_port | weight | status | compression | max_connections |>
Jul 21 10:28:28 19ctest proxysql[4680]: +--------------+----------+------+-----------+--------+--------+-------------+-----------------+>
Jul 21 10:28:28 19ctest proxysql[4680]: 2026-07-21 10:28:28 [INFO] New mysql_group_replication_hostgroups table
Jul 21 10:28:28 19ctest proxysql[4680]: 2026-07-21 10:28:28 [INFO] New mysql_galera_hostgroups table
Jul 21 10:28:28 19ctest proxysql[4680]: 2026-07-21 10:28:28 [INFO] New mysql_aws_aurora_hostgroups table
Jul 21 10:28:28 19ctest proxysql[4680]: 2026-07-21 10:28:28 [INFO] MySQL_HostGroups_Manager::commit() locked for 1us
Jul 21 10:28:28 19ctest proxysql[4680]: Standard Query Processor rev. 2.0.6.0805 -- Query_Processor.cpp -- Wed Nov  6 21:48:56 2019
Jul 21 10:28:28 19ctest proxysql[4680]: In memory Standard Query Cache (SQC) rev. 1.2.0905 -- Query_Cache.cpp -- Wed Nov  6 21:48:56 2019
Jul 21 10:28:28 19ctest proxysql[4680]: Standard MySQL Monitor (StdMyMon) rev. 2.0.1226 -- MySQL_Monitor.cpp -- Wed Nov  6 21:48:56 2019
Jul 21 10:28:28 19ctest proxysql[4680]: 2026-07-21 10:28:28 MySQL_Monitor.cpp:2698:monitor_group_replication(): [ERROR] Group replicatio>
[root@19ctest yum.repos.d]#


[root@19ctest proxysql]# systemctl status proxysql
● proxysql.service - High Performance Advanced Proxy for MySQL
   Loaded: loaded (/etc/systemd/system/proxysql.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2026-07-21 16:18:24 CST; 6min ago
  Process: 8344 ExecStart=/usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf $PROXYSQL_OPTS (code=exited, status=0/SUCCESS)
 Main PID: 8346 (proxysql)
    Tasks: 25 (limit: 37320)
   Memory: 39.9M
   CGroup: /system.slice/proxysql.service
           ├─8346 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf
           └─8347 /usr/bin/proxysql --idle-threads -c /etc/proxysql.cnf

Jul 21 16:18:24 19ctest systemd[1]: Starting High Performance Advanced Proxy for MySQL...
Jul 21 16:18:24 19ctest proxysql[8344]: 2026-07-21 16:18:24 [INFO] Using jemalloc with MALLOC_CONF: config.xmalloc:1, lg_tcache_max:16, >
Jul 21 16:18:24 19ctest proxysql[8344]: 2026-07-21 16:18:24 [INFO] Using config file /etc/proxysql.cnf
Jul 21 16:18:24 19ctest proxysql[8344]: 2026-07-21 16:18:24 [INFO] Current RLIMIT_NOFILE: 102400
Jul 21 16:18:24 19ctest proxysql[8344]: 2026-07-21 16:18:24 [INFO] Using OpenSSL version: OpenSSL 3.3.1 4 Jun 2024
Jul 21 16:18:24 19ctest proxysql[8344]: 2026-07-21 16:18:24 [INFO] No SSL keys/certificates found in datadir (/var/lib/proxysql). Genera>
Jul 21 16:18:24 19ctest systemd[1]: Started High Performance Advanced Proxy for MySQL.
lines 1-18/18 (END)

-- 查看端口,默认是6032和6033 。6032管理端口,6033对外服务端口

复制代码
[root@19ctest yum.repos.d]# netstat -anlp | grep proxysql
tcp        0      0 0.0.0.0:6033            0.0.0.0:*               LISTEN      4680/proxysql       
tcp        0      0 0.0.0.0:6032            0.0.0.0:*               LISTEN      4680/proxysql       
unix  3      [ ]         STREAM     CONNECTED     44396    4680/proxysql        
[root@19ctest yum.repos.d]#

--连接到proxysql

复制代码
[root@19ctest yum.repos.d]# mysql -uadmin -padmin -h 127.0.0.1 -P 6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show database;
ERROR 1045 (28000): ProxySQL Admin Error: near "show": syntax error
MySQL [(none)]> show databases;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.000 sec)

MySQL [(none)]> 

--配置group 复制的主机组

复制代码
INSERT INTO mysql_group_replication_hostgroups 
(writer_hostgroup, backup_writer_hostgroup, reader_hostgroup, offline_hostgroup, active, max_writers, writer_is_also_reader) 
VALUES 
(10, 20, 30, 40, 1, 1, 0);


[root@19ctest yum.repos.d]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select * from mysql_group_replication_hostgroups ;
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+----+
| writer_hostgroup | backup_writer_hostgroup | reader_hostgroup | offline_hostgroup | active | max_writers | writer_is_also_reader | max|
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+----+
| 10               | 20                      | 30               | 40                | 0      | 1           | 0                     | 0  |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+----+
1 row in set (0.001 sec)

MySQL [(none)]> 

--添加后端服务器

复制代码
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '192.168.2.21', 3306);
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '192.168.2.21', 3307);
INSERT INTO mysql_servers (hostgroup_id, hostname, port) VALUES (10, '192.168.2.21', 3308);

Note -- https://proxysql.com/documentation/proxysql-configuration

Set read_only = 0 on primary servers and read_only = 1 on replicas. ProxySQL uses this value for routing decisions.

复制代码
[root@19ctest yum.repos.d]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select * from mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----+
| hostgroup_id | hostname     | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max|
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----+
| 10           | 192.168.2.21 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0  |
| 30           | 192.168.2.21 | 3307 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0  |
| 30           | 192.168.2.21 | 3308 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0  |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----+
3 rows in set (0.001 sec)

MySQL [(none)]> 

--创建监控用户 ,在mgr上建立 。 -- https://proxysql.com/documentation/proxysql-configuration

复制代码
CREATE USER 'monitor'@'%' IDENTIFIED BY 'mysql';
GRANT SUPER, REPLICATION CLIENT, REPLICATION SLAVE ON *.* TO 'monitor'@'%';
FLUSH PRIVILEGES;

alter user 'monitor'@'%' IDENTIFIED WITH mysql_native_password BY 'mysql';  -- 要这样,否则查询出来是下面的
FLUSH PRIVILEGES;

--另外,在官网中明确说明 。https://proxysql.com/documentation/getting-started

If your MySQL client is version 8.4 or later, add --default-auth=mysql_native_password.

-- 后面出现一些问题,查看视图,发现是caching_sha2_password之类的

复制代码
MySQL [(none)]> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 10;
+--------------+------+------------------+-------------------------+--------------------------------------------------------------------+
| hostname     | port | time_start_us    | connect_success_time_us | connect_error                                                      |
+--------------+------+------------------+-------------------------+--------------------------------------------------------------------+
| 192.168.2.21 | 3306 | 1784615925491233 | 0                       | Plugin caching_sha2_password could not be loaded: lib/mariadb/plugi|
| 192.168.2.21 | 3308 | 1784615924744054 | 0                       | Plugin caching_sha2_password could not be loaded: lib/mariadb/plugi|
| 192.168.2.21 | 3307 | 1784615923996854 | 0                       | Plugin caching_sha2_password could not be loaded: lib/mariadb/plugi|
+--------------+------+------------------+-------------------------+--------------------------------------------------------------------+
3 rows in set (0.001 sec)

MySQL [(none)]>

-- 出现SHUNNED,防火墙的问题,关闭掉防火墙后,状态为Online

复制代码
[root@19ctest yum.repos.d]# mysql -uadmin -padmin -h 127.0.0.1 -P 6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> SELECT hostgroup_id, hostname, port, status 
    -> FROM runtime_mysql_servers 
    -> ORDER BY hostgroup_id;
+--------------+--------------+------+---------+
| hostgroup_id | hostname     | port | status  |
+--------------+--------------+------+---------+
| 10           | 192.168.2.21 | 3306 | SHUNNED |
| 10           | 192.168.2.21 | 3307 | SHUNNED |
| 10           | 192.168.2.21 | 3308 | SHUNNED |
+--------------+--------------+------+---------+
3 rows in set (0.003 sec)

MySQL [(none)]> 

MySQL [(none)]> SELECT hostgroup_id, hostname, port, status  FROM runtime_mysql_servers  ORDER BY hostgroup_id;
+--------------+--------------+------+--------+
| hostgroup_id | hostname     | port | status |
+--------------+--------------+------+--------+
| 10           | 192.168.2.21 | 3306 | ONLINE |
| 30           | 192.168.2.21 | 3307 | ONLINE |
| 30           | 192.168.2.21 | 3308 | ONLINE |
+--------------+--------------+------+--------+
3 rows in set (0.001 sec)

MySQL [(none)]> 

--proxy中配置监控账号

复制代码
UPDATE global_variables SET variable_value='monitor' WHERE variable_name='mysql-monitor_username';
UPDATE global_variables SET variable_value='mysql' WHERE variable_name='mysql-monitor_password';
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;

-- 监视后台健康

复制代码
SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 3;
SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 3;


admin@db:6032 09:39:53 [(none)]> SELECT * FROM monitor.mysql_server_connect_log ORDER BY time_start_us DESC LIMIT 3;
+------------------+------+------------------+-------------------------+---------------+
| hostname         | port | time_start_us    | connect_success_time_us | connect_error |
+------------------+------+------------------+-------------------------+---------------+
| 192.168.2.21     | 3306 | 1784770765165169 | 1405                    | NULL          |
| innodb-cluster01 | 3308 | 1784770764854459 | 3293                    | NULL          |
| innodb-cluster01 | 3306 | 1784770764544541 | 2429                    | NULL          |
+------------------+------+------------------+-------------------------+---------------+
3 rows in set (0.001 sec)

admin@db:6032 09:39:56 [(none)]> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 3;
+------------------+------+------------------+----------------------+------------+
| hostname         | port | time_start_us    | ping_success_time_us | ping_error |
+------------------+------+------------------+----------------------+------------+
| 192.168.2.21     | 3307 | 1784770803746959 | 952                  | NULL       |
| innodb-cluster01 | 3306 | 1784770803746865 | 1022                 | NULL       |
| 192.168.2.21     | 3306 | 1784770803746766 | 1098                 | NULL       |
+------------------+------+------------------+----------------------+------------+
3 rows in set (0.001 sec)

admin@db:6032 09:40:11 [(none)]> 

--创建读写分离用户

复制代码
CREATE USER 'apps'@'%' IDENTIFIED BY 'mysql';
GRANT ALL PRIVILEGES ON *.* TO 'apps'@'%';
INSERT INTO mysql_users (username, password, default_hostgroup, transaction_persistent) 
VALUES ('apps', 'mysql', 10, 1);

-- 创建路由规则 -- https://proxysql.com/documentation/proxysql-read-write-split-howto

-- 规则1:SELECT ... FOR UPDATE 路由到主库 (writer_hostgroup=10)

复制代码
INSERT INTO mysql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply) 
VALUES (1, 1, '^SELECT.*FOR UPDATE', 10, 1);

-- 规则2:普通 SELECT 路由到从库 (reader_hostgroup=30)

复制代码
INSERT INTO mysql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply) 
VALUES (2, 1, '^SELECT', 30, 1);

-- 规则3:其他所有语句(INSERT/UPDATE/DELETE)路由到主库 (writer_hostgroup=10)

复制代码
INSERT INTO mysql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply) 
VALUES (3, 1, '^(?!SELECT).*', 10, 1);

[root@19ctest yum.repos.d]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select * from mysql_query_rules ;
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+---------------------+------------+
| rule_id | active | username | schemaname | flagIN | client_addr | proxy_addr | proxy_port | digest | match_digest        | match_patte|
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+---------------------+------------+
| 1       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | ^SELECT.*FOR UPDATE | NULL       |
| 2       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | ^SELECT             | NULL       |
| 3       | 1      | NULL     | NULL       | 0      | NULL        | NULL       | NULL       | NULL   | ^(?!SELECT).*       | NULL       |
+---------+--------+----------+------------+--------+-------------+------------+------------+--------+---------------------+------------+
3 rows in set (0.000 sec)

MySQL [(none)]> 

--持久化

复制代码
LOAD MYSQL SERVERS TO RUNTIME;
SAVE MYSQL SERVERS TO DISK;
LOAD MYSQL USERS TO RUNTIME;
SAVE MYSQL USERS TO DISK;
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;
LOAD MYSQL VARIABLES TO RUNTIME;
SAVE MYSQL VARIABLES TO DISK;

-- 验证功能:

--连接到6033应用端口

复制代码
[root@19ctest yum.repos.d]# mysql -uapps -h127.0.0.1 -p -P6033
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.30 (ProxySQL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> select user,host from mysql.user limit 1;
+------+------+
| user | host |
+------+------+
| apps | %    |
+------+------+
1 row in set (0.003 sec)

MySQL [(none)]> select user,host from mysql.user limit 1;
+------+------+
| user | host |
+------+------+
| apps | %    |
+------+------+
1 row in set (0.001 sec)

MySQL [(none)]> select user,host from mysql.user limit 1;
+------+------+
| user | host |
+------+------+
| apps | %    |
+------+------+
1 row in set (0.005 sec)

MySQL [(none)]> 
MySQL [(none)]> create database test_db ;
Query OK, 1 row affected (0.031 sec)

MySQL [(none)]> 

-- 连接到管理端口6032看效果

复制代码
[root@19ctest yum.repos.d]# mysql -uadmin -padmin -h127.0.0.1 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> SELECT hostgroup, count_star, digest_text 
    -> FROM stats_mysql_query_digest 
    -> ORDER BY count_star DESC;
+-----------+------------+------------------------------------------------------------------------------------------------+
| hostgroup | count_star | digest_text                                                                                    |
+-----------+------------+------------------------------------------------------------------------------------------------+
| 30        | 9          | select ?                                                                                       |
| 10        | 4          | select @@version_comment limit ?                                                               |
| 30        | 3          | select user,host from mysql.user limit ?                                                       |
| 10        | 1          | create database test_db                                                                        |
| 30        | 1          | SELECT * FROM mysql.user                                                                       |
| 30        | 1          | SELECT hostgroup,count_star,digest_text FROM stats_mysql_query_digest ORDER BY count_star DESC |
| 30        | 1          | select * from mysql.user                                                                       |
+-----------+------------+------------------------------------------------------------------------------------------------+
7 rows in set (0.008 sec)

MySQL [(none)]> 

--切换mgr中primary ,把3308切换成primary

复制代码
select group_replication_set_as_primary('12d96b6c-8256-11f1-94a0-000c2951f167');

root@db:3306 16:53:40 [(none)]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST      | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBE|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| group_replication_applier | 12d96b6c-8256-11f1-94a0-000c2951f167 | innodb-cluster01 |        3308 | ONLINE       | SECONDARY   | 8.4.1|
| group_replication_applier | 7c52d66e-8251-11f1-a139-000c2951f167 | innodb-cluster01 |        3306 | ONLINE       | PRIMARY     | 8.4.1|
| group_replication_applier | c57f918e-8253-11f1-8af0-000c2951f167 | innodb-cluster01 |        3307 | ONLINE       | SECONDARY   | 8.4.1|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
3 rows in set (0.00 sec)

root@db:3306 16:53:42 [(none)]> 
root@db:3306 16:57:45 [(none)]> 
root@db:3306 16:57:45 [(none)]> 
root@db:3306 16:57:46 [(none)]> select group_replication_set_as_primary('12d96b6c-8256-11f1-94a0-000c2951f167');
+--------------------------------------------------------------------------+
| group_replication_set_as_primary('12d96b6c-8256-11f1-94a0-000c2951f167') |
+--------------------------------------------------------------------------+
| Primary server switched to: 12d96b6c-8256-11f1-94a0-000c2951f167         |
+--------------------------------------------------------------------------+
1 row in set (0.02 sec)

root@db:3306 16:57:49 [(none)]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST      | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBE|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| group_replication_applier | 12d96b6c-8256-11f1-94a0-000c2951f167 | innodb-cluster01 |        3308 | ONLINE       | PRIMARY     | 8.4.1|
| group_replication_applier | 7c52d66e-8251-11f1-a139-000c2951f167 | innodb-cluster01 |        3306 | ONLINE       | SECONDARY   | 8.4.1|
| group_replication_applier | c57f918e-8253-11f1-8af0-000c2951f167 | innodb-cluster01 |        3307 | ONLINE       | SECONDARY   | 8.4.1|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
3 rows in set (0.00 sec)

root@db:3306 16:57:54 [(none)]> 

--再次验证,6033应用端口

复制代码
mysql -uapps -h127.0.0.1 -p -P6033

DELETE FROM mysql_query_rules WHERE rule_id = 3;
INSERT INTO mysql_query_rules (rule_id, active, match_digest, destination_hostgroup, apply) 
VALUES (3, 1, '^(INSERT|UPDATE|DELETE|CREATE|DROP|ALTER|REPLACE|TRUNCATE|RENAME)', 10, 1);
LOAD MYSQL QUERY RULES TO RUNTIME;
SAVE MYSQL QUERY RULES TO DISK;


begin;
CREATE TABLE IF NOT EXISTS test_rw (
    id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(50) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);


INSERT INTO test_rw (name) VALUES ('proxy'), ('sql'), ('read_write');

commit;


MySQL [(none)]> use test_db;
Database changed
MySQL [test_db]> begin;
Query OK, 0 rows affected (0.001 sec)

MySQL [test_db]> CREATE TABLE IF NOT EXISTS test_rw (
    ->     id INT PRIMARY KEY AUTO_INCREMENT,
    ->     name VARCHAR(50) NOT NULL,
    ->     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    -> );


Query OK, 0 rows affected (0.016 sec)

MySQL [test_db]> 
MySQL [test_db]> 
MySQL [test_db]> INSERT INTO test_rw (name) VALUES ('proxy'), ('sql'), ('read_write');

Query OK, 3 rows affected (0.007 sec)
Records: 3  Duplicates: 0  Warnings: 0

MySQL [test_db]> 
MySQL [test_db]> commit;
Query OK, 0 rows affected (0.000 sec)

MySQL [test_db]> 
MySQL [test_db]> select * from test_db.test_rw;
+----+------------+---------------------+
| id | name       | created_at          |
+----+------------+---------------------+
|  1 | proxy      | 2026-07-22 07:40:22 |
|  2 | sql        | 2026-07-22 07:40:22 |
|  3 | read_write | 2026-07-22 07:40:22 |
+----+------------+---------------------+
3 rows in set (0.007 sec)

MySQL [test_db]> 

--管理端口,看结果

复制代码
SELECT hostgroup, count_star, digest_text FROM stats_mysql_query_digest ORDER BY count_star DESC;

MySQL [(none)]> SELECT hostgroup, count_star, digest_text FROM stats_mysql_query_digest ORDER BY count_star DESC;
+-----------+------------+--------------------------------------------------------------------------------------------------------------+
| hostgroup | count_star | digest_text                                                                                                  |
+-----------+------------+--------------------------------------------------------------------------------------------------------------+
| 30        | 1          | select * from test_db.test_rw                                                                                |
| 10        | 1          | begin                                                                                                        |
| 10        | 1          | show databases                                                                                               |
| 10        | 1          | CREATE TABLE IF NOT EXISTS test_rw (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(?) NOT NULL,created_at TIM|
| 10        | 1          | show tables                                                                                                  |
| 10        | 1          | begin                                                                                                        |
| 10        | 1          | select @@version_comment limit ?                                                                             |
| 10        | 1          | CREATE TABLE IF NOT EXISTS test_rw (id INT PRIMARY KEY AUTO_INCREMENT,name VARCHAR(?) NOT NULL,created_at TIM|
| 10        | 1          | commit                                                                                                       |
| 10        | 1          | commit                                                                                                       |
| 10        | 1          | INSERT INTO test_rw (name) VALUES (?),(?),(?)                                                                |
| 10        | 1          | INSERT INTO test_rw (name) VALUES (?),(?),(?)                                                                |
| 30        | 1          | SELECT DATABASE()                                                                                            |
+-----------+------------+--------------------------------------------------------------------------------------------------------------+
13 rows in set (0.006 sec)

MySQL [(none)]> 

--primary变成3308 ,出现的问题,monitor用户的权限问题,innodb-cluster01 未添加到proxysql所在的hosts中

复制代码
root@db:3306 07:35:28 [(none)]> select group_replication_set_as_primary('12d96b6c-8256-11f1-94a0-000c2951f167');
+--------------------------------------------------------------------------+
| group_replication_set_as_primary('12d96b6c-8256-11f1-94a0-000c2951f167') |
+--------------------------------------------------------------------------+
| Primary server switched to: 12d96b6c-8256-11f1-94a0-000c2951f167         |
+--------------------------------------------------------------------------+
1 row in set (0.02 sec)

root@db:3306 07:44:17 [(none)]> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST      | MEMBER_PORT | MEMBER_STATE | MEMBER_ROLE | MEMBE|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
| group_replication_applier | 12d96b6c-8256-11f1-94a0-000c2951f167 | innodb-cluster01 |        3308 | ONLINE       | PRIMARY     | 8.4.1|
| group_replication_applier | 7c52d66e-8251-11f1-a139-000c2951f167 | innodb-cluster01 |        3306 | ONLINE       | SECONDARY   | 8.4.1|
| group_replication_applier | c57f918e-8253-11f1-8af0-000c2951f167 | innodb-cluster01 |        3307 | ONLINE       | SECONDARY   | 8.4.1|
+---------------------------+--------------------------------------+------------------+-------------+--------------+-------------+------+
3 rows in set (0.00 sec)

root@db:3306 07:44:23 [(none)]> 

INSERT INTO test_rw (name) VALUES ('proxy'), ('sql'), ('read_write');
INSERT INTO test_rw (name) VALUES ('proxy'), ('sql'), ('read_write');
INSERT INTO test_rw (name) VALUES ('proxy'), ('sql'), ('read_write');


MySQL [(none)]>  SELECT hostgroup, count_star, digest_text FROM stats_mysql_query_digest ORDER BY count_star DESC;
+-----------+------------+-----------------------------------------------+
| hostgroup | count_star | digest_text                                   |
+-----------+------------+-----------------------------------------------+
| 10        | 6          | INSERT INTO test_rw (name) VALUES (?),(?),(?) |
| 10        | 3          | INSERT INTO test_rw (name) VALUES (?),(?),(?) |
| 10        | 1          | select @@version_comment limit ?              |
| 30        | 1          | SELECT * FROM `test_rw` WHERE ?=?             |
| 30        | 1          | SELECT DATABASE()                             |
| 10        | 1          | show databases                                |
| 10        | 1          | show tables                                   |
+-----------+------------+-----------------------------------------------+
7 rows in set (0.006 sec)

MySQL [(none)]> 

MySQL [test_db]> select * from test_rw;
+----+------------+---------------------+
| id | name       | created_at          |
+----+------------+---------------------+
|  1 | proxy      | 2026-07-22 07:40:22 |
|  2 | sql        | 2026-07-22 07:40:22 |
|  3 | read_write | 2026-07-22 07:40:22 |
|  4 | proxy      | 2026-07-22 08:30:30 |
|  5 | sql        | 2026-07-22 08:30:30 |
|  6 | read_write | 2026-07-22 08:30:30 |
|  7 | proxy      | 2026-07-22 08:30:30 |
|  8 | sql        | 2026-07-22 08:30:30 |
|  9 | read_write | 2026-07-22 08:30:30 |
| 10 | proxy      | 2026-07-22 08:30:31 |
| 11 | sql        | 2026-07-22 08:30:31 |
| 12 | read_write | 2026-07-22 08:30:31 |
+----+------------+---------------------+
12 rows in set (0.004 sec)

MySQL [test_db]> 

SELECT hostgroup_id, hostname, port, status, weight  FROM mysql_servers;
SELECT hostgroup_id, hostname, port, status, weight FROM runtime_mysql_servers;


MySQL [(none)]> SELECT hostgroup_id, hostname, port, status, weight  FROM mysql_servers;
+--------------+--------------+------+--------+--------+
| hostgroup_id | hostname     | port | status | weight |
+--------------+--------------+------+--------+--------+
| 10           | 192.168.2.21 | 3306 | ONLINE | 1      |
| 30           | 192.168.2.21 | 3307 | ONLINE | 1      |
| 30           | 192.168.2.21 | 3308 | ONLINE | 1      |
+--------------+--------------+------+--------+--------+
3 rows in set (0.001 sec)

MySQL [(none)]> SELECT hostgroup_id, hostname, port, status, weight FROM runtime_mysql_servers;
+--------------+------------------+------+--------+--------+
| hostgroup_id | hostname         | port | status | weight |
+--------------+------------------+------+--------+--------+
| 10           | innodb-cluster01 | 3308 | ONLINE | 1      |
| 20           | 192.168.2.21     | 3308 | ONLINE | 1      |
| 30           | 192.168.2.21     | 3306 | ONLINE | 1      |
| 30           | 192.168.2.21     | 3307 | ONLINE | 1      |
| 30           | innodb-cluster01 | 3306 | ONLINE | 1      |
| 30           | innodb-cluster01 | 3307 | ONLINE | 1      |
+--------------+------------------+------+--------+--------+
6 rows in set (0.001 sec)

MySQL [(none)]> 

-- 开启网页访问

复制代码
admin@db:6032 16:00:04 [(none)]> SET admin-web_enabled = 'true';
Query OK, 1 row affected (0.001 sec)

admin@db:6032 16:01:34 [(none)]> LOAD ADMIN VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.013 sec)

admin@db:6032 16:01:46 [(none)]> SAVE ADMIN VARIABLES TO DISK;
Query OK, 49 rows affected (0.004 sec)

admin@db:6032 16:01:53 [(none)]> 

-- 访问的地址:https://192.168.2.100:6080 用户名stats 密码stats

SELECT * FROM monitor.mysql_server_group_replication_log; -- 排错

END

相关推荐
yagami_gagami3 小时前
第四届黄河流域公安院校电子物证个人赛服务器取证
linux·服务器·网络·mysql·安全·docker·llama
小张同学a.6 小时前
LAMP架构4——MySQL高可用
linux·运维·服务器·数据库·mysql·架构·负载均衡
一条泥憨鱼7 小时前
苍穹外卖【day7| 对购物车进行操作】
java·后端·mysql·苍穹外卖
小码农 - 初11 小时前
MySQL 增删改查
mysql
五阿哥永琪18 小时前
MySQL中操作json的函数!
数据库·mysql·json
来者皆善18 小时前
了解Mysql优化吗?如何优化索引?
数据库·mysql
Ai拆代码的曹操1 天前
MySQL GROUP BY 性能优化:执行计划分析 + 索引重构实战
mysql·性能优化·重构
安_1 天前
MySQL SQL 调优完整指南
数据库·sql·mysql
艾莉丝努力练剑1 天前
【MYSQL】MYSQL学习的一大重点:事务(上)- 原子性与持久性
android·数据库·学习·mysql·面试