第一种情况,开启GTID,从库与主库不同步。

1、在从库上查看从的状态

 
  1. mysql> show slave status \G
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 10.120.141.168
  5. Master_User: sys_repl
  6. Master_Port: 3306
  7. Connect_Retry: 10
  8. Master_Log_File:
  9. Read_Master_Log_Pos: 4
  10. Relay_Log_File: mysqld-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. 。。。。。。。。
  13. Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'
  14. 。。。。。。
  15. Retrieved_Gtid_Set: 79212d47-7122-11e7-8641-0050569f788a:13579-17760
  16. Executed_Gtid_Set: 07cfd8f2-b30c-11e7-8909-000d3a80115c:1-7,
  17. 635227c2-af6c-11e8-a447-5254003471ec:1-297,
  18. 66d902a5-b546-11e7-b1d4-000d3a80115c:1-13,
  19. 79212d47-7122-11e7-8641-0050569f788a:1-17760,
  20. 9d5436f6-7122-11e7-8e0c-0050569f19f6:1-6
  21. Auto_Position: 1
  22. Replicate_Rewrite_DB:
  23. Channel_Name:
  24. Master_TLS_Version:
  25. 1 row in set (0.00 sec)
 

结果报1236的错误,再从主库上查看下主库的状态

 
  1. mysql> show master status \G
  2. *************************** 1. row ***************************
  3. File: mysql-bin.000480
  4. Position: 10751
  5. Binlog_Do_DB:
  6. Binlog_Ignore_DB:
  7. Executed_Gtid_Set: 07cfd8f2-b30c-11e7-8909-000d3a80115c:1-7,
  8. 635227c2-af6c-11e8-a447-5254003471ec:1-297,
  9. 66d902a5-b546-11e7-b1d4-000d3a80115c:1-13,
  10. 79212d47-7122-11e7-8641-0050569f788a:1-17797,
  11. 9d5436f6-7122-11e7-8e0c-0050569f19f6:1-6,
  12. ac29dfbf-aa66-11e8-9d1e-5254003471ec:1
  13. 1 row in set (0.00 sec)
  14.  
  15. mysql> show variables like '%gtid_purged%';
  16. +---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  17. | Variable_name | Value |
  18. +---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  19. | gtid_purged | 07cfd8f2-b30c-11e7-8909-000d3a80115c:1-7,
  20. 635227c2-af6c-11e8-a447-5254003471ec:1-297,
  21. 66d902a5-b546-11e7-b1d4-000d3a80115c:1-13,
  22. 79212d47-7122-11e7-8641-0050569f788a:1-15338,
  23. 9d5436f6-7122-11e7-8e0c-0050569f19f6:1-6,
  24. ac29dfbf-aa66-11e8-9d1e-5254003471ec:1 |
  25. +---------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  26. 1 row in set (0.00 sec)
 

