利用ProxySQL实现MGR多主模负载均衡

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了利用ProxySQL实现MGR多主模负载均衡相关的知识,希望对你有一定的参考价值。

MGR多主模配置负载均衡

实验环境信息
mysql主从信息

IP 角色
188.188.0.68 Node1
188.188.0.69 Node2
188.188.0.70 Node3

ProxySQL版本: 2.0.3-29-g00f26d5

》》》服务器、用户配置篇《《《

1、添加后端数据库服务器

Admin> SELECT * FROM mysql_servers;
Empty set (0.00 sec)

Admin> INSERT INTO mysql_servers(hostgroup_id,hostname,port,comment) 
    -> VALUES 
    -> (1,‘188.188.0.68‘,3306,‘Node1‘),
    -> (1,‘188.188.0.69‘,3306,‘Node2‘),
    -> (1,‘188.188.0.70‘,3306,‘Node3‘);
Query OK, 3 rows affected (0.00 sec)

Admin> SELECT * FROM mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname     | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 188.188.0.68 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node1   |
| 1            | 188.188.0.69 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node2   |
| 1            | 188.188.0.70 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node3   |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)

2、配置监控

1)在MySQL中创建监控依赖视图

由于对MGR得监控是依赖于sys.gr_member_routing_candidate_status视图得,而该视图并非系统自带,所以需要手动建立。
相关脚本地址:https://github.com/lefred/mysql_gr_routing_check/下得addition_to_sys.sql脚本
登录MGR得主节点,在sys库执行脚本:

# mysql -uroot -p -S /data/database-3306/mysql-3306.sock sys < /root/mysql_gr_routing_check-master/addition_to_sys.sql

查看结果

mysql> show tables from sys like ‘gr%‘;
+------------------------------------+
| Tables_in_sys (gr%)                |
+------------------------------------+
| gr_member_routing_candidate_status |
+------------------------------------+
1 row in set (0.00 sec)

视图已经建立。

2)添加ProXYSQL监视后MYSQL所需得用户(需要在MySQL Server中已创建该用户):

MySQL中创建设置监控用户:

mysql> GRANT select on sys.gr_member_routing_candidate_status TO ‘psql_monitor‘@‘188.188.0.%‘ IDENTIFIED BY ‘987654‘;

mysql> show grants for ‘psql_monitor‘@‘188.188.0.%‘ ;
+--------------------------------------------------------------------------------------------+
| Grants for psql_monitor@188.188.0.%                                                        |
+--------------------------------------------------------------------------------------------+
| GRANT PROCESS, REPLICATION CLIENT ON *.* TO ‘psql_monitor‘@‘188.188.0.%‘                   |
| GRANT SELECT ON `sys`.`gr_member_routing_candidate_status` TO ‘psql_monitor‘@‘188.188.0.%‘ |
+--------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

查看在ProxySQL中配置用户:

Admin> select * from global_variables where variable_name=‘mysql-monitor_username‘;
+------------------------+----------------+
| variable_name          | variable_value |
+------------------------+----------------+
| mysql-monitor_username | monitor        |
+------------------------+----------------+
1 row in set (0.00 sec)

Admin> select * from global_variables where variable_name=‘mysql-monitor_password‘;
+------------------------+----------------+
| variable_name          | variable_value |
+------------------------+----------------+
| mysql-monitor_password | monitor        |
+------------------------+----------------+
1 row in set (0.00 sec)

配置监控用户:

Admin> UPDATE global_variables SET variable_value=‘psql_monitor‘ WHERE variable_name=‘mysql-monitor_username‘;
Query OK, 1 row affected (0.00 sec)

Admin> UPDATE global_variables SET variable_value=‘987654‘ WHERE variable_name=‘mysql-monitor_password‘;
Query OK, 1 row affected (0.01 sec)

Admin> select * from global_variables where variable_name=‘mysql-monitor_username‘;
+------------------------+----------------+
| variable_name          | variable_value |
+------------------------+----------------+
| mysql-monitor_username | psql_monitor   |
+------------------------+----------------+
1 row in set (0.00 sec)

