MySQL Group Replication mgr 单主 proxysql 读写分离配置过程

1、前期准备,mgr安装见上一篇文章

2、创建用户和导入脚本

GRANT ALL ON *.* TO ‘rootuser‘@‘%‘ IDENTIFIED BY ‘123456‘;
    /mgr/mysql/bin/mysql -h127.0.0.1 -P24802 <a.sql
[[email protected] ~]# cat a.sql 

USE sys;

DELIMITER $$

CREATE FUNCTION IFZERO(a INT, b INT)
RETURNS INT
DETERMINISTIC
RETURN IF(a = 0, b, a)$$

CREATE FUNCTION LOCATE2(needle TEXT(10000), haystack TEXT(10000), offset INT)
RETURNS INT
DETERMINISTIC
RETURN IFZERO(LOCATE(needle, haystack, offset), LENGTH(haystack) + 1)$$

CREATE FUNCTION GTID_NORMALIZE(g TEXT(10000))
RETURNS TEXT(10000)
DETERMINISTIC
RETURN GTID_SUBTRACT(g, ‘‘)$$

CREATE FUNCTION GTID_COUNT(gtid_set TEXT(10000))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE result BIGINT DEFAULT 0;
DECLARE colon_pos INT;
DECLARE next_dash_pos INT;
DECLARE next_colon_pos INT;
DECLARE next_comma_pos INT;
SET gtid_set = GTID_NORMALIZE(gtid_set);
SET colon_pos = LOCATE2(‘:‘, gtid_set, 1);
WHILE colon_pos != LENGTH(gtid_set) + 1 DO
    SET next_dash_pos = LOCATE2(‘-‘, gtid_set, colon_pos + 1);
    SET next_colon_pos = LOCATE2(‘:‘, gtid_set, colon_pos + 1);
    SET next_comma_pos = LOCATE2(‘,‘, gtid_set, colon_pos + 1);
    IF next_dash_pos < next_colon_pos AND next_dash_pos < next_comma_pos THEN
    SET result = result +
        SUBSTR(gtid_set, next_dash_pos + 1,
                LEAST(next_colon_pos, next_comma_pos) - (next_dash_pos + 1)) -
        SUBSTR(gtid_set, colon_pos + 1, next_dash_pos - (colon_pos + 1)) + 1;
    ELSE
    SET result = result + 1;
    END IF;
    SET colon_pos = next_colon_pos;
END WHILE;
RETURN result;
END$$

CREATE FUNCTION gr_applier_queue_length()
RETURNS INT
DETERMINISTIC
BEGIN
RETURN (SELECT sys.gtid_count( GTID_SUBTRACT( (SELECT
Received_transaction_set FROM performance_schema.replication_connection_status
WHERE Channel_name = ‘group_replication_applier‘ ), (SELECT
@@global.GTID_EXECUTED) )));
END$$

CREATE FUNCTION gr_member_in_primary_partition()
RETURNS VARCHAR(3)
DETERMINISTIC
BEGIN
RETURN (SELECT IF( MEMBER_STATE=‘ONLINE‘ AND ((SELECT COUNT(*) FROM
performance_schema.replication_group_members WHERE MEMBER_STATE != ‘ONLINE‘) >=
((SELECT COUNT(*) FROM performance_schema.replication_group_members)/2) = 0),
‘YES‘, ‘NO‘ ) FROM performance_schema.replication_group_members JOIN
performance_schema.replication_group_member_stats USING(member_id));
END$$

CREATE VIEW gr_member_routing_candidate_status AS SELECT
sys.gr_member_in_primary_partition() as viable_candidate,
IF( (SELECT (SELECT GROUP_CONCAT(variable_value) FROM
performance_schema.global_variables WHERE variable_name IN (‘read_only‘,
‘super_read_only‘)) != ‘OFF,OFF‘), ‘YES‘, ‘NO‘) as read_only,
sys.gr_applier_queue_length() as transactions_behind, Count_Transactions_in_queue as ‘transactions_to_cert‘ from performance_schema.replication_group_member_stats;$$

DELIMITER ;
[[email protected] ~]# sz a.sql 

3、mgr现有结构及其主节点信息

