Mysql主从复制----传统模式切换到GTID模式

Mysql主从复制----传统模式切换到GTID模式

传统模式(binlog+position)的复制切换到GTID复制:

1)主库和从库上修改参数enforce_gtid_consistency=warn,然后观察error log,确认没有GTID不兼容的语句。

  1. mysql> set @@global.enforce_gtid_consistency=warn;
  2. Query OK, 0 rows affected (0.12 sec)

enforce_gtid_consistency:
    Scope:Global
    Dynamic:Yes(>= 5.7.6) No(<= 5.7.5)
    Type:Enumeration(>= 5.7.6) Boolean(<= 5.7.5)
    Default Value:OFF(>= 5.7.6) false(<= 5.7.5)
    Valid Values OFF\ON\WARN

Depending on the value of this variable, the server enforces GTID consistency by allowing execution of only statements that can be safely logged using a GTID. You must set this variable to ON before enabling GTID based replication.
    
The values that enforce_gtid_consistency can be configured to are:
    OFF: all transactions are allowed to violate GTID consistency.
    ON: no transaction is allowed to violate GTID consistency.
    WARN: all transactions are allowed to violate GTID consistency, but a warning is generated in this case. WARN was added in MySQL 5.7.6.

以下语法GTID都不支持:
    1.CREATE TABLE ...SELECT ..
        该语句会被拆分为create table和insert table两个事务且分配同一个GTID,传到备库后,insert操作会被忽略
        ERROR 1786 (HY000): Statement violates GTID consistency: CREATE TABLE ... SELECT.
    2.在事务内部的CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE
        ERROR 1787 (HY000): Statement violates GTID consistency:CREATE TEMPORARY TABLE and DROP TEMPORARY TABLE can only be executed outside transactional context.
        These statements are also not allowed in a function or trigger because functions and triggers are also considered to be multi-statement transactions.
    3.在一个事务内部同时更新非事务引擎表和事务引擎表
        ERROR 1785 (HY000): When @@GLOBAL.ENFORCE_GTID_CONSISTENCY = 1, updates to non-transactional tables can only be done in either autocommitted statements or single-statement transactions,and never in the same statement as updates to transactional tables.

2)主库和从库上修改参数enforce_gtid_consistency=on(记得修改my.cnf配置文件)

  1. mysql> set @@global.enforce_gtid_consistency=on;
  2. Query OK, 0 rows affected (0.00 sec)


3)主库和从库上同时修改参数gtid_mode,顺序为OFF-->OFF_PERMISSIVE-->ON_PERMISSIVE-->ON(记得修改my.cnf配置文件)

  1. mysql> set @@global.gtid_mode = on_permissive;
  2. ERROR 1788 (HY000): The value of @@GLOBAL.GTID_MODE can only be changed one step at a time: OFF <-> OFF_PERMISSIVE <-> ON_PERMISSIVE <-> ON. Also note that this value must be stepped up or down simultaneously on all servers. See the Manual for instructions.
  3. mysql> set @@global.gtid_mode = off_permissive;
  4. Query OK, 0 rows affected (0.30 sec)
  5.  
  6. mysql> set @@global.gtid_mode = on_permissive;
  7. Query OK, 0 rows affected (0.01 sec)
  8.  
  9. mysql> set @@global.gtid_mode = on;
  10. Query OK, 0 rows affected (0.14 sec)

4)查看从库延迟,确认无等待事务

  1. mysql> show global status like '%ongoing_anonymous%';
  2. +-------------------------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------------------------+-------+
  5. | Ongoing_anonymous_transaction_count | 0 |
  6. +-------------------------------------+-------+
  7. 1 row in set (0.11 sec)

5)从库修改复制模式
    1.stop slave;
    2.change master to master_auto_position=1;
    3.start slave;

6)主从同步验证,特别注意观察主库Executed_Gtid_Set和从库Retrieved_Gtid_Set和Executed_Gtid_Set值的变化

  1. mysql> show master status;
  2. +------------------+----------+---------------------------------+------------------+------------------------------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +------------------+----------+---------------------------------+------------------+------------------------------------------+
  5. | mysql-bin.000037 | 949 | test,testrecovery,testrecovery2 | | 7842c967-0958-11e9-9f6c-000c296ee978:1-3 |
  6. +------------------+----------+---------------------------------+------------------+------------------------------------------+
  7. 1 row in set (0.30 sec)
    1. mysql> show slave status\G
    2. *************************** 1. row ***************************
    3. Slave_IO_State: Waiting for master to send event
    4. Master_Host: 192.169.10.241
    5. Master_User: repl
    6. Master_Port: 3306
    7. Connect_Retry: 60
    8. Master_Log_File: mysql-bin.000037
    9. Read_Master_Log_Pos: 949
    10. Relay_Log_File: LAPTOP-UC5V1DNQ-relay-bin.000002
    11. Relay_Log_Pos: 1162
    12. Relay_Master_Log_File: mysql-bin.000037
    13. Slave_IO_Running: Yes
    14. Slave_SQL_Running: Yes
    15. Replicate_Do_DB: test,testrecovery,testrecovery2
    16. Replicate_Ignore_DB:
    17. Replicate_Do_Table:
    18. Replicate_Ignore_Table:
    19. Replicate_Wild_Do_Table:
    20. Replicate_Wild_Ignore_Table:
    21. Last_Errno: 0
    22. Last_Error:
    23. Skip_Counter: 0
    24. Exec_Master_Log_Pos: 949
    25. Relay_Log_Space: 1379
    26. Until_Condition: None
    27. Until_Log_File:
    28. Until_Log_Pos: 0
    29. Master_SSL_Allowed: No
    30. Master_SSL_CA_File:
    31. Master_SSL_CA_Path:
    32. Master_SSL_Cert:
    33. Master_SSL_Cipher:
    34. Master_SSL_Key:
    35. Seconds_Behind_Master: 0
    36. Master_SSL_Verify_Server_Cert: No
    37. Last_IO_Errno: 0
    38. Last_IO_Error:
    39. Last_SQL_Errno: 0
    40. Last_SQL_Error:
    41. Replicate_Ignore_Server_Ids:
    42. Master_Server_Id: 10241
    43. Master_UUID: 7842c967-0958-11e9-9f6c-000c296ee978
    44. Master_Info_File: E:\mysql-5.7.24-winx64\data\master.info
    45. SQL_Delay: 0
    46. SQL_Remaining_Delay: NULL
    47. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
    48. Master_Retry_Count: 86400
    49. Master_Bind:
    50. Last_IO_Error_Timestamp:
    51. Last_SQL_Error_Timestamp:
    52. Master_SSL_Crl:
    53. Master_SSL_Crlpath:
    54. Retrieved_Gtid_Set: 7842c967-0958-11e9-9f6c-000c296ee978:1-3
    55. Executed_Gtid_Set: 7842c967-0958-11e9-9f6c-000c296ee978:1-3
    56. Auto_Position: 1
    57. Replicate_Rewrite_DB:
    58. Channel_Name:
    59. Master_TLS_Version:
    60. 1 row in set (0.00 sec)
posted @ 2021-02-24 13:15  da0h1  阅读(105)  评论(0编辑  收藏  举报