Admin> select * from global_variables where variable_name=‘mysql-monitor_password‘;
+------------------------+----------------+
| variable_name          | variable_value |
+------------------------+----------------+
| mysql-monitor_password | 987654         |
+------------------------+----------------+
1 row in set (0.00 sec)

3)修改监控时间间隔

Admin> SELECT * FROM global_variables WHERE variable_name IN (‘mysql-monitor_connect_interval‘,‘mysql-monitor_ping_interval‘,‘mysql-monitor_read_only_interval‘);
+----------------------------------+----------------+
| variable_name                    | variable_value |
+----------------------------------+----------------+
| mysql-monitor_connect_interval   | 60000          |
| mysql-monitor_ping_interval      | 10000          |
| mysql-monitor_read_only_interval | 1500           |
+----------------------------------+----------------+
3 rows in set (0.00 sec)

Admin> UPDATE global_variables SET variable_value=‘2000‘ WHERE variable_name IN (‘mysql-monitor_connect_interval‘,‘mysql-monitor_ping_interval‘,‘mysql-monitor_read_only_interval‘);
Query OK, 3 rows affected (0.00 sec)

4)配置的生效和持久化

加载配置到RUNTIME层:

Admin> LOAD MYSQL VARIABLES TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

持久化配置到DISK层:

Admin> SAVE MYSQL VARIABLES TO DISK;
Query OK, 110 rows affected (0.02 sec)

3、后端MySQL服务健康检测

1)查看后端连接状态:

Admin> 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 |
+--------------+------+------------------+-------------------------+---------------+
| 188.188.0.70 | 3306 | 1556620581265582 | 387                     | NULL          |
| 188.188.0.69 | 3306 | 1556620581239483 | 413                     | NULL          |
| 188.188.0.68 | 3306 | 1556620581213375 | 432                     | NULL          |
| 188.188.0.70 | 3306 | 1556620579255264 | 381                     | NULL          |
| 188.188.0.68 | 3306 | 1556620579234288 | 426                     | NULL          |
| 188.188.0.69 | 3306 | 1556620579213311 | 492                     | NULL          |
| 188.188.0.70 | 3306 | 1556620577259808 | 443                     | NULL          |
| 188.188.0.68 | 3306 | 1556620577236530 | 437                     | NULL          |
| 188.188.0.69 | 3306 | 1556620577213253 | 500                     | NULL          |
| 188.188.0.70 | 3306 | 1556620575249404 | 468                     | NULL          |
+--------------+------+------------------+-------------------------+---------------+
10 rows in set (0.00 sec)

查看后端存活状态:

Admin> SELECT * FROM monitor.mysql_server_ping_log ORDER BY time_start_us DESC LIMIT 10;
+--------------+------+------------------+----------------------+------------+
| hostname     | port | time_start_us    | ping_success_time_us | ping_error |
+--------------+------+------------------+----------------------+------------+
| 188.188.0.70 | 3306 | 1556620607647791 | 85                   | NULL       |
| 188.188.0.68 | 3306 | 1556620607631491 | 80                   | NULL       |
| 188.188.0.69 | 3306 | 1556620607615190 | 89                   | NULL       |
| 188.188.0.70 | 3306 | 1556620605653805 | 83                   | NULL       |
| 188.188.0.69 | 3306 | 1556620605634458 | 85                   | NULL       |
| 188.188.0.68 | 3306 | 1556620605615106 | 88                   | NULL       |
| 188.188.0.70 | 3306 | 1556620603663711 | 84                   | NULL       |
| 188.188.0.68 | 3306 | 1556620603639370 | 93                   | NULL       |
| 188.188.0.69 | 3306 | 1556620603615027 | 94                   | NULL       |
| 188.188.0.69 | 3306 | 1556620601666891 | 81                   | NULL       |
+--------------+------+------------------+----------------------+------------+
10 rows in set (0.00 sec)