[[email protected] proxysql]# /mgr/mysql/bin/mysql -h127.0.0.1 -P24802
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 192
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 5c7975ec-a9cd-11e9-a8c9-0800273906ff | mgr1        |       24801 | ONLINE       |
| group_replication_applier | 69908c35-a9cd-11e9-8b78-0800273906ff | mgr1        |       24802 | ONLINE       |
| group_replication_applier | 79ca1c48-a9cd-11e9-9526-0800273906ff | mgr1        |       24803 | ONLINE       |
| group_replication_applier | e9ef573e-a9dc-11e9-8003-0800273906ff | mgr1        |       24804 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
4 rows in set (0.00 sec)

mysql> show variables like ‘%read_only%‘;
+-----------------------+-------+
| Variable_name         | Value |
+-----------------------+-------+
| innodb_read_only      | OFF   |
| read_only             | OFF   |
| super_read_only       | OFF   |
| transaction_read_only | OFF   |
| tx_read_only          | OFF   |
+-----------------------+-------+
5 rows in set (0.01 sec)

mysql> SELECT @@server_id;
+-------------+
| @@server_id |
+-------------+
|           2 |
+-------------+
1 row in set (0.00 sec)

mysql> exit
Bye

4、proxysql启动并配置

[[email protected] proxysql]# /etc/init.d/proxysql start
Starting ProxySQL: 2019-07-19 03:48:26 [INFO] Using config file /etc/proxysql.cnf
2019-07-19 03:48:26 [INFO] No SSL keys/certificates found in datadir (/var/lib/proxysql). Generating new keys/certificates.
DONE!
[[email protected] proxysql]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
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, 2019, Oracle and/or its affiliates. All rights reserved.

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.

5、添加监控用户和后端连接用户

mysql> SET mysql-monitor_username=‘rootuser‘;
Query OK, 1 row affected (0.00 sec)

mysql> SET mysql-monitor_password=‘123456‘;
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (‘rootuser‘,‘123456‘,10);
Query OK, 1 row affected (0.00 sec)

6、配置默认组信息,组ID含义如下写组:10备写组:20读组:30离线组(不可用):40

mysql> insert into mysql_group_replication_hostgroups(writer_hostgroup,backup_writer_hostgroup,reader_hostgroup,offline_hostgroup,active) values(10,20,30,40,1);
Query OK, 1 row affected (0.01 sec)

7、添加服务器地址

mysql> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24801);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24802);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24803);
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24804);
Query OK, 1 row affected (0.00 sec)

8、添加路由规则并保持

mysql> INSERT INTO mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) VALUES (1,1,‘^SELECT.*FOR UPDATE$‘,10,1),(2,1,‘^SELECT‘,30,1);
Query OK, 2 rows affected (0.01 sec)

mysql> save mysql users to disk;save mysql servers to disk;save mysql query rules to disk;save mysql variables to disk;save admin variables to disk;load mysql users to runtime;load mysql servers to runtime;load mysql query rules to runtime;load mysql variables to runtime;load admin variables to runtime;
Query OK, 0 rows affected (0.06 sec)

Query OK, 0 rows affected (0.19 sec)

Query OK, 0 rows affected (0.04 sec)

Query OK, 116 rows affected (0.02 sec)

Query OK, 32 rows affected (0.08 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

9、查看服务器配置和运行时服务器配置

mysql> select * from mysql_servers;
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port  | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)

mysql> 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 |
+--------------+-----------+-------+-----------+--------------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24802 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24801 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 24803 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 127.0.0.1 | 24803 | 0         | OFFLINE_HARD | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
5 rows in set (0.04 sec)

mysql> select * from scheduler;
Empty set (0.00 sec)

mysql> exit
Bye

10、关闭当前主服务mysql测试

[[email protected] proxysql]# /mgr/mysql/bin/mysql -h127.0.0.1 -P24802
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 201
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> shutdown;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[[email protected] proxysql]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 runtime_mysql_servers;
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port  | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)

mysql> exit
Bye

11、重新开启原来的主服务器

