3、切换从属MGR的主节点回E,在D、E上查看数据与复制状态
在D上操作:
mysql> show slave status for channel 'normal_replication'\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 24801
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 3849
Relay_Log_File: myserver01-relay-bin-normal_replication.000002
Relay_Log_Pos: 389
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3849
Relay_Log_Space: 612
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 595937c0-4d9c-11e8-a819-00163e06ea60
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-26,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab:1-3
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: normal_replication
Master_TLS_Version:
1 row in set (0.02 sec)
mysql> stop group_replication;
Query OK, 0 rows affected (9.36 sec)
mysql> start group_replication;
Query OK, 0 rows affected (4.78 sec)
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 31ab6701-4ec3-11e8-a66b-00163e06ea60 | myserver01 | 24806 | ONLINE |
| group_replication_applier | 9e4308df-4f44-11e8-b364-00163e06ea60 | myserver01 | 24804 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.08 sec)
mysql> show global status like 'group_replication%';
+----------------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------------+--------------------------------------+
| group_replication_primary_member | 31ab6701-4ec3-11e8-a66b-00163e06ea60 |
+----------------------------------+--------------------------------------+
1 row in set (0.11 sec)
mysql> show slave status for channel 'normal_replication'\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 24801
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 3849
Relay_Log_File: myserver01-relay-bin-normal_replication.000002
Relay_Log_Pos: 389
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3849
Relay_Log_Space: 612
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 595937c0-4d9c-11e8-a819-00163e06ea60
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-26,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab:1-4
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: normal_replication
Master_TLS_Version:
1 row in set (0.02 sec)
可以看到切换后,从属MGR主节点D上的复制进程已经被自动停止了。
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
+----+--------+
5 rows in set (0.00 sec)
mysql> insert into test.t1 values(6,'From A');
Query OK, 1 row affected (0.02 sec)
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
+----+--------+
6 rows in set (0.00 sec)
在D、E上操作:
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
+----+--------+
5 rows in set (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status for channel 'normal_replication'\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 24801
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 3849
Relay_Log_File: myserver01-relay-bin-normal_replication.000002
Relay_Log_Pos: 389
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1593
Last_Error: Fatal error: Failed to run 'applier_start' hook
Skip_Counter: 0
Exec_Master_Log_Pos: 3849
Relay_Log_Space: 612
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: Failed to run 'thread_start' hook
Last_SQL_Errno: 1593
Last_SQL_Error: Fatal error: Failed to run 'applier_start' hook
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 595937c0-4d9c-11e8-a819-00163e06ea60
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 180504 11:50:06
Last_SQL_Error_Timestamp: 180504 11:50:06
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-26,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab:1-4
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: normal_replication
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
+----+--------+
5 rows in set (0.00 sec)
数据是过不来的,在D节点上启动slave进程也会因为当前状态为从节点没有启动。验证结果,D、E数据一致,在主MGR的数据无法过来。
在E上操作:
mysql> stop group_replication;
Query OK, 0 rows affected (9.43 sec)
mysql> start group_replication;
Query OK, 0 rows affected (4.92 sec)
mysql> select * from performance_schema.replication_group_members;
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| CHANNEL_NAME | MEMBER_ID | MEMBER_HOST | MEMBER_PORT | MEMBER_STATE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
| group_replication_applier | 31ab6701-4ec3-11e8-a66b-00163e06ea60 | myserver01 | 24806 | ONLINE |
| group_replication_applier | 9e4308df-4f44-11e8-b364-00163e06ea60 | myserver01 | 24804 | ONLINE |
+---------------------------+--------------------------------------+-------------+-------------+--------------+
2 rows in set (0.00 sec)
mysql> show global status like 'group_replication%';
+----------------------------------+--------------------------------------+
| Variable_name | Value |
+----------------------------------+--------------------------------------+
| group_replication_primary_member | 9e4308df-4f44-11e8-b364-00163e06ea60 |
+----------------------------------+--------------------------------------+
1 row in set (0.01 sec)
把从属MGR的主节点切换回D。
在D上操作:
mysql> show slave status for channel 'normal_replication'\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 24801
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 3849
Relay_Log_File: myserver01-relay-bin-normal_replication.000002
Relay_Log_Pos: 389
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1593
Last_Error: Fatal error: Failed to run 'applier_start' hook
Skip_Counter: 0
Exec_Master_Log_Pos: 3849
Relay_Log_Space: 612
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: Failed to run 'thread_start' hook
Last_SQL_Errno: 1593
Last_SQL_Error: Fatal error: Failed to run 'applier_start' hook
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 595937c0-4d9c-11e8-a819-00163e06ea60
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 180504 11:50:06
Last_SQL_Error_Timestamp: 180504 11:50:06
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-26,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab:1-5
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: normal_replication
Master_TLS_Version:
1 row in set (0.02 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)
mysql> show slave status for channel 'normal_replication'\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 24801
Connect_Retry: 60
Master_Log_File: binlog.000004
Read_Master_Log_Pos: 4093
Relay_Log_File: myserver01-relay-bin-normal_replication.000003
Relay_Log_Pos: 633
Relay_Master_Log_File: binlog.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 4093
Relay_Log_Space: 1095
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 595937c0-4d9c-11e8-a819-00163e06ea60
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:27
Executed_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-27,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab:1-5
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: normal_replication
Master_TLS_Version:
1 row in set (0.00 sec)
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
+----+--------+
6 rows in set (0.00 sec)
当从属MGR主节点切换回D后,启动slave进程,数据开始恢复同步。
最后,我们附加测试一下两个MGR之间的双向复制,用于两个机房做机房级别的故障切换。如果方案可行,可做为机房双活方案,需要加上DNS的切换,平时DNS使用主机房VIP,故障切换时,使用备用机房VIP,重启应用。
测试方案:mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
+----+--------+
6 rows in set (0.02 sec)
mysql> insert into test.t1 values(7,'From A');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
| 7 | From A |
+----+--------+
7 rows in set (0.00 sec)
在D上操作:
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
| 7 | From A |
+----+--------+
7 rows in set (0.00 sec)
mysql> insert into test.t1 values(8,'From D');
Query OK, 1 row affected (0.01 sec)
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
| 7 | From A |
| 8 | From D |
+----+--------+
8 rows in set (0.00 sec)
在A上操作:
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
| 7 | From A |
| 8 | From D |
+----+--------+
8 rows in set (0.00 sec)
验证双向复制是可行的。
在D上操作:
mysql> show global 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.00 sec)
mysql> set global read_only=on;
Query OK, 0 rows affected (0.00 sec)
mysql> set global super_read_only=on;
Query OK, 0 rows affected (0.00 sec)
mysql> show global variables like '%read_only';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_read_only | OFF |
| read_only | ON |
| super_read_only | ON |
| transaction_read_only | OFF |
| tx_read_only | OFF |
+-----------------------+-------+
5 rows in set (0.00 sec)
mysql> insert into test.t1 values(9,'From D');
ERROR 1290 (HY000): The MySQL server is running with the --super-read-only option so it cannot execute this statement
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: repl
Master_Port: 24801
Connect_Retry: 60
Master_Log_File: binlog.000005
Read_Master_Log_Pos: 3096
Relay_Log_File: myserver01-relay-bin-normal_replication.000004
Relay_Log_Pos: 1476
Relay_Master_Log_File: binlog.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 3096
Relay_Log_Space: 2182
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 595937c0-4d9c-11e8-a819-00163e06ea60
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:27-30
Executed_Gtid_Set: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa:1-30,
aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaab:1-6
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name: normal_replication
Master_TLS_Version:
1 row in set (0.00 sec)
在A上操作:
mysql> insert into test.t1 values(9,'From A');
Query OK, 1 row affected (0.31 sec)
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
| 7 | From A |
| 8 | From D |
| 9 | From A |
+----+--------+
9 rows in set (0.19 sec)
在D上操作:
mysql> select * from test.t1;
+----+--------+
| c1 | c2 |
+----+--------+
| 1 | Luis |
| 2 | from A |
| 3 | From A |
| 4 | From B |
| 5 | From A |
| 6 | From A |
| 7 | From A |
| 8 | From D |
| 9 | From A |
+----+--------+
9 rows in set (0.12 sec)
验证结果是可以在从属MGR上设置super_read_only,在不影响复制的情况下拒绝错误的写请求。