MySQL 5.7.24 由传统的主从复制在线切换为GTID复制


MySQL5.7.6版本以上支持在线将传统复制切换为GTID复制.同时提供了一个表查看GTID:
mysql> select * from mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid                          | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| 1c54fdbc-3f0d-11e9-a1b3-005056be07bb |              1 |            1 |
| 1c54fdbc-3f0d-11e9-a1b3-005056be07bb |              2 |            2 |
+--------------------------------------+----------------+--------------+
2 rows in set (0.00 sec)



主库:
mysql> show variables like 'gtid_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode     | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

--从库:
mysql> show variables like 'gtid_mode';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| gtid_mode     | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show variables like '%gtid%';
+----------------------------------+-----------+
| Variable_name                    | Value     |
+----------------------------------+-----------+
| binlog_gtid_simple_recovery      | ON        |
| enforce_gtid_consistency         | OFF       |
| gtid_executed_compression_period | 1000      |
| gtid_mode                        | OFF       |
| gtid_next                        | AUTOMATIC |
| gtid_owned                       |           |
| gtid_purged                      |           |
| session_track_gtids              | OFF       |
+----------------------------------+-----------+
8 rows in set (0.00 sec)
参数enforce_gtid_consistency=off和gtid_mode=off.

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.4.171
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000001
          Read_Master_Log_Pos: 763635081
               Relay_Log_File: mysql_relay.000003
                Relay_Log_Pos: 678009142
        Relay_Master_Log_File: mysql_bin.000001
             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: 678008976
              Relay_Log_Space: 763635450
              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: 520
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: 100
                  Master_UUID: 1c54fdbc-3f0d-11e9-a1b3-005056be07bb
             Master_Info_File: /data/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Reading event from the relay log
           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: 
1 row in set (0.00 sec)


注释:当Auto_Position: 0表示是传统的主从复制。

主库和从库均设置参数:enforce_gtid_consistency=warn
mysql> set @@global.enforce_gtid_consistency=warn;
Query OK, 0 rows affected (0.53 sec)

mysql> select @@global.enforce_gtid_consistency;
+-----------------------------------+
| @@global.enforce_gtid_consistency |
+-----------------------------------+
| WARN                              |
+-----------------------------------+
1 row in set (0.03 sec)

--分别在主库和从库查看错误日志信息:确保主库和从库均没有warn信息。

mysql> show variables like 'log_error';
+---------------+---------------------+
| Variable_name | Value               |
+---------------+---------------------+
| log_error     | /var/log/mysqld.log |
+---------------+---------------------+
1 row in set (0.39 sec)