[[email protected] proxysql]# /mgr/mysql/bin/mysqld --defaults-file=/mgr/data/s2/s2.cnf &
[[email protected] proxysql]# /mgr/mysql/bin/mysql -h127.0.0.1 -P24802
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 performance_schema.replication_group_members;
+---------------------------+-----------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+-----------+-------------+-------------+--------------+
| group_replication_applier |           |             |        NULL | OFFLINE      |
+---------------------------+-----------+-------------+-------------+--------------+
1 row in set (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (3.36 sec)

mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 5c7975ec-a9cd-11e9-a8c9-0800273906ff | mgr1        |       24801 | ONLINE       |
| group_replication_applier | 69908c35-a9cd-11e9-8b78-0800273906ff | mgr1        |       24802 | ONLINE       |
| group_replication_applier | 79ca1c48-a9cd-11e9-9526-0800273906ff | mgr1        |       24803 | ONLINE       |
| group_replication_applier | e9ef573e-a9dc-11e9-8003-0800273906ff | mgr1        |       24804 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[[email protected] proxysql]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 runtime_mysql_servers;
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port  | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)

mysql> 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 |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.01 sec)

mysql> exit
Bye
[[email protected] proxysql]# cd
[[email protected] ~]# cat test.sh 

for i in {1..100}
do
/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13  -P6033 -e "SELECT @@server_id;"
#/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13 -P6033 -e "BEGIN;SELECT @@server_id;commit;"
done
[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 2 |wc -l
61
[[email protected] ~]# cat test.txt |grep 3 |wc -l
0
[[email protected] ~]# cat test.txt |grep 1 |wc -l
0
[[email protected] ~]# cat test.txt |grep 4 |wc -l
39

12、重新保持一下观察

[[email protected] ~]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 104
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> save mysql users to disk;save mysql servers to disk;save mysql query rules to disk;save mysql variables to disk;save admin variables to disk;load mysql users to runtime;load mysql servers to runtime;load mysql query rules to runtime;load mysql variables to runtime;load admin variables to runtime;
Query OK, 0 rows affected (0.30 sec)

Query OK, 0 rows affected (0.12 sec)

Query OK, 0 rows affected (0.04 sec)

Query OK, 116 rows affected (0.01 sec)

Query OK, 32 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> save mysql users to disk;save mysql servers to disk;save mysql query rules to disk;save mysql variables to disk;save admin variables to disk;load mysql users to runtime;load mysql servers tomysql> exit                                                                                                                                                                                          mysql> 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 |
+--------------+-----------+-------+-----------+--------------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24803 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24802 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE       | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 20           | 127.0.0.1 | 24802 | 0         | OFFLINE_HARD | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 10           | 127.0.0.1 | 24804 | 0         | OFFLINE_HARD | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
6 rows in set (0.01 sec)

mysql> 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 |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 1 |wc -l
0
[[email protected] ~]# cat test.txt |grep 2 |wc -l
30
[[email protected] ~]# cat test.txt |grep 3 |wc -l
34
[[email protected] ~]# cat test.txt |grep 4 |wc -l
36
[[email protected] ~]# vim test.sh
-bash: vim: command not found
[[email protected] ~]# vi test.sh
for i in {1..100}
do
/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13  -P6033 -e "SELECT @@server_id;"
#/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13 -P6033 -e "BEGIN;SELECT @@server_id;commit;"
done

for i in {1..100}
do
#/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13  -P6033 -e "SELECT @@server_id;"
"test.sh" 6L, 224C written
[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 1 |wc -l
100
[[email protected] ~]# cat test.txt |grep 2 |wc -l
0
[[email protected] ~]# cat test.txt |grep 3 |wc -l
0
[[email protected] ~]# cat test.txt |grep 4 |wc -l
0
[[email protected] ~]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 305
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> save mysql users to disk;save mysql servers to disk;save mysql query rules to disk;save mysql variables to disk;save admin variables to disk;load mysql users to runtime;load mysql servers tomysql> select * from runtime_mysql_servers;                                                                                                                                                          mysql> 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 |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)

mysql> exit
Bye

13、重启当前主服务mysql观察

[[email protected] ~]# /mgr/mysql/bin/mysql -h127.0.0.1 -P24801
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 140
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> shutdown;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[[email protected] ~]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 306
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 runtime_mysql_servers;
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port  | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 40           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 1 |wc -l
0
[[email protected] ~]# cat test.txt |grep 2 |wc -l
100
[[email protected] ~]# cat test.txt |grep 3 |wc -l
0
[[email protected] ~]# cat test.txt |grep 4 |wc -l
0
[[email protected] ~]# vi test.sh
for i in {1..100}
do
#/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13  -P6033 -e "SELECT @@server_id;"
/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13 -P6033 -e "BEGIN;SELECT @@server_id;commit;"
done

for i in {1..100}
do
/mgr/mysql/bin/mysql -urootuser -p123456 -h192.168.56.13  -P6033 -e "SELECT @@server_id;"
"test.sh" 6L, 224C written
[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 1 |wc -l
0
[[email protected] ~]# cat test.txt |grep 2 |wc -l
0
[[email protected] ~]# cat test.txt |grep 3 |wc -l
56
[[email protected] ~]# cat test.txt |grep 4 |wc -l
44
[[email protected] ~]# /mgr/mysql/bin/mysqld --defaults-file=/mgr/data/s1/s1.cnf &

[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 1 |wc -l
0
[[email protected] ~]# cat test.txt |grep 2 |wc -l
0
[[email protected] ~]# cat test.txt |grep 3 |wc -l
51
[[email protected] ~]# cat test.txt |grep 4 |wc -l
49
[[email protected] ~]# /mgr/mysql/bin/mysql -h127.0.0.1 -P24801
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 performance_schema.replication_group_members;
+---------------------------+-----------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+-----------+-------------+-------------+--------------+
| group_replication_applier |           |             |        NULL | OFFLINE      |
+---------------------------+-----------+-------------+-------------+--------------+
1 row in set (0.00 sec)

mysql> START GROUP_REPLICATION;
Query OK, 0 rows affected (3.27 sec)

mysql> exit
Bye
[[email protected] ~]# /mgr/mysql/bin/mysql -h127.0.0.1 -P24801
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.25-log MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME              | MEMBER_ID                            | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 5c7975ec-a9cd-11e9-a8c9-0800273906ff | mgr1        |       24801 | ONLINE       |
| group_replication_applier | 69908c35-a9cd-11e9-8b78-0800273906ff | mgr1        |       24802 | ONLINE       |
| group_replication_applier | 79ca1c48-a9cd-11e9-9526-0800273906ff | mgr1        |       24803 | ONLINE       |
| group_replication_applier | e9ef573e-a9dc-11e9-8003-0800273906ff | mgr1        |       24804 | ONLINE       |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
4 rows in set (0.00 sec)

mysql> exit
Bye
[[email protected] ~]# sh test.sh >test.txt
[[email protected] ~]# cat test.txt |grep 1 |wc -l
28
[[email protected] ~]# cat test.txt |grep 2 |wc -l
0
[[email protected] ~]# cat test.txt |grep 3 |wc -l
28
[[email protected] ~]# cat test.txt |grep 4 |wc -l
44
[[email protected] ~]# /mgr/mysql/bin/mysql  -u admin -padmin -h 127.0.0.1 -P6032
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 707
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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 runtime_mysql_servers;
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| hostgroup_id | hostname  | port  | gtid_port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
| 10           | 127.0.0.1 | 24802 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24801 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24803 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
| 30           | 127.0.0.1 | 24804 | 0         | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |
+--------------+-----------+-------+-----------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+
4 rows in set (0.01 sec)

mysql> exit
Bye

14、关键命令备份

SET mysql-monitor_username=‘rootuser‘;
SET mysql-monitor_password=‘123456‘;
INSERT INTO mysql_users(username,password,default_hostgroup) VALUES (‘rootuser‘,‘123456‘,10);
insert into mysql_group_replication_hostgroups(writer_hostgroup,backup_writer_hostgroup,reader_hostgroup,offline_hostgroup,active) values(10,20,30,40,1);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24801);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24802);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24803);
INSERT INTO mysql_servers(hostgroup_id,hostname,port) VALUES (10,‘127.0.0.1‘,24804);
INSERT INTO mysql_query_rules(rule_id,active,match_digest,destination_hostgroup,apply) VALUES (1,1,‘^SELECT.*FOR UPDATE$‘,10,1),(2,1,‘^SELECT‘,30,1);
save mysql users to disk;save mysql servers to disk;save mysql query rules to disk;save mysql variables to disk;save admin variables to disk;load mysql users to runtime;load mysql servers to runtime;load mysql query rules to runtime;load mysql variables to runtime;load admin variables to runtime;

