一、服务器环境
MySQL5.7 MGR集群切换
目标:故障切换,启用备选主之后。还原MGR,切换回原主
服务器信息
[root@0321-mysqlmgr3 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.82.11.22 0321-mysqlmgr1.test.com 0321-mysqlmgr1
10.82.11.23 0321-mysqlmgr2.test.com 0321-mysqlmgr2
10.82.11.24 0321-mysqlmgr3.test.com 0321-mysqlmgr3
mysql> select @@version;
+------------+
| @@version |
+------------+
| 5.7.26-log |
+------------+
1 row in set (0.00 sec)
二、MGR发生切换,备选主提升为主库
此时mysqlmgr3为主库,读写,其他节点只读
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| group_replication_applier | 8e12773f-c7a9-11e9-a014-005056bf2bfd | 0321-mysqlmgr3 | 3306 | ONLINE |
| group_replication_applier | 94c4ba24-c7a9-11e9-87a0-005056bf300d | 0321-mysqlmgr2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
2 rows in set (0.00 sec)
mysql> select variable_value from performance_schema.global_status where variable_name='group_replication_primary_member';
+--------------------------------------+
| variable_value |
+--------------------------------------+
| 8e12773f-c7a9-11e9-a014-005056bf2bfd |
+--------------------------------------+
1 row in set (0.00 sec)
原主库状态:
[root@0321-mysqlmgr1 ~]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
[root@0321-mysqlmgr1 ~]# ps -ef |grep mysql
root 6947 4865 0 09:13 pts/1 00:00:00 grep --color=auto mysql
原主库mysqlmgr1起库,启动GR
[root@0321-mysqlmgr1 ~]#/usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql_3306/my.cnf --user=mysql &
mysql> start group_replication;
此时的MGR架构:
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| group_replication_applier | 23fb82d9-c3bf-11e9-a56e-005056bf4fbc | 0321-mysqlmgr1 | 3306 | ONLINE |
| group_replication_applier | 8e12773f-c7a9-11e9-a014-005056bf2bfd | 0321-mysqlmgr3 | 3306 | ONLINE |
| group_replication_applier | 94c4ba24-c7a9-11e9-87a0-005056bf300d | 0321-mysqlmgr2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
3 rows in set (0.00 sec)
此时mysqlmgr3为主库
mysql> select variable_value from performance_schema.global_status where variable_name='group_replication_primary_member';
+--------------------------------------+
| variable_value |
+--------------------------------------+
| 8e12773f-c7a9-11e9-a014-005056bf2bfd |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> system ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:bf:2b:fd brd ff:ff:ff:ff:ff:ff
inet 10.82.11.24/24 brd 10.82.11.255 scope global ens192
valid_lft forever preferred_lft forever
inet 10.82.11.25/32 scope global ens192
valid_lft forever preferred_lft forever
mysql> system hostname
0321-mysqlmgr3
VIP绑定在mysqlmgr3
三、原主库mysqlmgr1,设置权重,切换MGR为旧的主库
mysql> select @@group_replication_member_weight;
+-----------------------------------+
| @@group_replication_member_weight |
+-----------------------------------+
| 50 |
+-----------------------------------+
1 row in set (0.00 sec)
mysql> set global group_replication_member_weight=100;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@group_replication_member_weight;
+-----------------------------------+
| @@group_replication_member_weight |
+-----------------------------------+
| 100 |
+-----------------------------------+
1 row in set (0.00 sec)
开启mysqlmgr1的keepalive -->重启mysqlmgr3的MySQL实例,启动keepalive--> 重置mysqlmgr1的group_replication_member_weight=50
--> mysqlmgr3 mysql> set global group_replication_allow_local_disjoint_gtids_join=ON; start group_replication;
主库切换回原主,
mysql> SELECT * FROM performance_schema.replication_group_members;
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
| group_replication_applier | 23fb82d9-c3bf-11e9-a56e-005056bf4fbc | 0321-mysqlmgr1 | 3306 | ONLINE |
| group_replication_applier | 8e12773f-c7a9-11e9-a014-005056bf2bfd | 0321-mysqlmgr3 | 3306 | ONLINE |
| group_replication_applier | 94c4ba24-c7a9-11e9-87a0-005056bf300d | 0321-mysqlmgr2 | 3306 | ONLINE |
+---------------------------+--------------------------------------+----------------+-------------+--------------+
3 rows in set (0.00 sec)
mysql> select variable_value from performance_schema.global_status where variable_name='group_replication_primary_member';
+--------------------------------------+
| variable_value |
+--------------------------------------+
| 23fb82d9-c3bf-11e9-a56e-005056bf4fbc |
+--------------------------------------+
1 row in set (0.00 sec)
VIP绑定在原主的host
[root@0321-mysqlmgr1 keepalived]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
link/ether 00:50:56:bf:4f:bc brd ff:ff:ff:ff:ff:ff
inet 10.82.11.22/24 brd 10.82.11.255 scope global ens192
valid_lft forever preferred_lft forever
inet 10.82.11.25/32 scope global ens192
valid_lft forever preferred_lft forever