GTID 模式切换流程

step1

在 所有机器上 依次执行如下命令(每执行一条需确认没有报错才能进行下一步),不区分主从顺序

1
2
3
4
5
set global enforce_gtid_consistency=warn;
set global enforce_gtid_consistency=on;
set global gtid_mode=off_permissive;
set global gtid_mode=on_permissive;
show status like '%ongoing_anonymous_transaction_count%'; #这个值需要为0才能进行下一步,但这个值可能跳动,但不重要,只要出现一次0就可以。

在开始下一步之前,需要确认所有的 anonymous 事务全部执行完毕,否则不能开始下一步。

正常情况下,执行完上面步骤,在日志中会有如下内容:

1
2
3
4
2021-03-23T16:29:38.633061+08:00 78376 [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN.
2021-03-23T16:30:57.357049+08:00 78376 [Note] Changed ENFORCE_GTID_CONSISTENCY from WARN to ON.
2021-03-23T16:32:01.873079+08:00 78376 [Note] Changed GTID_MODE from OFF to OFF_PERMISSIVE.
2021-03-23T16:32:28.508045+08:00 78376 [Note] Changed GTID_MODE from OFF_PERMISSIVE to ON_PERMISSIVE.
step2

在所有机器上 依次执行如下命令,不区分主从顺序

1
set global gtid_mode=on;       
step3

在 slave 执行

1
2
3
STOP SLAVE [FOR CHANNEL 'channel'];
CHANGE MASTER TO MASTER_AUTO_POSITION = 1 [FOR CHANNEL 'channel'];
START SLAVE [FOR CHANNEL 'channel'];

结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.204.11.158
Master_User: rpl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 7152948
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 6603724
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Exec_Master_Log_Pos: 7152948
Relay_Log_Space: 6603965
Until_Condition: None
Master_Server_Id: 111583306
Master_UUID: 00000010-0204-0011-0158-330600000000
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
Retrieved_Gtid_Set: 00000010-0204-0011-0158-330600000000:3327-18541
Executed_Gtid_Set: 00000010-0204-0011-0158-330600000000:1-18541
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
step4

在所有机器配置文件添加

1
2
enforce_gtid_consistency=on;
gtid_mode=on;

GTID 模式关闭流程

step1

在 slave 执行

1
2
3
4
STOP SLAVE [FOR CHANNEL 'channel'];
CHANGE MASTER TO MASTER_AUTO_POSITION = 0, MASTER_LOG_FILE = file, \
MASTER_LOG_POS = position [FOR CHANNEL 'channel'];
START SLAVE [FOR CHANNEL 'channel'];
step2

在 所有机器上 依次执行如下命令(每执行一条需确认没有报错才能进行下一步),不区分主从顺序

1
2
3
set global gtid_mode = on_permissive;
set global gtid_mode = off_permissive;
SELECT @@GLOBAL.GTID_OWNED; #等待 gtid_owned 为空,可能出现跳变,不重要,只要曾经为空就可以。

在开始下一步之前,需要确认所有的 GTID 事务全部执行完毕,否则不能开始下一步。

step3

在 所有机器上 依次执行如下命令(每执行一条需确认没有报错才能进行下一步),不区分主从顺序

1
2
set global gtid_mode = off;
set global enforce_gtid_consistency = OFF;

配置文件添加

1
2
gtid_mode = off
enforce_gtid_consistency = off