MySQL高可用架构中,主库复制是非常常见的一种。

当主库宕机后,可以提升一个从库作为新的主库,保证服务可用性;同时可以通过扩展从库,提高整个集群的QPS。

在主从复制架构下,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: 35ERROR: 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的binlogExec_Master_Log_Pos: 950583017          //在slave上已经执行的position位置点

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

Relay_Master_Log_File: mysql-bin.002540     //slave库已读取的master的binlogExec_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

预防措施

配置文件设置

relay_log_info_repository=table
master_info_repository=table

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 STATEMENTformat (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文件后,发现第一行空行 。

在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)

空行被认为是文件结束

(参考https://yq.aliyun.com/articles/213657文章)

 

预防措施

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

总结

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

阅读原文

Binlog中最容易踩到的坑的更多相关文章

  1. js/jQuery使用过程中常见问题/已踩过的坑大杂烩

    目录 一.jQuery选择器选择选中的或者disabled的选择框时attr函数无效 二.jQuery each函数的break/continue 三.jQuery 获取元素的left会值/left数 ...

  2. 记录一个微信网页授权中不小心踩到的坑(Curl请求返回false)

    原文章地址在这里 这个问题是file_get_contents不能获取https的内容引起的.这样的情况下,我们一般会采用curl拓展来模拟请求. 代码demo(当然这是错误的示范): functio ...

  3. vue中eventbus被多次触发(vue中使用eventbus踩过的坑)【bus.$on事件被多次绑定】

    问题描述:只要页面没有强制刷新,存在组件切换,bus.$on方法会被多次绑定,造成事件多次触发   触发bus.$on中绑定的方法.png   bus.$on多次绑定.png 解决办法:在每次调用方法 ...

  4. 最近C#项目中不小心踩的低级坑

    都是很基础的错误问题,大部分都是因为不查一下资料就直接根据其它类似语言的经验写代码导致的 1. 一个企业微信上的正常的界面突然不能滚动了 本以为是浏览器代码计算问题,结果发现是JS出错导致. 2. R ...

  5. 项目中踩过的坑之-sessionStorage

    总想写点什么,却不知道从何写起,那就从项目中踩过的坑开始吧,希望能给可能碰到相同问题的小伙伴一点帮助. 项目情景: 有一个id,要求通过当前网页打开一个新页面(不是当前页面),并把id传给打开的新页面 ...

  6. 使用ffmpeg视频编码过程中踩的一个坑

           今天说说使用ffmpeg在写视频编码程序中踩的一个坑,这个坑让我花了好多时间,回头想想,非常多时候一旦思维定势真的挺难突破的.以下是不对的编码结果:                   ...

  7. Java 开发中如何正确踩坑

    为什么说一个好的员工能顶 100 个普通员工 我们的做法是,要用最好的人.我一直都认为研发本身是很有创造性的,如果人不放松,或不够聪明,都很难做得好.你要找到最好的人,一个好的工程师不是顶10个,是顶 ...

  8. git工作中常用命令-工作中踩过的坑

    踩坑篇又来啦,这是我在工作中从git小白进化到现在工作中运用自如的过程中,踩过的坑,以及解决办法. 1.基于远程develop分支,建一个本地task分支,并切换到该task分支 git checko ...

  9. ng-zorro-antd中踩过的坑

    ng-zorro-antd中踩过的坑 前端项目中,我们经常会使用阿里开源的组件库:ant-design,其提供的组件已经足以满足多数的需求,拿来就能直接用,十分方便,当然了,有些公司会对组件库进行二次 ...

随机推荐

  1. 【Linux】命令写在文件中并调用awk -f

    我们在使用awk命令的时候,有时候命令特别长,在终端写出来格式太乱,难以阅读,以下是一个将命令写在文件中,并使用awk调用的具体案例 1.现在有文件file3.txt,内容如下: 2.ak2.awk脚 ...

  2. 取某字段不为空的数据is not null

    SELECT * FROM 表名  where  uid='xxx'  and  time  is not null

  3. AJAX的同步返回结果值

    function makeJQGridDataFromList(url) {     var rowData;     var viewPage = 0;     var viewTotal = 0; ...

  4. 【laravel5.4】安装predis

    1.服务器先安装redis-server,这是毋庸置疑的!!! 2.服务器开启redis-server,配置相关参数 3.laravel执行 : $  yourPhpPath composer.pha ...

  5. 基于24位AD转换模块HX711的重量称量实验(已补充皮重存储,线性温度漂移修正)

    转载:http://www.geek-workshop.com/thread-2315-1-1.html 以前在X宝上买过一个称重放大器,180+大洋.原理基本上就是把桥式拉力传感器输出的mV级信号放 ...

  6. Windows Server 2008中安装IIS7.0

    最近由于需求重新部署了一台服务器Windows Server 2008,由于以前都是在Windows Server 2003上操作,因此记录下,供其他同学参考.   下面主要介绍在Windows Se ...

  7. windows Server 2012安装GUI

    1.进入PowerShell 在命令行提示输入符处,直接输入: PowerShell 2.安装GUI 在命令行提示输入符处,直接输入: Install-WindowsFeature Server-Gu ...

  8. State Threads 回调终结者

    上回写了篇<一个“蝇量级”C语言协程库>,推荐了一下Protothreads,通过coroutine模拟了用户级别的multi-threading模型,虽然本身足够“轻”,杜绝了系统开销, ...

  9. 条件触发和边缘触发 及 epoll 的长处

    条件触发: 仅仅要输入缓冲有数据就会一直通知该事件 边缘触发: 输入缓冲收到数据时仅注冊1次该事件.即使输入缓冲中还留有数据,也不会再进行注冊 水平触发(level-triggered.也被称为条件触 ...

  10. SharePoint下在Feature中动态Register/Remove HttpModule

    在SharePoint开发时,你会遇到这样一个问题,Global.asax去哪儿?怎样添加一个Global.asax?怎样在Application_Start这个事件处理程序里设置初始化?似乎在Vis ...