2)配置的生效和持久化

确认后端服务正常后,就可以加载到RUNTIME层进行启用和持久化到DISK层:

Admin> SELECT * FROM runtime_mysql_servers;
Empty set (0.00 sec)

Admin> SELECT * FROM disk.mysql_servers;
Empty set (0.00 sec)

加载配置到RUNTIME层:

Admin> LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.01 sec)

持久化配置到DISK层:

Admin> SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.03 sec)

Admin> SELECT * FROM runtime_mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname     | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 188.188.0.70 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node3   |
| 1            | 188.188.0.69 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node2   |
| 1            | 188.188.0.68 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node1   |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)

Admin> SELECT * FROM disk.mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname     | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 188.188.0.68 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node1   |
| 1            | 188.188.0.69 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node2   |
| 1            | 188.188.0.70 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node3   |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)

4、MySQL多主架构信息配置

1)新增多主配置

Admin> SELECT * FROM mysql_group_replication_hostgroups;
Empty set (0.00 sec

插入读写、只读组配置信息:

Admin> INSERT INTO mysql_group_replication_hostgroups(writer_hostgroup,backup_writer_hostgroup,reader_hostgroup,offline_hostgroup,active,max_writers,comment) VALUES (1,2,4,6,1,3,‘MGR_MM‘);
Query OK, 1 row affected (0.00 sec)

说明:
写组编号为1,其他可写节点所在组为2(因为MGR可多主),只读组为4,下线组为6;
对于上面的服务器,如果想同时提供读、写功能,可以将writer_is_also_reader设置为1 ;

Admin> select * from mysql_group_replication_hostgroups;
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
| writer_hostgroup | backup_writer_hostgroup | reader_hostgroup | offline_hostgroup | active | max_writers | writer_is_also_reader | max_transactions_behind | comment |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
| 1                | 2                       | 4                | 6                 | 1      | 3           | 0                     | 0                       | MGR_MM  |
+------------------+-------------------------+------------------+-------------------+--------+-------------+-----------------------+-------------------------+---------+
1 row in set (0.00 sec)

2)配置生效

将修改的配置加载到RUNTIME层:

Admin> LOAD MYSQL SERVERS TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

查看MySQL分组:

Admin> select * from runtime_mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname     | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 188.188.0.69 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node2   |
| 1            | 188.188.0.70 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node3   |
| 1            | 188.188.0.68 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node1   |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)

可以看到,3个节点都是组1,因为我们设置了max_writers为3,就是要求最多有3个节点提供写服务。

注意:
如果节点数超过了max_writers的设置,那么多余的会被放入组2;若查看时发现组号都是1,且长时间没有变化,也找不出其他错误,那么请在任意一个MySQL节点上执行:

mysql> stop group_replication;

手动制造异常,触发ProxySQL对服务器状态的更新,这时查看runtime_mysql_servers就会识别到了;等识别到后,在再将节点加入即可。

mysql> start group_replication;

3)查看MySQL状态

Admin> SELECT * FROM monitor.mysql_server_group_replication_log ORDER BY time_start_us DESC LIMIT 10;
+--------------+------+------------------+-----------------+------------------+-----------+---------------------+-------+
| hostname     | port | time_start_us    | success_time_us | viable_candidate | read_only | transactions_behind | error |
+--------------+------+------------------+-----------------+------------------+-----------+---------------------+-------+
| 188.188.0.70 | 3306 | 1558246065466522 | 1948            | YES              | NO        | 0                   | NULL  |
| 188.188.0.69 | 3306 | 1558246065466059 | 1907            | YES              | NO        | 0                   | NULL  |
| 188.188.0.68 | 3306 | 1558246065465594 | 1850            | YES              | NO        | 0                   | NULL  |
| 188.188.0.70 | 3306 | 1558246060466448 | 1874            | YES              | NO        | 0                   | NULL  |
| 188.188.0.69 | 3306 | 1558246060465986 | 1896            | YES              | NO        | 0                   | NULL  |
| 188.188.0.68 | 3306 | 1558246060465515 | 1834            | YES              | NO        | 0                   | NULL  |
| 188.188.0.70 | 3306 | 1558246055466430 | 2342            | YES              | NO        | 0                   | NULL  |
| 188.188.0.69 | 3306 | 1558246055465968 | 1927            | YES              | NO        | 0                   | NULL  |
| 188.188.0.68 | 3306 | 1558246055465503 | 1882            | YES              | NO        | 0                   | NULL  |
| 188.188.0.70 | 3306 | 1558246050466312 | 1895            | YES              | NO        | 0                   | NULL  |
+--------------+------+------------------+-----------------+------------------+-----------+---------------------+-------+
10 rows in set (0.00 sec)

