MySQL 5.7 Non stop service changes traditional replication to an instance of GTID replication

  • 2021-07-18 09:14:39
  • OfStack

Because of the advantages of GTID, we need to change the traditional replication based on file-pos to replication based on GTID. How to change online has become a point of concern to us. The following are specific methods:

At present, we have a traditional replication of M-S structure:

port 3301 master

port 3302 slave


master On ( 3301 ): 
[zejin] 3301>select * from t_users;
+----+------+
| id | name |
+----+------+
| 1 | hao |
| 2 | zhou |
+----+------+
rows in set (0.00 sec)
 
 
slave On ( 3302 ): 
[zejin] 3302>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.240
Master_User: repl
Master_Port: 3301
Connect_Retry: 60
Master_Log_File: binlog57.000002
Read_Master_Log_Pos: 417
Relay_Log_File: zejin240-relay-bin.000004
Relay_Log_Pos: 628
Relay_Master_Log_File: binlog57.000002
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: 417
Relay_Log_Space: 884
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: 3301
Master_UUID: a97983fc-5a29-11e6-9d28-000c29d4dc3f
Master_Info_File: /home/mysql/I3302/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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
row in set (0.00 sec)
 
[zejin] 3302>select * from t_users;
+----+------+
| id | name |
+----+------+
| 1 | hao |
| 2 | zhou |
+----+------+
rows in set (0.00 sec)

The following are the specific operation steps of online change:

Premises:

1. All mysql versions 5.7. 6 or later are required.

2. The values of gtid_mode of all mysql in the current topology are off.

3. The following operation steps are orderly, so don't jump.

Description of global system variable GTID_MODE variable value under supplement 1:

The new transaction of OFF is non-GTID, and Slave only accepts transactions without GTID, and the transactions sent to GTID will report errors

The OFF_PERMISSIVE new transaction is a non-GTID, and Slave accepts both transactions without GTID and transactions with GTID

ON_PERMISSIVE the new transaction is GTID, and Slave accepts both transactions without GTID and transactions with GTID

The new ON transaction is GTID, and Slave only accepts transactions with GTID

It should be noted that these values change in sequence, that is,

off < --- > OFF_PERMISSIVE < --- > ON_PERMISSIVE < --- > ON

If you can't jump, you will report an error.

step1: On each mysql instance, set ENFORCE_GTID_CONSISTENCY to warning, which executes first does not affect the result.


[zejin] 3302>set @@global.enforce_gtid_consistency=warn;
Query OK, 0 rows affected (0.00 sec)
[zejin] 3301>set @@global.enforce_gtid_consistency=warn;
Query OK, 0 rows affected (0.00 sec)

Note: After executing this statement, If GTID incompatible statement usage occurs, relevant information will be recorded in the error log, so it is necessary to adjust the program to avoid incompatible writing until no incompatible statement is generated at all. You can check all sql through the program, or you can observe the error log for a period of time after setting. This step is very important.

step2: On each mysql instance, set ENFORCE_GTID_CONSISTENCY to ON, which executes first does not affect the result

After Step 1 is complete, you can set the value to on.


[zejin] 3301>set @@global.enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.03 sec)
 
[zejin] 3302>set @@global.enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)

step3: On each mysql instance, set GTID_MODE to off_permissiv; Which one executes first does not affect the result


[zejin] 3301>SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;
Query OK, 0 rows affected (0.00 sec)
 
[zejin] 3302>SET @@GLOBAL.GTID_MODE = OFF_PERMISSIVE;
Query OK, 0 rows affected (0.00 sec)

step4: On each mysql instance, set GTID_MODE to on_permissiv; ; Which one executes first does not affect the result


[zejin] 3302>SET @@GLOBAL.GTID_MODE = on_permissive;
Query OK, 0 rows affected (0.00 sec)
[zejin] 3301>SET @@GLOBAL.GTID_MODE = on_permissive;
Query OK, 0 rows affected (0.01 sec)