原文地址:https://blog.51cto.com/860143/2421791

时间: 2024-10-09 15:54:04

MySQL Group Replication mgr 单主 proxysql 读写分离配置过程的相关文章

MySQL 5.7 MGR单主确定主节点方法

我们行MGR年底要上线了,每天都要看官方文档学习,做测试,坚持每天写一个小知识点,有想一起学习的么~ MySQL 5.7 MGR单主确定主节点是哪个,我们可以通过成员ID来判断,然后结合read_only参数来确认. [[email protected] ~]# mysql -uroot -p -P 3306 -h 127.0.0.1 mysql: [Warning] Using a password on the command line interface can be insecure.

MySQL Group Replication mgr 安装关键过程

1.初始化和启动 mysql/bin/mysqld --initialize-insecure --basedir=/mgr/mysql --datadir=/mgr/data/s1 mysql/bin/mysqld --initialize-insecure --basedir=/mgr/mysql --datadir=/mgr/data/s2 mysql/bin/mysqld --initialize-insecure --basedir=/mgr/mysql --datadir=/mgr/

proxysql 主从复制读写分离配置过程记录

1.环境信息 软件GitHub地址: https://github.com/sysown/proxysql/ 软件官网:https://proxysql.com/ 系统版本: [[email protected] proxysql]# cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core) 主从环境dockers +---------------+------+-----------+ | hostname | port | st