发现有一个GTID从库还没有跑,主库就把这个GTID purged掉了,由于从库没有业务在跑,属于备份库,所以我索性直接

 
  1. mysql> stop slave;
  2. mysql> reset master;
  3.  
  4. mysql> set global gtid_purged = '07cfd8f2-b30c-11e7-8909-000d3a80115c:1-7,
  5. '> 635227c2-af6c-11e8-a447-5254003471ec:1-297,
  6. '> 66d902a5-b546-11e7-b1d4-000d3a80115c:1-13,
  7. '> 79212d47-7122-11e7-8641-0050569f788a:1-15223,
  8. '> 9d5436f6-7122-11e7-8e0c-0050569f19f6:1-6,
  9. '> ac29dfbf-aa66-11e8-9d1e-5254003471ec:1'; #主库的gtid_purged#
  10. mysql> change master to master_host='10.120.141.136',master_user='sys_replication',master_password='x!Jkz@SIe',master_port=3306,master_auto_position=1;
  11. mysql> start slave;
  12. mysql> show slave status \G
  13. *************************** 1. row ***************************
  14. Slave_IO_State: Waiting for master to send event
  15. Master_Host: 10.120.141.136
  16. Master_User: sys_replication
  17. Master_Port: 3306
  18. Connect_Retry: 60
  19. Master_Log_File: mysql-bin.000480
  20. Read_Master_Log_Pos: 10272
  21. Relay_Log_File: mysqld-relay-bin.000034
  22. Relay_Log_Pos: 10285
  23. Relay_Master_Log_File: mysql-bin.000480
  24. Slave_IO_Running: Yes
  25. Slave_SQL_Running: Yes
  26. Replicate_Do_DB:
  27. Replicate_Ignore_DB:
  28. Replicate_Do_Table:
  29. Replicate_Ignore_Table:
  30. Replicate_Wild_Do_Table:
  31. Replicate_Wild_Ignore_Table:
  32. Last_Errno: 0
  33. Last_Error:
  34. Skip_Counter: 0
  35. Exec_Master_Log_Pos: 10272
  36. Relay_Log_Space: 10580
  37. Until_Condition: None
  38. Until_Log_File:
  39. Until_Log_Pos: 0
  40. Master_SSL_Allowed: No
  41. Master_SSL_CA_File:
  42. Master_SSL_CA_Path:
  43. Master_SSL_Cert:
  44. Master_SSL_Cipher:
  45. Master_SSL_Key:
  46. Seconds_Behind_Master: 0
  47. Master_SSL_Verify_Server_Cert: No
  48. Last_IO_Errno: 0
  49. Last_IO_Error:
  50. Last_SQL_Errno: 0
  51. Last_SQL_Error:
  52. Replicate_Ignore_Server_Ids:
  53. Master_Server_Id: 141135
  54. Master_UUID: 79212d47-7122-11e7-8641-0050569f788a
  55. Master_Info_File: mysql.slave_master_info
  56. SQL_Delay: 0
  57. SQL_Remaining_Delay: NULL
  58. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  59. Master_Retry_Count: 86400
  60. Master_Bind:
  61. Last_IO_Error_Timestamp:
  62. Last_SQL_Error_Timestamp:
  63. Master_SSL_Crl:
  64. Master_SSL_Crlpath:
  65. Retrieved_Gtid_Set: 79212d47-7122-11e7-8641-0050569f788a:15224-17793
  66. Executed_Gtid_Set: 07cfd8f2-b30c-11e7-8909-000d3a80115c:1-7,
  67. 635227c2-af6c-11e8-a447-5254003471ec:1-297,
  68. 66d902a5-b546-11e7-b1d4-000d3a80115c:1-13,
  69. 79212d47-7122-11e7-8641-0050569f788a:1-17793,
  70. 9d5436f6-7122-11e7-8e0c-0050569f19f6:1-6,
  71. ac29dfbf-aa66-11e8-9d1e-5254003471ec:1
  72. Auto_Position: 1
  73. Replicate_Rewrite_DB:
  74. Channel_Name:
  75. Master_TLS_Version:
  76. 1 row in set (0.00 sec)
 

总结:

从库在开始同步前,主库会依靠GTID来确认从库在开始同步以后, 能够把每一个主库上执行过的事务(包括slave的SQL Thread)都复现一次,最终保持和主库完全一致;
判断方法也很简单,基本基于两个条件:
1.主库不能purge从库还没有execute的事务(即从库的executed_GTID要大于主库的GTID_Purged);
2.主库上的事务号不能低于从库(即从库的executed_GTID的最后一个事务要在主库的executed_GTID的范围之内);

2、 构架为双主(一主一从,且互为主从),业务和应用在主库上跑,从库做备份,基本没有业务和应用。