3个节点的属性都已识别到,而且没错误信息。

4)对配置存盘

Admin> SAVE MYSQL SERVERS TO DISK;
Query OK, 0 rows affected (0.03 sec)

Admin> SELECT * FROM disk.mysql_servers;
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname     | port | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 1            | 188.188.0.68 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node1   |
| 1            | 188.188.0.69 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node2   |
| 1            | 188.188.0.70 | 3306 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              | Node3   |
+--------------+--------------+------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
3 rows in set (0.00 sec)

5、设置MySQL Users(连接后端使用)

1)在后端MySQL创建业务账号

Admin> SELECT * FROM mysql_users;
Empty set (0.00 sec)

-- 在后端MySQL上创建业务库及读写账号:

mysql> CREATE DATABASE test;
Query OK, 1 row affected (0.01 sec)

mysql> GRANT select,insert,update,delete ON test.* TO ‘test_rw‘@‘188.188.0.%‘ IDENTIFIED BY ‘123456‘; 
Query OK, 0 rows affected, 1 warning (0.00 sec)

2)将业务账号加入ProxySQL

-- 添加后端MySQL上的业务账号到 mysql_users

Admin> INSERT INTO mysql_users(username,password,default_hostgroup,comment) VALUES (‘test_rw‘,‘123456‘,1,‘业务读写账号‘);
Query OK, 1 row affected (0.00 sec)

Admin> SELECT * FROM mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment            |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| test_rw  | 123456   | 1      | 0       | 1                 | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           | 业务读写账号       |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
1 row in set (0.00 sec)

3)配置的生效

再次,将配置加载到运行时以使其生效。

Admin> LOAD MYSQL USERS TO RUNTIME;
Query OK, 0 rows affected (0.00 sec)

Admin> SELECT * FROM runtime_mysql_users;
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment            |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| test_rw  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1      | 0       | 1                 |                | 0             | 1                      | 0            | 0       | 1        | 10000           | 业务读写账号       |
| test_rw  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1      | 0       | 1                 |                | 0             | 1                      | 0            | 1       | 0        | 10000           | 业务读写账号       |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
2 rows in set (0.00 sec)

被添加的每个用户出现了成对的记录,这是因为每个用户在前端和后端都有一条用户记录;

4)加密MEMORY层用户密码

从RUNTIME层获取用户加密后密码,更新MEMORY层:

Admin> SELECT * FROM mysql_users;
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment            |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| test_rw  | 123456   | 1      | 0       | 1                 | NULL           | 0             | 1                      | 0            | 1       | 1        | 10000           | 业务读写账号       |
+----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
1 row in set (0.00 sec)

Admin> SAVE MYSQL USERS FROM RUNTIME;
Query OK, 0 rows affected (0.00 sec)

Admin> SELECT * FROM mysql_users;
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment            |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| test_rw  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1      | 0       | 1                 |                | 0             | 1                      | 0            | 1       | 1        | 10000           | 业务读写账号       |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
1 row in set (0.00 sec)

5)配置持久化

持久化用户信息到DISK层:

Admin> SELECT * FROM disk.mysql_users;
Empty set (0.00 sec)

