mysql.gtid_executed表的更新机制

mysql.gtid_executed表的更新机制

环境

  • 主库:mysql5.7.18,IP:192.168.1.21
  • 备库:mysql5.7.18,IP:192.168.1.128

mysql.gtid_executed在主库上更新机制

1. 主库开启binlog

  • 查看binlog是否打开
  
  
  1. mysql> show variables like '%log_bin%';
  2. +---------------------------------+------------------------------------+
  3. | Variable_name | Value |
  4. +---------------------------------+------------------------------------+
  5. | log_bin | ON |
  6. | log_bin_basename | /data/mysql/binlog/mysql-bin |
  7. | log_bin_index | /data/mysql/binlog/mysql-bin.index |
  8. | log_bin_trust_function_creators | OFF |
  9. | log_bin_use_v1_row_events | OFF |
  10. | sql_log_bin | ON |
  11. +---------------------------------+------------------------------------+
  12. 6 rows in set (0.01 sec)
  • 主库上进行操作,产生事务,观察mysql.gtid_executed表的变化情况
  
  
  1. mysql> select * from mysql.gtid_executed;
  2. +--------------------------------------+----------------+--------------+
  3. | source_uuid | interval_start | interval_end |
  4. +--------------------------------------+----------------+--------------+
  5. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 2 |
  6. +--------------------------------------+----------------+--------------+
  7. 1 row in set (0.00 sec)
  8. mysql> insert into xiyouji values (1,'shawujin');
  9. Query OK, 1 row affected (0.02 sec)
  10. mysql> select * from mysql.gtid_executed;
  11. +--------------------------------------+----------------+--------------+
  12. | source_uuid | interval_start | interval_end |
  13. +--------------------------------------+----------------+--------------+
  14. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 2 |
  15. +--------------------------------------+----------------+--------------+
  16. 1 row in set (0.00 sec)
  17. #mysql.gtid_executed表没有发生变化
  • 刷新日志
  
  
  1. mysql> flush logs;
  2. Query OK, 0 rows affected (0.07 sec)
  3. mysql> select * from mysql.gtid_executed;
  4. +--------------------------------------+----------------+--------------+
  5. | source_uuid | interval_start | interval_end |
  6. +--------------------------------------+----------------+--------------+
  7. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 4 |
  8. +--------------------------------------+----------------+--------------+
  9. 1 row in set (0.00 sec)
  10. #发现主库在开启binlog的情况下,mysql.gtid_executed并不会实时更新,仅在二进制binlog rotation时发生

2. 主库关闭binlog

  • 查看binlog是否关闭
  
  
  1. mysql> show variables like '%log_bin%';
  2. +---------------------------------+-------+
  3. | Variable_name | Value |
  4. +---------------------------------+-------+
  5. | log_bin | OFF |
  6. | log_bin_basename | |
  7. | log_bin_index | |
  8. | log_bin_trust_function_creators | OFF |
  9. | log_bin_use_v1_row_events | OFF |
  10. | sql_log_bin | ON |
  11. +---------------------------------+-------+
  12. 6 rows in set (0.01 sec)
  • 主库上进行操作,产生事务,观察mysql.gtid_executed表的变化情况
  
  
  1. mysql> select * from mysql.gtid_executed;
  2. +--------------------------------------+----------------+--------------+
  3. | source_uuid | interval_start | interval_end |
  4. +--------------------------------------+----------------+--------------+
  5. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 5 |
  6. +--------------------------------------+----------------+--------------+
  7. 1 row in set (0.00 sec)
  8. mysql> insert into wukong_test.xiyouji values (10,'aaaaa');
  9. Query OK, 1 row affected (0.01 sec)
  10. mysql> select * from mysql.gtid_executed;
  11. +--------------------------------------+----------------+--------------+
  12. | source_uuid | interval_start | interval_end |
  13. +--------------------------------------+----------------+--------------+
  14. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 5 |
  15. +--------------------------------------+----------------+--------------+
  16. 1 row in set (0.00 sec)
  17. #发现mysql.gtid_executed并不会记录信息
  • 刷新日志
  
  
  1. mysql> flush logs;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> show master status;
  4. Empty set (0.00 sec)
  5. mysql> select * from mysql.gtid_executed;
  6. +--------------------------------------+----------------+--------------+
  7. | source_uuid | interval_start | interval_end |
  8. +--------------------------------------+----------------+--------------+
  9. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 5 |
  10. +--------------------------------------+----------------+--------------+
  11. 1 row in set (0.00 sec)
  12. #mysql.gtid_executed表还是不会进行更新
  • 此时备库mysql.gtid_executed表也不会记录信息。因为,主库都关闭了binlog,此时主从复制已经出现了错误。
  
  
  1. root@localhost : (none) 03:25:09> show slave status\G;
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 192.168.1.21
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File: mysql-bin.000009
  9. Read_Master_Log_Pos: 464
  10. Relay_Log_File: mysql-relay-bin.000016
  11. Relay_Log_Pos: 677
  12. Relay_Master_Log_File: mysql-bin.000009
  13. Slave_IO_Running: No
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  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: 464
  25. Relay_Log_Space: 971
  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: NULL
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 1236
  38. Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Binary log is not open'
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 12001
  43. Master_UUID: 0a646c88-36e2-11e7-937d-fa163ed7a7b1
  44. Master_Info_File: mysql.slave_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: 170524 03:23:16
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set: 0a646c88-36e2-11e7-937d-fa163ed7a7b1:3-12
  55. Executed_Gtid_Set: 0a646c88-36e2-11e7-937d-fa163ed7a7b1:1-12,
  56. 3a1f3827-4029-11e7-9dc1-fa163ebca1df:1-3
  57. Auto_Position: 1
  58. Replicate_Rewrite_DB:
  59. Channel_Name:
  60. Master_TLS_Version:
  61. 1 row in set (0.00 sec)
  62. #解决方法:开启主库binlog,然后stop slave;start slave;