从库(s)指向主库(m)时连接良好,主库(m)指向从库(s)时报错1236。

 
  1. mysql> show slave status \G #主库(m)状态
  2. *************************** 1. row ***************************
  3. Slave_IO_State:
  4. Master_Host: 10.120.141.168
  5. Master_User: sys_repl
  6. Master_Port: 3306
  7. Connect_Retry: 10
  8. Master_Log_File:
  9. Read_Master_Log_Pos: 4
  10. Relay_Log_File: mysqld-relay-bin.000001
  11. Relay_Log_Pos: 4
  12. Relay_Master_Log_File:
  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: 0
  25. Relay_Log_Space: 154
  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: 1236
  38. Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'The slave is connecting using CHANGE MASTER TO MASTER_AUTO_POSITION = 1, but the master has purged binary logs containing GTIDs that the slave requires.'
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:
  41. Replicate_Ignore_Server_Ids:
  42. Master_Server_Id: 141168
  43. Master_UUID: 055a9521-4906-11e8-8cdb-0050569f3621
  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: 181119 14:27:58
  51. Last_SQL_Error_Timestamp:
  52. Master_SSL_Crl:
  53. Master_SSL_Crlpath:
  54. Retrieved_Gtid_Set:
  55. Executed_Gtid_Set: 055a9521-4906-11e8-8cdb-0050569f3621:1-164859,
  56. 2f7211a3-fba0-11e5-b668-0050569f3621:1-627,
  57. 30d4f3f6-b56b-11e7-acf1-000d3a801c2f:1-14,
  58. 375942c7-0723-11e6-b55c-0050569f3621:1-16630,
  59. 61edd40b-af6c-11e8-a4f6-525400adeb6d:1-5059,
  60. 971844d6-d7ca-11e6-8d01-0050569f6058:1-11929269,
  61. d408633e-fb9f-11e5-8de2-0050569f6058:1-1317354
  62. Auto_Position: 1
  63. Replicate_Rewrite_DB:
  64. Channel_Name:
  65. Master_TLS_Version:
  66. 1 row in set (0.00 sec)
 

再看从库(s)的gtid_purged

 
  1. mysql> show variables like '%gtid%purged%'\G
  2. *************************** 1. row ***************************
  3. Variable_name: gtid_purged
  4. Value: 055a9521-4906-11e8-8cdb-0050569f3621:1-157766,
  5. 2f7211a3-fba0-11e5-b668-0050569f3621:1-627,
  6. 30d4f3f6-b56b-11e7-acf1-000d3a801c2f:1-14,
  7. 375942c7-0723-11e6-b55c-0050569f3621:1-16633,
  8. 971844d6-d7ca-11e6-8d01-0050569f6058:1-9581086,
  9. d408633e-fb9f-11e5-8de2-0050569f6058:1-1317354
  10. 1 row in set (0.00 sec)
 

发现由于从库(s)的 gtid_purged大于主库(m)的Executed_Gtid  #从库指向主库的结构已经搭建完成,现在是搭建主库指向从库时报错,即当前主是从#

根据之前总结的规则,主库(s)不能purge从库(m)还没有execute的事务(即从库(m)的executed_GTID要大于主库(s)的GTID_Purged)

所以会报1236的错误。由于主库(m)上还有业务和应用在跑,所以不能生硬的reset master,所以只能想办法把execunt gtid追回来,

