MySQL主从复制常见故障排除

一、简介

主从复制原理
主从复制的原理本站的其他文章已经介绍得很详细了,这里不再赘述。简单概况一下就是:从端服务器获取主端服务器的操作日志,并对其进行解析,再在从端复现同样的操作,从而达到同步的目的。
生产环境中为了保证系统的兼容与稳定,在搭建服务器时就应该配置主从复制,主从服务器中的MySQL版本尽量一致。但有时难以保证版本的一致,或者在学习过程中遇到版本不兼容的问题,就容易遇到各种奇葩的故障。本文介绍了两例故障和解决方案,供各位朋友参考学习。
注:首先需保证主端服务器与从端服务器可以ping通,主从配置文件正确无误。

二、故障一

Got fatal error 1236 from master when reading data from binary log:
Slave can not handle replication events with the checksum that master is configured to log;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.61.1
                  Master_User: jeffery
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 453
               Relay_Log_File: hadoop1-relay-bin.000002
                Relay_Log_Pos: 150
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: No
            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: 453
              Relay_Log_Space: 308
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Slave can not handle replication events with the checksum that master is configured to log; the first event 'mysql-bin.000009' at 453, the last event read from '.\mysql-bin.000009' at 123, the last byte read from '.\mysql-bin.000009' at 123.'
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

1. 故障分析:

错误描述的大致意思是从端服务器在拷贝主端日志时,发生了校验错误。这类故障多发生在主端高版本MySQL向从端低版本MySQL复制的场景下。

2. 故障解决:

最直接的解决方式是关闭主端的校验:
方法一:设置mysql5.5的set global binlog_checksum=NONE;
在这里插入图片描述
方法二:修改到my.cnf (win下的my.ini)文件中的binlog_checksum=NONE
在这里插入图片描述
之后保存关闭即可,注意win下保存注意编码集必须为ANSI,否则MySQL就启动不起来了。
之后再次配置主从复制,show slave status;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.61.1
                  Master_User: jeffery
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 441
               Relay_Log_File: hadoop1-relay-bin.000002
                Relay_Log_Pos: 269
        Relay_Master_Log_File: mysql-bin.000011
             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: 441
              Relay_Log_Space: 427
              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: 1
1 row in set (0.00 sec)

这是最直接的解决方式。当然最根本、最万能的解决方式就是在从端服务器安装与主端版本相同的MySQL。

三、故障二

Error: 1594
Relay log read failure: Could not parse relay log event entry.

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.61.1
                  Master_User: jeffery
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000011
          Read_Master_Log_Pos: 620
               Relay_Log_File: hadoop1-relay-bin.000002
                Relay_Log_Pos: 269
        Relay_Master_Log_File: mysql-bin.000011
             Slave_IO_Running: Yes
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 1594
                   Last_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 441
              Relay_Log_Space: 606
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 1594
               Last_SQL_Error: Relay log read failure: Could not parse relay log event entry. The possible reasons are: the master's binary log is corrupted (you can check this by running 'mysqlbinlog' on the binary log), the slave's relay log is corrupted (you can check this by running 'mysqlbinlog' on the relay log), a network problem, or a bug in the master's or slave's MySQL code. If you want to check the master's binary log or slave's relay log, you will be able to know their names by issuing 'SHOW SLAVE STATUS' on this slave.
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
1 row in set (0.00 sec)

1. 故障分析:

这一次的故障就相对棘手了,系统定位了多种故障可能:主端服务器日志崩溃、网络问题、主端或从端MySQL语句中的 bug。
笔者参考了许多相关技术文章,如下:
https://blog.csdn.net/shaochenshuo/article/details/60574721
http://blog.itpub.net/30221425/viewspace-2107638/
首先感谢上面大神的无私分享,但是我按照文章中的方法执行后,仍然没有起色,那就只好拿出杀手锏 —— 版本统一。

2. 故障解决:

当所有解决方法都尝试并无济于事,只能采取最简单粗暴的方式 —— 在从端服务器安装与主端版本相同的MySQL。笔者通过在从端服务器安装相同版本的MySQL,并配置主从复制,最终实现了主从服务器的同步复制。

四、结论

生产环境中为了保证系统的兼容与稳定,在搭建服务器时就应该配置主从复制,且主从服务器中的MySQL版本尽量保持一致,避免将来不必要的麻烦。

发布了5 篇原创文章 · 获赞 8 · 访问量 220

猜你喜欢

转载自blog.csdn.net/weixin_44512041/article/details/104338557