step5: Check the variable ONGOING_ANONYMOUS_TRANSACTION_COUNT on each mysql instance


[zejin] 3301>SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT';
+-------------------------------------+-------+
| Variable_name      | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0  |
+-------------------------------------+-------+
row in set (0.02 sec)
 
 
[zejin] 3302>SHOW STATUS LIKE 'ONGOING_ANONYMOUS_TRANSACTION_COUNT';
+-------------------------------------+-------+
| Variable_name      | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0  |
+-------------------------------------+-------+
row in set (0.02 sec)

You need to wait until this variable is 0

step6: Ensure that all anonymous transactions (non-GTID transactions) have been fully replicated on all server.

Inspection method:


 In master On: 
[zejin] 3301>show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File   | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| binlog57.000005 |  154 |    |     |     |
+-----------------+----------+--------------+------------------+-------------------+
row in set (0.00 sec)
 
 
 In slave Go, 
 
[zejin] 3302>show slave status\G
*************************** 1. row ***************************
 ... 
  Relay_Master_Log_File: binlog57.000005
   Exec_Master_Log_Pos: 154
 ... 

Check that the values of these two entries Relay_Master_Log_File are greater than binlog57.000005,

Or Relay_Master_Log_File is equal to binlog57.000005 and the value of Exec_Master_Log_Pos is greater than or equal to 154

Or slave directly uses the function:


[zejin] 3302>SELECT MASTER_POS_WAIT('binlog57.000005', 154);
+-----------------------------------------+
| MASTER_POS_WAIT('binlog57.000005', 154) |
+-----------------------------------------+
|          0 |
+-----------------------------------------+
row in set (0.00 sec)

If the return result is greater than or equal to 0, it means that all anonymous transactions have been replicated

step7: Confirm that there are no anonymous transactions in the whole topology. If all anonymous transactions generated before have been executed, there should be no anonymous transactions even in binary logs. You can use flush logs and let mysql automatically clean up the old binary log files.

step8: On each mysql instance, set GTID_MODE to on,


[zejin] 3301>SET @@GLOBAL.GTID_MODE = ON;
Query OK, 0 rows affected (0.04 sec)
 
[zejin] 3302>SET @@GLOBAL.GTID_MODE = ON;
Query OK, 0 rows affected (0.04 sec)

step9: Add gtid-mode=ON to the configuration file my. cnf for each mysql instance

Verification:


[zejin] 3301>insert into t_users values(3,'chen');
Query OK, 1 row affected (0.02 sec)
[zejin] 3301>update t_users set name='li' where id=1;
Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0
[zejin] 3301>select * from t_users;
+----+------+
| id | name |
+----+------+
| 1 | li |
| 2 | zhou |
| 3 | chen |
+----+------+
rows in set (0.00 sec)
 
 
[zejin] 3302>show slave status\G
*************************** 1. row ***************************
    Slave_IO_State: Waiting for master to send event
     Master_Host: 192.168.1.240
     Master_User: repl
     Master_Port: 3301
    Connect_Retry: 60
    Master_Log_File: binlog57.000006
   Read_Master_Log_Pos: 462
    Relay_Log_File: zejin240-relay-bin.000012
    Relay_Log_Pos: 673
  Relay_Master_Log_File: binlog57.000006
    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: 462
    Relay_Log_Space: 969
    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: 3301
     Master_UUID: a97983fc-5a29-11e6-9d28-000c29d4dc3f
    Master_Info_File: /home/mysql/I3302/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: a97983fc-5a29-11e6-9d28-000c29d4dc3f:1-2
   Executed_Gtid_Set: a97983fc-5a29-11e6-9d28-000c29d4dc3f:1-2
    Auto_Position: 0
   Replicate_Rewrite_DB: 
     Channel_Name: 
   Master_TLS_Version: 
row in set (0.00 sec)

At this point, the online conversion from traditional replication to GTID replication is completed.


Related articles: