首页 > 数据库 >MySQL 主从复制,常见的binlog错误及解决方法

MySQL 主从复制,常见的binlog错误及解决方法

时间:2024-07-30 10:52:07浏览次数:8  
标签:binlog 主从复制 slave log relay mysql master MySQL

在主从复制架构下,MySQL通过binlog来实现主从数据的一致性。

 

MySQL主从复制主要有以下步骤

1. master将改变记录到binary log中

2. slave io_thread去请求主库的binlog,并将得到的binlog日志写到relay log中

3. slave sql_thread重做relay log中的事件

 

除了作为MySQL主从复制的纽带,binlog还有其他的作用。比如:

1. 通过mysqlbinlog工具解析binlog文件,来进行Point-in-Time数据库恢复;

2. 基于binlog事件,进行数据库的flashback(闪回)(mariadb可以直接使用mysqlbinlog进行flashback)

3. Github开源的在线改表工具gh-ost也是通过binlog来实现的

4. 还可以通过解析binlog进行增量订阅&消费

 

binlog如此有用,但是在平日的运维过程中难免遇到一些问题。下面介绍几种binlog相关的错误。

 

常见问题之一

 

现象:  mysqlbinlog5.5解析mysql5.7 binlog文件出现

ERROR: Error in Log_event::read_log_event(): 'Sanity check failed', data_len: 31, event_type: 35

ERROR: Could not read entry at offset 123: Error in log format or read error.

 

原因分析

** mysql5.6等高版本binlog文件增加了新的binlog event,如gtid event等。

** mysql5.5版本的mysqlbinlog是识别不了这样的binlog event的。

 

解决方法:使用高版本的mysqlbinlog解析低版本的mysql产生的binlog

 

常见问题之二

 

现象:正常运行的mysql服务器show slave status出现

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.

 

原因分析:无法读取relay log 里的条目,可能因为master库的binlog错误,或slave库的中继日志错误,或者网络问题及bug原因。一般是由于网络故障或slave库压力过大,导致relay-log格式错误造成的。

 

解决方法:

找到当前已经同步的时间点,重新设置主从同步后,就会产生新的中继日志,恢复正常。

 

从"show  slave  status\G"的输出中,找到如下信息:

Relay_Master_Log_File: mysql-bin.002540     //slave库已读取的master的binlog

Exec_Master_Log_Pos: 950583017             //在slave上已经执行的position位置点

 

停掉slave,以slave已经读取的binlog文件,和已经执行的position为起点,重新设置同步。

Relay_Master_Log_File: mysql-bin.002540     //slave库已读取的master的binlog

Exec_Master_Log_Pos: 950583017             //在slave上已经执行的position位置点

 

 

常见问题之三

 

现象:宕机之后恢复show slave status报错:

Last_SQL_Error: Error initializing relay log position: I/O error reading the header from the binary log

Last_SQL_Error: Error initializing relay log position: Binlog has bad magic number; It's not a binary log file that can be used by this version of MySQL

 

原因分析:宕机,例如电源故障、主板烧了等,或者非法关机,造成relay-bin文件损坏

 

解决方法:同问题二 。

也可以设置relay_log_recovery = 1 。

当slave从库宕机后,如果relay-log发生损坏,导致一部分中继日志没有处理,就自动放弃未执行的relay-log,重新从master上获取日志,完成了中继日志的恢复。

 

常见问题之四

 

现象:从库机器宕机重启后change master to 时出现Error (Code 1201): Could not initialize master info structure; more error messages can be found in the MySQL error log

      或者ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository

 

原因分析:宕机,例如电源故障、主板烧了等,或者非法关机,造成master.info或者realy-log.info文件损坏

 

解决方法:slave> reset slave all,重新change master to

 

预防措施:

(1)配置文件设置

relay_log_info_repository=table

master_info_repository=table

(2)

mysql5.6.5以前的版本mysql.slave_master_info和mysql.slave_relay_log_info的存储引擎是默认为MyISAM的,需要更改为InnoDB的存储引擎

ALTER TABLE mysql.slave_master_info ENGINE=InnoDB;

ALTER TABLE mysql.slave_relay_log_info ENGINE=InnoDB;

 

mysql.slave_master_info    表将在sync_master_info 个events后被更新 。

mysql.slave_relay_log_info 表将在每个事务commit时被更新 。

 

常见问题之五

 

现象:主从原来binlog_format都是statement,将主库binlog_format改为row后,从库show slave status出现:

      Last_Error: Error executing row event: 'Cannot execute statement: impossible to write to binary log since statement is in row format and BINLOG_FORMAT = STATEMENT.'

 

原因分析:主库binlog_format是row,从库binlog_format是statement时,会出现上面的报错 。

 

但是主库binlog_format是statement,从库binlog_format是row;或者主库binlog_format是row,从库binlog_format是mixed就不会报错。

 

If the your SQL thread is indeed configured with binlog_format=STATEMENT once it receives a ROW event it will stop.

The reason is that it would be unable to log that ROW event in STATEMENT format (sometimes we refer to this as ROW injection, which is either a BINLOG statement or a ROW event executed by the slave's SQL thread)

详细原因参考:https://bugs.mysql.com/bug.php?id=69095

 

解决方法:

SLAVE> STOP SLAVE;

SLAVE> SET GLOBAL binlog_format=MIXED;

SLAVE> START SLAVE;

 

常见问题之六

 

现象:mysql5.6同步mysql5.5时出错

 

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.000001' at 4, the last event read from 'mysql-bin.000001' at 120, the last byte read from 'mysql-bin.000001' at 120.'

 

原因分析:为了解决软硬件或者网络传输出错,导致主服务器上运行的sql语句与从服务器上运行的sql语句不一致(称为event corrupt)的问题,mysql5.6版本添加了replication event checksum功能 。当一个event被写入binary log的时候,checksum也同时写入binary log,然后在event通过网络传输到slave之后,再在slave上对其进行验证并写入slave的relay log 。由于每一步都记录了event和checksum,所以我们可以很快地找出问题所在。

 

mysql5.6.5以后的版本中binlog_checksum默认值是crc32,而之前的版本binlog_checksum默认值是none

 

解决方案:

Slave> set global binlog_checksum=none

 

常见问题之七

 

现象:磁盘满了,手动清理binlog文件和mysql-bin.index文件后,

show binary logs为空,但是show master status正常。

 

mysql> show binary logs;

Empty set (0.00 sec)

mysql> show master status;

 

+------------------+-----------+--------------+------------------+

 

| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |

 

+------------------+-----------+--------------+------------------+

 

| mysql-bin.001385 | 987114584 |              |                  |

 

+------------------+-----------+--------------+------------------+

 

原因分析:检查mysql-bin.index文件后,发现第一行空行 。解法很简单,删了第一行的空行,然后 flush binary logs 生成新的 index 文件把 cache 失效掉,就可以了。

 

在mysql源码rpl_master.cc:show_binlogs()有如下代码:

/* The file ends with EOF or empty line */

while ((length=my_b_gets(index_file, fname, sizeof(fname))) > 1)

空行被认为是文件结束

 

预防措施:不要人工去删除binlog,不要人工编辑mysql-bin.index文件,除非你知道你在干什么,否者你就可能在给自己埋雷!

 

总结:DBA需要关注MySQL的每个新版本对binlog都有那些改进(比如5.6版本添加的gtid 特性,5.7版本Enhanced Multi-threaded Slaves ) ,详细了解每个参数的含义,这样遇到错误知其意,解决问题才能顺手。

 

标签:binlog,主从复制,slave,log,relay,mysql,master,MySQL
From: https://www.cnblogs.com/wuchangsoft/p/18331855

相关文章

  • 十分钟教你学会 MySQL字符串处理
    CONCAT函数在Java语言中,我们经常会对字符串进行处理,在ySQL中,我们同样也有这样的函数来处理字符串,这节课我们来学习CONCAT函数。语法我们来看一下concat函数的语法:SELECTcolumn_name1,CONCAT(column_name2,str,column_name3),column_name4FROMtable_name;我们来分析......
  • 简单认识MySQL存储引擎
    MySQL体系结构连接层。一些客户端和链接服务,主要完成如连接处理、授权认证及选相关的安全方案服务层。主要完成大多数的核心服务功能,比如SQL接口,缓存的查询,SQL分析和优化,部分内置函数的执行。所以跨存储引擎的功能在这一层实现,如存储过程、函数等。引擎层。真正负责DBMS中数据......
  • MySQL 基础语法教程
    MySQL是一个开源的关系型数据库管理系统,它广泛应用于各种网站和应用程序中。本文将介绍MySQL的基础语法,包括数据库和表的创建、数据操作、索引和约束等内容。1.基本概念在MySQL中,数据库用于存储数据,表是数据库中的基本单位。了解这些基本概念是使用MySQL的第一步......
  • 只讲干货!!数据库的裤腰带!!今天拿下:MySQL中的约束!!
    MySQL中的约束约束概述        数据库约束是对表中的数据进行进一步的限制,保证数据的正确性、有效性和完整性主键约束(PrimaryKey)PK        主键约束是使用最频繁的约束。在设计数据表时,一般情况下,都会要求表中设置一个主键。        ......
  • mysql中substring_index类似split分组功能
     这条MySQL语句中使用了substring_index函数来处理training_pictures列的数据。下面是该函数的具体用法:substring_index(str,delim,count):这个函数会返回字符串str中第count个出现的分隔符delim之前的所有字符,或者之后的所有字符(取决于count的正负)。具体到你提供的查询:s......
  • mysql的主从复制和读写分离
    目录主从复制主从复制的模式1.异步模式2.全同步模式3.半同步模式主从复制的延迟主从复制的延迟的原因解决方案双一设置性能化设置主从复制如何实现安装步骤读写分离实验步骤面试题主从复制主从复制的模式1.异步模式mysql的默认模式:主库在更新完事务之后会......
  • mysql的MHA以及故障模拟
    目录MHA概念MHA的组件MHA的特点实验:搭建完成MHA的架构实验:主备切换实验结果实验:故障切换实验:故障恢复MHA概念MHA:高可用模式下的故障切换,基于主从复制。它解决的是单点故障和主从复制不能切换的问题。它至少需要3台。故障切换过程0-30秒。它能根据VIP地址所在的主机......
  • MySQL 性能调优
    文章目录一.MySQL调优金字塔1.架构调优2.MySQL调优3.硬件和OS调优4.小结二.查询性能调优三.慢查询1.概念2.优化数据访问3.请求了不需要数据,怎么做4.是否在扫描额外的记录5.慢查询相关配置&日志位置6.小结四.查询优化器五.实现调优手段一.MySQL调......
  • Windows11安装MySQL8.4.2版本详细过程记录
    下载地址:https://dev.mysql.com/downloads/mysql/8.0.html我选择下载zip版本:点击下载需要登录:于是我登录:接着点下载:被迅雷拦截了,直接使用迅雷下载:下载好了:复制到C盘的dev目录:安装解压:这个看上去需要一些基础命令才能操作:于是我重新下载了这个msi版本......
  • MySQL数据库基础操作与概念详解(三)
    DML和DQL语句1.新增–INSERTINTO表名(字段名,字段名,…字段名)values/value(值,值,…值)–日期使用字符串的形式进行书写日期格式(yyyy-MM-ddHH-dd)1.全字段的输入(1)方式一INSERTINTOstudent(sid,sname,birthday,ssex,classid)VALUES(9,‘张三’,‘2002-9-23’,‘......