MySQL Group Replication(多主同步复制MGR)

开启replication配置: server-id=1                                      #标识服务器唯一 log-bin=mysql-bin                           #二进制日志开启 enforce_gtid_consistency = ON #GTID模式是组复制的基础技术 binlog-format=row                           #必须是ROW模式 gtid-mode=ON         

Mysql Group Replication 简介及单主模式组复制配置【转】

一 Mysql Group Replication简介 Mysql Group Replication(MGR)是一个全新的高可用和高扩张的MySQL集群服务. 高一致性,基于原生复制及paxos协议的组复制技术,以插件方式提供一致数据安全保证: 高容错性,大多数服务正常就可继续工作,自动不同节点检测资源征用冲突,按顺序优先处理,内置自动防脑裂机制: 高扩展性,自动添加移除节点,并更新组信息: 高灵活性,单主模式和多主模式.单主模式自动选主,所有更新操作在主进行:多主模式,所有server同时更

Percona XtraDB Cluster vs Galera Cluster vs MySQL Group Replication

Overview Galera Cluster Percona XtraDB Cluster MySQL Group Replication MySQL InnoDB Cluster Similarities Similar - Use Cases Similar Limitation Differences GR & Galera Group Communication System 组通信系统 Binlogs & Gcache Node Provisioning 节点配置 GTID v

MySQL group replication介绍

"MySQL group replication" group replication是MySQL官方开发的一个开源插件,是实现MySQL高可用集群的一个工具.第一个GA版本正式发布于MySQL5.7.17中:想要使用group replication只需要从官网上下载MySQL5.7.17及以后的版本即可 group replication发布以后,有3种方法来实现MySQL的高可用集群: ①:异步复制 ②:半同步复制 ③:group replication ---注意: 异步复制是

[MGR——Mysql的组复制之单主模式 ]详细搭建部署过程

1,关于MySQL Group Replication 基于组的复制(Group-basedReplication)是一种被使用在容错系统中的技术.Replication-group(复制组)是由能够相互通信的多个服务器(节点)组成的. 在通信层,Groupreplication实现了一系列的机制:比如原子消息(atomicmessage delivery)和全序化消息(totalorderingof messages). 这些原子化,抽象化的机制,为实现更先进的数据库复制方案提供了强有力的支持

MySQL Group Replication调研剖析

一.MySQL复制的三种模式 MySQL当前存在的三种复制模式有:异步模式.半同步模式和组复制模式,先了解一下三种模式的工作方式. 1.MySQL Asynchronous Replication(异步复制) 异步复制是MySQL最早的也是当前使用最多的复制模式,异步复制提供了一种简单的主-从复制方法,包含一个主库(master)和备库(一个,或者多个)之间,主库执行并提交了事务,在这之后(因此才称之为异步),这些事务才在从库上重新执行一遍(基于statement)或者变更数据内容(基于row)