--主库:
# tail -f /var/log/mysqld.log 
2019-03-05T07:49:58.624406Z 33 [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN.
2019-03-05T07:50:14.505444Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 4975ms. The settings might not be optimal. (flushed=608 and evicted=361, during the time.)
2019-03-05T07:50:53.181344Z 0 [Note] InnoDB: page_cleaner: 1000ms intended loop took 10286ms. The settings might not be optimal. (flushed=593 and evicted=841, during the time.)
--从库:
# tail -f /var/log/mysqld.log 
2019-03-05T07:52:21.513110Z 7 [Note] Changed ENFORCE_GTID_CONSISTENCY from OFF to WARN.

分别在主库和从库上将参数设置为on:
mysql> set @@global.enforce_gtid_consistency=on;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@global.enforce_gtid_consistency;
+-----------------------------------+
| @@global.enforce_gtid_consistency |
+-----------------------------------+
| ON                                |
+-----------------------------------+
1 row in set (0.00 sec)

分别在主库和从库设置:

mysql> set @@global.gtid_mode=off_permissive;
Query OK, 0 rows affected (0.06 sec)

分别在主库和从库设置:
mysql> set @@global.gtid_mode=on_permissive;
Query OK, 0 rows affected (0.24 sec)

--查看主库和从库的参数:
mysql> show status like 'ongoing_anonymous_transaction_count';
+-------------------------------------+-------+
| Variable_name                       | Value |
+-------------------------------------+-------+
| Ongoing_anonymous_transaction_count | 0     |
+-------------------------------------+-------+
1 row in set (0.05 sec)

查看返回的值是否是为0。

--主从库执行:
mysql> set @@global.gtid_mode=on_permissive;


主从库查看GTID参数:
mysql> show variables like '%gtid%';
+----------------------------------+---------------+
| Variable_name                    | Value         |
+----------------------------------+---------------+
| binlog_gtid_simple_recovery      | ON            |
| enforce_gtid_consistency         | ON            |
| gtid_executed_compression_period | 1000          |
| gtid_mode                        | ON_PERMISSIVE |
| gtid_next                        | AUTOMATIC     |
| gtid_owned                       |               |
| gtid_purged                      |               |
| session_track_gtids              | OFF           |
+----------------------------------+---------------+
8 rows in set (0.00 sec)
参数enforce_gtid_consistency=on,gtid_mode  =ON_PERMISSIVE .


主库查看:
mysql> show variables like '%gtid%';
+----------------------------------+---------------+
| Variable_name                    | Value         |
+----------------------------------+---------------+
| binlog_gtid_simple_recovery      | ON            |
| enforce_gtid_consistency         | ON            |
| gtid_executed_compression_period | 1000          |
| gtid_mode                        | ON_PERMISSIVE |
| gtid_next                        | AUTOMATIC     |
| gtid_owned                       |               |
| gtid_purged                      |               |
| session_track_gtids              | OFF           |
+----------------------------------+---------------+
8 rows in set (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set                        |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql_bin.000005 |      492 |              |                  | 1c54fdbc-3f0d-11e9-a1b3-005056be07bb:1-2 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)


从库查看:
在线修改:
mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to master_auto_position=1;
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like '%gtid%';
+----------------------------------+------------------------------------------+
| Variable_name                    | Value                                    |
+----------------------------------+------------------------------------------+
| binlog_gtid_simple_recovery      | ON                                       |
| enforce_gtid_consistency         | ON                                       |
| gtid_executed_compression_period | 1000                                     |
| gtid_mode                        | ON                                       |
| gtid_next                        | AUTOMATIC                                |
| gtid_owned                       |                                          |
| gtid_purged                      | 1c54fdbc-3f0d-11e9-a1b3-005056be07bb:1-2 |
| session_track_gtids              | OFF                                      |
+----------------------------------+------------------------------------------+
8 rows in set (0.00 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.4.171
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000005
          Read_Master_Log_Pos: 492
               Relay_Log_File: mysql_relay.000002
                Relay_Log_Pos: 705
        Relay_Master_Log_File: mysql_bin.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: 492
              Relay_Log_Space: 908
              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: 100
                  Master_UUID: 1c54fdbc-3f0d-11e9-a1b3-005056be07bb
             Master_Info_File: /data/mysql/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: 1c54fdbc-3f0d-11e9-a1b3-005056be07bb:1-2
            Executed_Gtid_Set: 1c54fdbc-3f0d-11e9-a1b3-005056be07bb:1-2
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

注意查看参数:Auto_Position: 1;Retrieved_Gtid_Set和Executed_Gtid_Set参数。

查看正常之后需要将参数写入到配置文件/etc/my.cnf:
...
gtid_mode=ON
enforce-gtid-consistency=true

总结:在线将传统复制修改为GTID复制的步骤:
1.在主从复制的MySQL实例上运行:set  @@global.enforce_gtid_consistency=warn;
  该参数使得所有事务都允许违反GTID的一致性.
  此步骤很重要,在进行下一步之前需要确保在 错误日志 中无任何警告.
2.在主从复制的MySQL实例上运行: set @@global.enforce_gtid_consistency=on;
  该参数确保所有的事务都不能违反GTID的一致性.
3.在主从复制的MySQL实例上运行: set @@global.gtid_mode=off_permissive;
  该参数表示新的事务是匿名的,同事允许复制的事务是GTID或匿名的.
4.在主从复制的MySQL实例上运行: set @@global.gtid_mode=on_permissive;
  该参数表示新的事务是匿名的,同事允许复制的事务是GTID或匿名的.
5.在MySQL主从复制的从库实例上查询:show status like 'ongoing_anonymous_transaction_count';
  在所有从库上查询改参数值,确保该值为0才能进行下一步操作.
  该状态表示已经标记为匿名的正在进行的事务数据量,若状态值为0则表示无事务等待处理。
 
6.在MySQL主从复制的每个实例上运行: set @@global.gtid_mode=on;
  表示开启GTID复制.
7.将参数写入到每个实例的my.cnf 文件,即使MySQL实例重启仍然生效:
..
gtid_mode = ON
enforce-gtid-consistency = true
8.在线调整为GTID复制:
mysql>stop slave;
mysql>change master to master_auto_position=1;
mysql>start slave;
9.查看参数和状态:
mysql> show variables like '%gtid%';
mysql> show slave status\G


GTID复制相关的参数:

server-id:每个MySQL 实例的ID值,不能相同。必选项
gtid_mode = ON: MySQL是开启GTID模式.
enforce-gtid-consistency=on:开启GTID复制是,保证GTID的一致性.
log-bin:MySQL 必须开启binlog.
binlog_format=row: MySQLbinlog 的事实标准,其他格式会导致数据丢失.
log-slave-updates=on:级联复制,当slave接受到master的更新且执行完毕之后,执行的binlog是否写入slave的binlog记录中. 开启.

 

你可能感兴趣的:(MySQL)