mysql.gtid_executed在备库上更新机制

1. 备库关闭log_slave_updates

  • 查看log_slave_updates是否关闭
  
  
  1. root@localhost : mysql 11:48:31> show variables like '%log_slave%';
  2. +-------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------+-------+
  5. | log_slave_updates | OFF |
  6. +-------------------+-------+
  7. 1 row in set (0.00 sec)
  • 在主库上产生事务,并观察备库mysql.gtid_executed表的变化情况
  
  
  1. root@localhost : mysql 02:55:12> select * from mysql.gtid_executed;
  2. +--------------------------------------+----------------+--------------+
  3. | source_uuid | interval_start | interval_end |
  4. +--------------------------------------+----------------+--------------+
  5. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 1 |
  6. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 2 | 2 |
  7. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 3 | 3 |
  8. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 4 | 4 |
  9. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 5 | 5 |
  10. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 6 | 6 |
  11. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 7 | 7 |
  12. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 8 | 8 |
  13. | 3a1f3827-4029-11e7-9dc1-fa163ebca1df | 1 | 3 |
  14. +--------------------------------------+----------------+--------------+
  15. 9 rows in set (0.00 sec)
  16. #主库上进行insert
  17. mysql> insert into wukong_test.xiyouji values (10,'aaaaa');
  18. Query OK, 1 row affected (0.01 sec)
  19. #备库上的mysql.gtid_executed会每一个事务进行更新
  20. root@localhost : mysql 02:58:28> select * from mysql.gtid_executed;
  21. +--------------------------------------+----------------+--------------+
  22. | source_uuid | interval_start | interval_end |
  23. +--------------------------------------+----------------+--------------+
  24. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 1 |
  25. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 2 | 2 |
  26. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 3 | 3 |
  27. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 4 | 4 |
  28. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 5 | 5 |
  29. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 6 | 6 |
  30. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 7 | 7 |
  31. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 8 | 8 |
  32. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 9 | 9 |
  33. | 3a1f3827-4029-11e7-9dc1-fa163ebca1df | 1 | 3 |
  34. +--------------------------------------+----------------+--------------+
  35. 10 rows in set (0.00 sec)

