前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL 5.6 主从报错一例

MySQL 5.6 主从报错一例

作者头像
老叶茶馆
发布2021-03-30 15:12:24
3650
发布2021-03-30 15:12:24
举报

作者:八怪(高鹏) 中亦科技数据库专家

1. 问题现象

版本:MySQL 5.6,采用传统 binlog file & pos 方式配置的主从复制结构。

实例重启后,主从复制报错如上图所示。

2. 错误含义

错误分为2部分。

第一部分

  • Client requested master to start replication from position > file size;
  • the first event 'mysql-bin.000398' at 163800795,the last event read from './mysql-binlog.000398' at 4,the last byte read from './mysql-bin.000398' at 4'
第一部分

这部分来源于主库的DUMP线程函数

代码语言:javascript
复制
mysql_binlog_send
  ->sender.run()
    ->Binlog_sender::init
       ->Binlog_sender::check_start_file

  if ((file= open_binlog_file(&cache, m_linfo.log_file_name, &errmsg)) < 0) 
  {
    set_fatal_error(errmsg);
    return 1;
  }

  size= my_b_filelength(&cache);
  end_io_cache(&cache);
  mysql_file_close(file, MYF(MY_WME)); 

  if (m_start_pos > size)
  {
    set_fatal_error("Client requested master to start replication from "
                    "position > file size");
    return 1;
  }

关键就是m_start_pos和size两个值,其中m_start_pos来源于从库需要读取的位点。而size则是本binlog文件的大小,那么很容易理解如果io线程需要的pos点比本binlog文件的大小还要大,那么自然不对。

第二部分

这部分也来源于DUMP线程

代码语言:javascript
复制
mysql_binlog_send
  ->sender.run()
     ->Binlog_sender::init
     ->while (!has_error() && !m_thd->killed)
     #如果正常这里开始循环读取binlog event,如果前面出错则直接继续后面逻辑
     #如果有读取错误则报错
       my_snprintf(error_text, sizeof(error_text),
                  "%s; the first event '%s' at %lld, "
                  "the last event read from '%s' at %lld, "
                  "the last byte read from '%s' at %lld.",
                  m_errmsg,
                  m_start_file, m_start_pos, m_last_file, m_last_pos,
                  log_file, my_b_tell(&log_cache));

这里我们主要看看m_start_pos和m_last_pos,实际上m_start_pos就是和前面报错一致的来自从库需要读取的位点信息,而m_last_pos来自dump线程,就是最后读取的位置,显然这里一次都没有读取,因此位置为最开始的pos 4。

3. 可能的原因

分析后觉得最有可能原因应该和sync_binlog 有关。

如果我们没有设置为1,那么可能os cache没有刷盘,如果主库服务器直接crash重启很容易就遇到这种问题。

稍微google查询了一下发现很大部分出现这种错误都是由于服务器crash且sync_binlog 没设置为 1导致的。

这也证明我们的说法。

最后查看问题数据库的主库确实没有设置为双1。

那么通过这个小案例,我们已经更加深刻体会到设置双1的重要性。

全文完。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-03-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 老叶茶馆 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 问题现象
    • 2. 错误含义
      • 3. 可能的原因
      相关产品与服务
      云数据库 SQL Server
      腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档