Admin> SAVE MYSQL USERS TO DISK;
Query OK, 0 rows affected (0.01 sec)

Admin> SELECT * FROM disk.mysql_users;
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| username | password                                  | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | comment            |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
| test_rw  | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | 1      | 0       | 1                 |                | 0             | 1                      | 0            | 1       | 1        | 10000           | 业务读写账号       |
+----------+-------------------------------------------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+--------------------+
1 row in set (0.00 sec)

6、业务账号可用性验证

到此,用户添加完毕,可用使用客户端连接ProxySQL尝试连接。

Admin> select * from stats.stats_mysql_query_digest ;
Empty set (0.01 sec)

ProxySQL中无SQL统计信息;

[root@localhost ~]# mysql -u test_rw -p123456 -h 188.188.0.71 -P6033 -e "SELECT 1"
mysql: [Warning] Using a password on the command line interface can be insecure.
+---+
| 1 |
+---+
| 1 |
+---+

注意:连接端口默认是6033,不是6032(ProxySQL管理端口)也不是3306(MySQL端口)。

[root@localhost ~]# mysql -u test_rw -p123456 -h 188.188.0.71 -P6033 -e "SELECT @@port"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------+
| @@port |
+--------+
|   3306 |
+--------+

这里查询出了后端MySQL服务的端口。

[root@localhost ~]# mysql -u test_rw -p123456 -h 188.188.0.71 -P6033 -e "delete from test.t1;"

[root@localhost ~]# mysql -u test_rw -p123456 -h 188.188.0.71 -P6033 -e "insert into test.t1 values(1,‘a‘);"

[root@localhost ~]# mysql -u test_rw -p123456 -h 188.188.0.71 -P6033 -e "update test.t1 set name=‘b‘ where id=1;"

[root@localhost ~]# mysql -u test_rw -p123456 -h 188.188.0.71 -P6033 -e "SELECT * from test.t1;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+------+
| id | name |
+----+------+
|  1 | b    |
+----+------+

以上增删改查都正常执行了。

Admin> select * from stats.stats_mysql_query_digest ;
+-----------+--------------------+----------+----------------+--------------------+--------------------------------------+------------+------------+------------+----------+----------+----------+
| hostgroup | schemaname         | username | client_address | digest             | digest_text                          | count_star | first_seen | last_seen  | sum_time | min_time | max_time |
+-----------+--------------------+----------+----------------+--------------------+--------------------------------------+------------+------------+------------+----------+----------+----------+
| 1         | information_schema | test_rw  |                | 0x79197FCA4792FA4C | update test.t1 set name=? where id=? | 2          | 1558252176 | 1558252177 | 4774     | 800      | 3974     |
| 1         | information_schema | test_rw  |                | 0x1FAA742A35A78D70 | delete from test.t1                  | 1          | 1558252124 | 1558252124 | 3833     | 3833     | 3833     |
| 1         | information_schema | test_rw  |                | 0x72F235F8DDF4234E | insert into test.t1 values(?,?)      | 3          | 1558251902 | 1558252156 | 14638    | 811      | 10222    |
| 1         | information_schema | test_rw  |                | 0x38B78BFFAF23614A | SELECT * from test.t1                | 14         | 1558251448 | 1558252227 | 3253     | 149      | 318      |
| 1         | information_schema | test_rw  |                | 0x5804011D7FE81F57 | insert into test.t1(name) values(?)  | 4          | 1558251747 | 1558251882 | 8270     | 719      | 3928     |
| 1         | information_schema | test_rw  |                | 0x831B091BA90D80E5 | SELECT @@port                        | 1          | 1558251443 | 1558251443 | 246      | 246      | 246      |
| 1         | information_schema | test_rw  |                | 0x226CD90D52A2BA0B | select @@version_comment limit ?     | 25         | 1558251443 | 1558252227 | 0        | 0        | 0        |
+-----------+--------------------+----------+----------------+--------------------+--------------------------------------+------------+------------+------------+----------+----------+----------+
7 rows in set (0.00 sec)