1. 备库打开log_slave_updates

  • 查看log_slave_updates是否打开
  
  
  1. root@localhost : (none) 03:46:54> show variables like '%log_slave%';
  2. +-------------------+-------+
  3. | Variable_name | Value |
  4. +-------------------+-------+
  5. | log_slave_updates | ON |
  6. +-------------------+-------+
  7. 1 row in set (0.00 sec)
  • 在主库上产生事务,并观察备库mysql.gtid_executed表的变化情况
  
  
  1. root@localhost : (none) 03:47:26> select * from mysql.gtid_executed;
  2. +--------------------------------------+----------------+--------------+
  3. | source_uuid | interval_start | interval_end |
  4. +--------------------------------------+----------------+--------------+
  5. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 1 |
  6. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 2 | 2 |
  7. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 3 | 3 |
  8. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 4 | 4 |
  9. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 5 | 5 |
  10. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 6 | 6 |
  11. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 7 | 7 |
  12. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 8 | 8 |
  13. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 9 | 9 |
  14. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 10 | 10 |
  15. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 11 | 11 |
  16. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 12 | 12 |
  17. | 3a1f3827-4029-11e7-9dc1-fa163ebca1df | 1 | 3 |
  18. +--------------------------------------+----------------+--------------+
  19. 13 rows in set (0.00 sec)
  20. #主库上操作,产生事务:
  21. mysql> insert into wukong_test.xiyouji values (10,'bbbb');
  22. Query OK, 1 row affected (0.02 sec)
  23. #查看备库上mysql.gtid_executed表并不会实时更新:
  24. root@localhost : (none) 03:48:09> select * from mysql.gtid_executed;
  25. +--------------------------------------+----------------+--------------+
  26. | source_uuid | interval_start | interval_end |
  27. +--------------------------------------+----------------+--------------+
  28. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 1 |
  29. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 2 | 2 |
  30. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 3 | 3 |
  31. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 4 | 4 |
  32. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 5 | 5 |
  33. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 6 | 6 |
  34. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 7 | 7 |
  35. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 8 | 8 |
  36. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 9 | 9 |
  37. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 10 | 10 |
  38. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 11 | 11 |
  39. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 12 | 12 |
  40. | 3a1f3827-4029-11e7-9dc1-fa163ebca1df | 1 | 3 |
  41. +--------------------------------------+----------------+--------------+
  42. 13 rows in set (0.00 sec)
  • 备库进行日志刷新
  
  
  1. root@localhost : (none) 03:49:40> flush logs;
  2. Query OK, 0 rows affected (0.11 sec)
  3. root@localhost : (none) 03:49:59> select * from mysql.gtid_executed;
  4. +--------------------------------------+----------------+--------------+
  5. | source_uuid | interval_start | interval_end |
  6. +--------------------------------------+----------------+--------------+
  7. | 0a646c88-36e2-11e7-937d-fa163ed7a7b1 | 1 | 16 |
  8. | 3a1f3827-4029-11e7-9dc1-fa163ebca1df | 1 | 3 |
  9. +--------------------------------------+----------------+--------------+
  10. 2 rows in set (0.00 sec)
  11. #发现在binary log rotation的时候会去记录并进行压缩。

总结

  • 1、主库上mysql.gtid_executed也是会进行更新,开启了binlog后,会在binlog rotation时进行更新。
  • 2、备库上mysql.gtid_executed表的更新情况主要取决于log_slave_updates参数。
    • log_slave_updates = ON,不会每个事务进行实时更新,而是在binary log rotation的时候会更新并进行压缩。
    • log_slave_updates = OFF,备库上的mysql.gtid_executed会每一个事务进行更新。并且,此时mysql.gtid_executed表的压缩取决于系统变量executed_gtids_compression_period,默认为1000个事务进行压缩。
  • 3、MySQL 5.6版本开启GTID模式,必须打开参数log_slave_updates,简单来说就是必须在从机上再记录一份二进制日志。这样的无论对性能还是存储的开销,无疑会相应的增大。而从MySQL 5.7.5版本开始无需在GTID模式下启用参数log_slave_updates,而是将GTID值持久化保存在一张InnoDB表中,并与用户事务一起进行提交,从而实现数据的一致性。
  • 4、关于log_slave_updates 参数
    • Normally, a slave does not log to its own binary log any updates that are received from a master server. This option tells the slave to log the updates performed by its SQL thread to its own binary log. For this option to have any effect, the slave must also be started with the –log-bin option to enable binary logging. Prior to MySQL 5.5, the server would not start when using the –log-slave-updates option without also starting the server with the –log-bin option, and would fail with an error; in MySQL 5.5, only a warning is generated. (Bug #44663) –log-slave-updates is used when you want to chain replication servers. For example, you might want to set up replication servers using this arrangement: 
      A -> B -> C 
      Here, A serves as the master for the slave B, and B serves as the master for the slave C. For this to work, B must be both a master and a slave. You must start both A and B with –log-bin to enable binary logging, and B with the –log-slave-updates option so that updates received from A are logged by B to its binary log.

参考资料

https://dev.mysql.com/doc/refman/5.7/en/replication-gtids-concepts.html

猜你喜欢

转载自blog.csdn.net/wukong_666/article/details/72723102