我的方法是跳过这三个事务(不是唯一解法,如果差的事务号过多,这个办法就很愚蠢,在这个构架下出现这种错误很有可能在从库上有应用执行过事务,

如果从库执行的事务太多,那就要查查原因了)

 
  1. stop slave
  2. set gtid_next='375942c7-0723-11e6-b55c-0050569f3621:16631'; --指定下一个事务执行的版本,即想要跳过的GTID
  3. begin;
  4. commit; --注入一个空事物
  5. set gtid_next='375942c7-0723-11e6-b55c-0050569f3621:16632';
  6. begin;
  7. commit;
  8. set gtid_next='375942c7-0723-11e6-b55c-0050569f3621:16633';
  9. begin;
  10. commit;
  11.  
  12. set gtid_next='AUTOMATIC'; --自动的寻找GTID事务。
  13.  
  14. start slave; --开始同步
  15.  
  16. mysql> show slave status \G
  17. *************************** 1. row ***************************
  18. Slave_IO_State: Waiting for master to send event
  19. Master_Host: 10.120.141.168
  20. Master_User: sys_repl
  21. Master_Port: 3306
  22. Connect_Retry: 10
  23. Master_Log_File: mysql-bin.000026
  24. Read_Master_Log_Pos: 185245755
  25. Relay_Log_File: mysqld-relay-bin.000002
  26. Relay_Log_Pos: 414
  27. Relay_Master_Log_File: mysql-bin.000026
  28. Slave_IO_Running: Yes
  29. Slave_SQL_Running: Yes
  30. Replicate_Do_DB:
  31. Replicate_Ignore_DB:
  32. Replicate_Do_Table:
  33. Replicate_Ignore_Table:
  34. Replicate_Wild_Do_Table:
  35. Replicate_Wild_Ignore_Table:
  36. Last_Errno: 0
  37. Last_Error:
  38. Skip_Counter: 0
  39. Exec_Master_Log_Pos: 185245755
  40. Relay_Log_Space: 622
  41. Until_Condition: None
  42. Until_Log_File:
  43. Until_Log_Pos: 0
  44. Master_SSL_Allowed: No
  45. Master_SSL_CA_File:
  46. Master_SSL_CA_Path:
  47. Master_SSL_Cert:
  48. Master_SSL_Cipher:
  49. Master_SSL_Key:
  50. Seconds_Behind_Master: 0
  51. Master_SSL_Verify_Server_Cert: No
  52. Last_IO_Errno: 0
  53. Last_IO_Error:
  54. Last_SQL_Errno: 0
  55. Last_SQL_Error:
  56. Replicate_Ignore_Server_Ids:
  57. Master_Server_Id: 141168
  58. Master_UUID: 055a9521-4906-11e8-8cdb-0050569f3621
  59. Master_Info_File: mysql.slave_master_info
  60. SQL_Delay: 0
  61. SQL_Remaining_Delay: NULL
  62. Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
  63. Master_Retry_Count: 86400
  64. Master_Bind:
  65. Last_IO_Error_Timestamp:
  66. Last_SQL_Error_Timestamp:
  67. Master_SSL_Crl:
  68. Master_SSL_Crlpath:
  69. Retrieved_Gtid_Set:
  70. Executed_Gtid_Set: 055a9521-4906-11e8-8cdb-0050569f3621:1-164859,
  71. 2f7211a3-fba0-11e5-b668-0050569f3621:1-627,
  72. 30d4f3f6-b56b-11e7-acf1-000d3a801c2f:1-14,
  73. 375942c7-0723-11e6-b55c-0050569f3621:1-16633,
  74. 61edd40b-af6c-11e8-a4f6-525400adeb6d:1-5059,
  75. 971844d6-d7ca-11e6-8d01-0050569f6058:1-11929305,
  76. d408633e-fb9f-11e5-8de2-0050569f6058:1-1317354
  77. Auto_Position: 1
  78. Replicate_Rewrite_DB:
  79. Channel_Name:
  80. Master_TLS_Version:
  81. 1 row in set (0.00 sec)
 

这样就好了。