到此为止,最简单的服务器、用户配置已经完成!!!

此外,还可以为节点服务器指定权重,以此来控制各个服务器的压力。

》》》系统参数配置篇《《《

修改自动提交参数:

Admin> show variables like ‘%auto%‘;
+---------------------------------------+-------+
| Variable_name                         | Value |
+---------------------------------------+-------+
| mysql-forward_autocommit              | false |
| mysql-enforce_autocommit_on_reads     | false |
| mysql-autocommit_false_not_reusable   | false |
| mysql-autocommit_false_is_transaction | false |
| mysql-auto_increment_delay_multiplex  | 5     |
+---------------------------------------+-------+
5 rows in set (0.01 sec)

修改以下2个参数:

1)

Admin> set mysql-forward_autocommit=‘true‘;

为true时,则SET autocommit = 0命令被转发到MySQL上后端。

2)

Admin> set mysql-autocommit_false_not_reusable = ‘true‘;

设置为true时,不会重新使用与autocommit = 0的连接,并在连接返回到连接池时销毁

使配置生效

Admin> save mysql variables to disk;

使配置持久化

Admin> load mysql variables to run;

》》》压测篇《《《

可以对MGR多主压测,但要注意:
由于ProxySQL监控主从状态是靠在后端MySQL上建立一个sys.gr_member_routing_candidate_status的状态视图,而对该视图的查询实际上是对多个系统表查询结果的汇总。因此,在MySQL繁忙时访问该视图会导致监控程序获取信息超时而失败,继而误判主机状态而修改runtime_mysql_servers的状态,导致连接后端MGR线程断开。所以,压测时最好将该视图用一个同名表代替,并在里面写死服务器状态,这样就不会在监控程序访问时获取内容超时异常,也就不会杀死后端连接线程。

对MGR多主模式压测可以参考以下思路:
由于MGR在对大事务支持和事务冲突检测上的限制和不足,导致对MGR直接并行压测是不可能的。Oracle官方的Multi-Primary Mode测试是在每个结点上,对不同的测试库进行压测,即这样可以避免了工具无法并行压测的问题,同时,这样也减少了冲突的可能性。切记,Multi-Primary Mode一定要避免热点数据冲突的场景。

例如MGR集群中有3个节点,分别为A、B、C;那么,压测时需要建立至少3个库;这样每个tpcc压测都使用1个线程来测试一个库,并且同时启动多个tpcc来测试。
可以这样测试:
A节点:

./tpcc_start -h188.188.0.68 -P3306 -uroot -p ‘123456‘ -d tpcc1 -w 5 -c 1 -r 10 -l 30 -i 10 -f tpcc_mysql.log -t tpcc_mysql.rtx
.....

B节点:

./tpcc_start -h188.188.0.69 -P3306 -uroot -p ‘123456‘ -d tpcc2 -w 5 -c 1 -r 10 -l 30 -i 10 -f tpcc_mysql.log -t tpcc_mysql.rtx
.....

C节点:

./tpcc_start -h188.188.0.70 -P3306 -uroot -p ‘123456‘ -d tpcc3 -w 5 -c 1 -r 10 -l 30 -i 10 -f tpcc_mysql.log -t tpcc_mysql.rtx
.....

不过对Multi-Primary Mode压测并不会有一个很好的结果,因为热点太过集中,会导致提交失败很多,或许反而会导致了性能的下降。

如果用sysbench压测,也是一样的思路,只能使用多库+单线程+多测试终端运行的方式。

~
~
完毕!

以上是关于利用ProxySQL实现MGR多主模负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

如何利用MHA+ProxySQL实现读写分离和负载均衡

利用ProxySQL实现MGR单主模式的读写分离

mysql中间件proxysql实现mysql读写分离

MySQL中间件proxysql实现MySQL读写分离

7.利用MySQL Router构建读写分离MGR集群 | 深入浅出MGR

nginx负载均衡+keepalived三主(多主)配置