主从GTID复制修复

介绍

GTID是5.6新增特性,减少DBA运维的工作。在以前一主两从架构下当主库M1发生故障我们需要选择一个从库S1作为新的主库,但是S2要重新change master 到新主库上 这时master_log_file和master_log_pos我们怎么获取到呢?在没有GTID时 MHA架构帮我们解决了这个问题,在有了GTID情况下 我们只需要在S2上面重新change master 下 设置MASTER_AUTO_POSITION = 1即可。 本文介绍下在GTID复制下遇到了错误如何解决。

场景描述

使用show slave status\G 查看主备复制状态时 错误如下:

  1. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 12:56:57]>SHOW SLAVE STATUS\G
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 10.10.30.101
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 10
  8. Master_Log_File: mysql-bin.000003
  9. Read_Master_Log_Pos: 28074558
  10. Relay_Log_File: mysql-relay-bin.000011
  11. Relay_Log_Pos: 100311
  12. Relay_Master_Log_File: mysql-bin.000003
  13. Slave_IO_Running: Yes
  14. Slave_SQL_Running: No
  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: 1007
  22. Last_Error: Error 'Can't create database 'dhy2'; database exists' on query. Default database: 'dhy2'. Query: 'create database dhy2'
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 28073671
  25. Relay_Log_Space: 287925
  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: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 1007
  40. Last_SQL_Error: Error 'Can't create database 'dhy2'; database exists' on query. Default database: 'dhy2'. Query: 'create database dhy2'
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 101
  43. Master_UUID: dd2a02a3-f0be-11e5-af62-0050563a97cc
  44. Master_Info_File: mysql.slave_master_info
  45. SQL_Delay: 0
  46. SQL_Remaining_Delay: NULL
  47. Slave_SQL_Running_State:
  48. Master_Retry_Count: 86400
  49. Master_Bind:
  50. Last_IO_Error_Timestamp:
  51. Last_SQL_Error_Timestamp: 160408 12:57:02
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:70261-71462
  55. Executed_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:1-71459,
  56. e28420fb-f0be-11e5-af62-0050562edd64:1-81209
  57. Auto_Position: 0
  58. 1 row in set (0.00 sec)

根据报错描述:

  1. Last_SQL_Error: Error 'Can't create database 'dhy2'; database exists' on query. Default database: 'dhy2'. Query: 'create database dhy2'
  2. dhy2 这个数据库已经在备库存在

我们在备库上面show database 查看dhy2这个数据库确实是存在的:

  1. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 12:57:04]>show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | information_schema |
  6. | dhy2 |
  7. | mysql |
  8. | performance_schema |
  9. | test |
  10. +--------------------+
  11. 7 rows in set (0.00 sec)

修改过程

当我们使用GTID复制时,不能像传统复制一样跳过事务,只能注册一个空的事务骗过MySQL。具体操作步骤如下:

  • 查看当前备份的GTID执行情况
  1. Retrieved_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:70261-71462
  2. Executed_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:1-71459,
  3. e28420fb-f0be-11e5-af62-0050562edd64:1-81209
  4. Auto_Position: 0

Retrieved_Gtid_Set 代表已经接受到的GTID集合 
Executed_Gtid_Set 代码已经执行的GTID集合

针对上面GTID执行情况 我们可以看到:

  1. Retrieved_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:70261-71462
  2. Executed_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:1-71459

接收到了71462 执行到了71459就报错了。

  • 手工注册事务
  1. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 12:59:41]>stop slave;
  2. Query OK, 0 rows affected (0.00 sec)
  3. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 13:04:57]>set gtid_next='dd2a02a3-f0be-11e5-af62-0050563a97cc:71460';
  4. Query OK, 0 rows affected (0.00 sec)
  5. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 13:05:23]>begin;
  6. Query OK, 0 rows affected (0.00 sec)
  7. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 13:05:29]>commit;
  8. Query OK, 0 rows affected (0.02 sec)
  9. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 13:05:31]>SET GTID_NEXT='AUTOMATIC';
  10. Query OK, 0 rows affected (0.00 sec)
  11. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 13:05:36]>start slave;
  12. Query OK, 0 rows affected (0.00 sec)

这里需要注意的是

  1. set gtid_next='dd2a02a3-f0be-11e5-af62-0050563a97cc:71460'; 这个值是
  2. Executed_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:1-71459 71459这个GTID+1
  3. 并且 uuid一定是Retrieved_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:70261-71462 接收到这里显示的uuid
  • 再次查看show slave status\G 已经恢复正常
  1. [root@shadow1:/root 5.6.28-log_Instance1 root@localhost:test 13:05:39]>show slave status\G
  2. *************************** 1. row ***************************
  3. Slave_IO_State: Waiting for master to send event
  4. Master_Host: 10.10.30.101
  5. Master_User: repl
  6. Master_Port: 3306
  7. Connect_Retry: 10
  8. Master_Log_File: mysql-bin.000003
  9. Read_Master_Log_Pos: 28275288
  10. Relay_Log_File: mysql-relay-bin.000012
  11. Relay_Log_Pos: 21794
  12. Relay_Master_Log_File: mysql-bin.000003
  13. Slave_IO_Running: Yes
  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: 28275288
  25. Relay_Log_Space: 302335
  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: 101
  43. Master_UUID: dd2a02a3-f0be-11e5-af62-0050563a97cc
  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 the slave I/O thread to update it
  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: dd2a02a3-f0be-11e5-af62-0050563a97cc:70261-72005
  55. Executed_Gtid_Set: dd2a02a3-f0be-11e5-af62-0050563a97cc:1-72005,
  56. e28420fb-f0be-11e5-af62-0050562edd64:1-81209
  57. Auto_Position: 0
展开阅读全文

没有更多推荐了,返回首页