mysql主从不同步问题 Error_code: 1236的更多相关文章

  1. mysql主从不同步问题 Error_code: 1197

    首先查看从的状态   mysql> show slave status \G *************************** 1. row *********************** ...

  2. MySQL主从数据库同步延迟问题解决(转)

    最近在做MySQL主从数据库同步测试,发现了一些问题,其中主从同步延迟问题是其中之一,下面内容是从网上找到的一些讲解,记录下来以便自己学习: MySQL的主从同步是一个很成熟的架构,优点为:①在从服务 ...

  3. 配置MySQL主从双向同步

    原文地址:http://www.cnblogs.com/zhongshengzhen/ 原主数据库:192.168.137.33 原从数据库:192.168.137.197   需要先阅读并操作:ht ...

  4. MYSQL主从库同步配置过程

    MYSQL主从库同步配置过程 为了实现网站数据库的异地备份,采用了MySQL数据库主从同步配置,需要两台服务器分别作为主从库,当主库发生增删改等操作,会实时反映到从库,我的个人服务器配置如下: 主库为 ...

  5. Centos 配置 Mysql 主从双向同步

    配置之前,请先阅读mysql主从复制: Mysql-主从复制 原:  主从环境: 主服务器:192.168.153.130 从服务器:192.168.153.131 1.从数据库创建同步用户,将主数据 ...

  6. mysql主从不同步处理过程分享

    背景  8月7日15:58收到报障数据库出现不同步:数据库共四台,分别为10.255.70.11,10.255.70.12,10.255.70.13,10.255.70.14(ip为虚拟ip) 数据库 ...

  7. 使用Percona Toolkit解决Mysql主从不同步问题【备忘】

    由于各种原因,mysql主从架构经常会出现数据不一致的情况出现,大致归结为如下几类 1:备库写数据 2:执行non-deterministic query 3:回滚掺杂事务表和非事务表的事务 4:bi ...

  8. mysql 主从 重新同步

    mysql 主从同步一担出了问题之后,就会导致从库上的数据和主库不一样了.所以需要生新同步数据. 1.登录主库服务器,进入mysql,命令为:mysql -uroot -ppassword 2.执行: ...

  9. 解决mysql 主从数据库同步不一致的方法

    接着上文 配置完Mysql 主从之后,在使用中可能会出现主从同步失败的情况. mysql> show slave status\G Slave_IO_Running: Yes Slave_SQL ...

随机推荐

  1. 复刻smartbits的国产网络测试工具minismb功能特点-如何加载、发送PCAP数据包

    复刻smartbits的网络性能测试工具minismb,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太网测试工具测试任何ip网络设备的端口吞吐率,带宽,并发 ...

  2. LVS+Heartbeat 高可用集群方案操作记录

    之前分别介绍了LVS基础知识和Heartbeat基础知识, 今天这里简单说下LVS+Heartbeat实现高可用web集群方案的操作说明. Heartbeat 项目是 Linux-HA 工程的一个组成 ...

  3. 程序员必知的8大排序(一)-------直接插入排序,希尔排序(java实现)

    http://blog.csdn.net/pzhtpf/article/details/7559896 程序员必知的8大排序(一)-------直接插入排序,希尔排序(java实现) 程序员必知的8大 ...

  4. LVS持久化与超时时间问题分析

    前言 在上一篇文章<搭建DNS+LVS(keepAlived)+OpenResty服务器(Docker环境)>中,我搭建了dns+lvs+openresty+web集群:先来回顾一下架构图 ...

  5. 并发编程之 Condition 源码分析

    前言 Condition 是 Lock 的伴侣,至于如何使用,我们之前也写了一些文章来说,例如 使用 ReentrantLock 和 Condition 实现一个阻塞队列,并发编程之 Java 三把锁 ...

  6. 并发编程之 线程协作工具 LockSupport

    前言 在前面的文章中,我们介绍了并发工具中的4个,Samephore,CyclicBarrier,CountDownLatch,Exchanger,但是我们漏了一个,非常的好用的工具,楼主在这里必须加 ...

  7. 设计模式学习总结(一)——设计原则与UML统一建模语言

    一.概要 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设计经验的总结. 使用设计模式的目的:为了代码可重用性.让代码更容易被他人理解.保证代码可靠性. 设计 ...

  8. ikanalyzer分词,计算信息熵排序分词结果

    因需求,现需分词接口,故记录之. 1.需要依赖: <!-- https://mvnrepository.com/artifact/com.janeluo/ikanalyzer --> &l ...

  9. Netty的基本概念

    异步 等待它的同时你也可以做点别的事情 阻塞I/O 只能同时处理一个连接,要管理多个并发客户端,需要为每个新的客户端Socket创建一个新的Thread 使用Selector的非阻塞I/O class ...

  10. Java - List总结

    Java提高篇(三二)-----List总结 前面LZ已经充分介绍了有关于List接口的大部分知识,如ArrayList.LinkedList.Vector.Stack,通过这几个知识点可以对List ...