MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

1.场景描述

早上7:25 接到Report中心同学告警,昨天业务报表数据没有完整跑出来,缺少500位业务员的数据,并且很快定位到,缺少的是huabei_order库上的数据。Report中心的数据是BI每天5:15从huabei_order库的从库上抽取。查看监控告警,从库实例确实在4:50 –6:00 有延迟,但已恢复。报表数据不完整, 直接原因就是主从延迟

7:50 确认主从延迟已恢复,请求BI重新抽取数据,重新生成报表。

8:20 报表数据验证无误。

问题还没结束:

忽视监测警报是不及时和不完整的。

[En]

Ignoring the monitoring alarm is not timely and incomplete.

还需要回答的问题是为什么在业务低峰期会出现延迟?是什么操作造成的?【问题1】

监控界面主从延迟指标

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

看到这样的走势图,又一个需要思考的问题: 为什么延迟会瞬间飙升到24440s以上(实际可能已达25K以上),延迟不应该是逐渐累积的吗?【问题2】

确实是业务低峰期,和研发、产品、业务确认,23:30 之后没有大的操作、大的流量,也没有特别的活动、促销。从数据库Server业务压力上考虑,确实没有特殊的操作。

忽然想到,备份,完整备份。我们的灾备,数据库的完整备份实在从库上做的,cron 设置在每天的00:05 , 一般执行2个小时, 并且是周二、周五执行,当天正好是周二。

虽然自己也不太相信这个就是真正的原因,但毕竟是一个可考虑的因素。查看完整备份的log。

2.全量备份

失败

报错信息如下:

210831 04:49:46 >> log scanned up to (1168680308582)
210831 04:49:47 >> log scanned up to (1168680308668)
210831 04:49:48 >> log scanned up to (1168680308770)
210831 04:49:49 >> log scanned up to (1168680308856)
210831 04:49:50 >> log scanned up to (1168680308942)
210831 04:49:51 >> log scanned up to (1168680309028)
210831 04:49:52 >> log scanned up to (1168680309114)
InnoDB: Last flushed lsn: 1168515708469 load_index lsn 1168680309114
InnoDB: An optimized (without redo logging) DDLoperation has been performed. All modified pages may not have been flushed to the disk yet.

PXB will not be able take a consistent backup. Retry the backup operation
mysql: [Warning] Using a password on the command line interface can be insecure.

失败了,并且时间都很吻合,04:49 完整备份失败,04:49 主从延迟瞬间飙升。

要检查完整备份历史记录,过去的备份大约需要2小时,但这次备份花费了4小时50分钟,并失败了。

[En]

To check the full backup history, the backup took about 2 hours in the past, but this backup took 4 hours and 50 minutes and failed.

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

(说明:从完整备份打印的log来看,打印FLUSH TABLES WITH READ LOCK时,完整备份就快结束了,所以,在此图上,通过FLUSH TABLES WITH READ LOCK的时间代表完整备份结束的时间)

到此时,相信很多同学,已经准备把锅甩给完整备份了。

需要思考的问题 :DDL操作和xtrabackup是怎么相互影响的?为什么全量备份失败后,延迟问题才会马上被监控到(Seconds_Behind_Master才显示异常)?【问题3】

3.继续分析原因,寻找DDL

每个问题都不简单。

我们先看问题3. DDL操作和xtrabackup是怎么相互影响的?为什么全量备份失败后,延迟问题才会马上被监控到(Seconds_Behind_Master才显示异常)?【问题3】

回答这个问题,需要先找到具体的DDL操作。

但是备份期间没有执行过DDL操作。上次一DDL操作 要追溯到前一天19:50。迷雾重重、元凶隐隐。

想到MySQL DDL操作,一般执行时间相对较长,slow log 中或许有蛛丝马迹。

ELK–MySQL日志系统走起。

还是找到了一条大鱼, alter table ,添加字段。时间点吻合(04:49),执行时间超长25438秒(并且和Seconds_Behind_Master飙升后的值很接近 )。

从库DDL执行log

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

25438秒 ≈ 7 小时

从库所有的操作都来自主库,接下来,我们看下主库啥时候执行DDL的。

主库DDL执行情况

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

主库执行了 5089 秒,结束于 前一天的21:18。向前推 5089 秒,也就是 19:50前后。

为什么执行时间会这么长呢?这张表确实是大表、宽表–1.6 亿 数据量、130G大小,其信息如下:

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

所以,到现在,才算把主从延迟的问题定位到—–DDL 和 innobackupex。两者相互影响、相互拖累,才导致这个时间段内的延迟产生。这也算问题1的答案吧。

4. Seconds_Behind_Master 问题解答

此处我们尝试问答第二个问题: 为什么延迟,会瞬间飙升到24440S以上,延迟不应该是逐渐累积的吗?【问题2】

我么先看下主从复制指标:Seconds_Behind_Master 的官方描述

Seconds_Behind_Master: The number of seconds that the replication SQL thread is behind processing the source’s binary log. A high number (or an increasing one) can indicate that the replica is unable to handle events from the source in a timely fashion.

A value of 0 for Seconds_Behind_Master can usually be interpreted as meaning that the replica has caught up with the source, but there are some cases where this is not strictly true. For example, this can occur if the network connection between source and replica is broken but the replication I/O thread has not yet noticed this—that is, slave_net_timeout has not yet elapsed.

It is also possible that transient values for Seconds_Behind_Master may not reflect the situation accurately. When the replication SQL thread has caught up on I/O, Seconds_Behind_Master displays 0; but when the replication I/O thread is still queuing up a new event, Seconds_Behind_Master may show a large value until the SQL thread finishes executing the new event. This is especially likely when the events have old timestamps; in such cases, if you execute SHOW SLAVE STATUS several times in a relatively short period, you may see this value change back and forth repeatedly between 0 and a relatively large value.

Check the Seconds_Behind_Master column in the output from SHOW REPLICA | SLAVE STATUS.

When the replication SQL thread executes an event read from the source, it modifies its own time to the event timestamp. (This is why TIMESTAMP is well replicated.) In the Time column in the output of SHOW PROCESSLIST, the number of seconds displayed for the replication SQL thread is the number of seconds between the timestamp of the last replicated event and the real time of the replica machine. You can use this to determine the date of the last replicated event. Note that if your replica has been disconnected from the source for one hour, and then reconnects, you may immediately see large Time values such as 3600 for the replication SQL thread in SHOW PROCESSLIST. This is because the replica is executing statements that are one hour old.

通过以上的描述 和 本 Case 中 Seconds_Behind_Master 的数字变化:

Seconds_Behind_Master 似乎应该为:SQL thread 重做主节点同步过来的binlog even, 执行提交后,比较从节点执行even的时间和主节点binlog event 中的时间所得到时间差。

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

再回答这个问题, Seconds_Behind_Master瞬间飙升到24440S 以上【因为我们的telegraf 抓取是20S一次,并且从监控库图上可看到,有一个快速下降趋势,所以,真正的最大值应该为25K以上,很可能会比慢查询的25438 还稍微大些】, 这个值是怎么来的?应该就是从库DDL提交的时刻【04:49】减去主库DDL提交的时刻【前一天的21:18】的差值。– 这也算问题2的答案吧。

所以,Seconds_Behind_Master = 0 时,主从延迟可能已经很厉害了,除了官网提到的网络问题,还有就是从库正则执行同步过来的大DDL(尚未提交)。例如在本例的00:00—04:00 其实已经存在延迟了。

5.innobackupex知识补充

关于问题 3中 为什么全量备份失败后,延迟问题才会马上被监控到(Seconds_Behind_Master才显示异常)? 目前来看,尚未分析到直接因果关系,应该是概率事件。

innobackupex备份流程图如下:

MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

经过验证测试,innobackupex备份开始时,并不是先看当前数据库是否有DDL操作,而是其中的某个线程在备份这张表时(或 scan lsn)才判断是否有DDL,有则推出报错。验证方法,在一张大表上执行DDL(保证DDL时间足够长),在DDL运行期间,开始备份,我们我看到这个很多表在执行copy备份,是运行了一段时间后,才报错的。

此外, innobackupex备份过程中,执行DDL,都会导致备份失败,与表大小无关,即使这张表只有一笔数据。

补充说明

MySQL版本为5.7.22

innobackupex的版本为 innobackupex version 2.4.9 Linux (x86_64) (revision id: a467167cdd4)

6.MySQL 8.0的备份说明

因为我们线上部分DB Server 是8.0.29 ,所以相应的percona-xtrabackup也要升级。

下载的工具是percona-xtrabackup-8.0.29-22-Linux-x86_64.glibc2.17.tar.gz,版本为8.0.29-22

..../bin/xtrabackup version 8.0.29-22 based on MySQL server 8.0.29 Linux (x86_64) (revision id: c31e7ddcce3)

与备份MySQL 5.7 的主要区别
XtraBackup 8.0版本与Mysql8.0配套,并移除innobackupex命令。参数的使用也有较大变化。

权限问题

2022-05-16T14:19:19.836225+08:00 0 [ERROR] [MY-011825] [Xtrabackup] failed to execute query 'LOCK INSTANCE FOR BACKUP' : 1227 (42000) Access denied; you need (at least one of) the BACKUP_ADMIN privilege(s) for this operation

解决方案

授权:
grant BACKUP_ADMIN on *.* to '备份账户'@'localhost';
刷新:
FLUSH PRIVILEGES;

参数问题

新增参数
新的备份工具一定要指定参数--backup=1。
显式指定参数
新的备份工具一定要显式指定参数 --target-dir=XXXXX
移除参数
新的备份工具一定要移除参数--no-timestamp
否则报错如下:
xtrabackup: [ERROR] unknown option '--no-timestamp'.

增量备份时,还需移除参数--incremental
否则报错如下:
xtrabackup: [ERROR] unknown option '--incremental'.

案例

完整备份

...../bin/xtrabackup --defaults-file=/etc/mysqld/my.cnf --user=用户 --password=密码 --socket=/mysqld/mysql.sock --backup --compress --compress-threads=2 --target-dir=/data/mysql_backups/base

增量备份

...../bin/xtrabackup --defaults-file=/etc/mysqld/my.cnf --user=用户 --password=密码 --socket=/mysqld/mysql.sock --compress --backup --compress-threads=2 --incremental --incremental-basedir=/data/mysql_backups/base --target-dir=/data/mysql_backups/inc_1

7.参考文献

1.mysqldump与innobackupex备份过程知多少(完结篇)

https://blog.csdn.net/joy0921/article/details/80130860

2.MySQL中的seconds_behind_master的理解

https://www.cnblogs.com/drizzle-xu/p/9713350.html?ivk_sa=1024320u

3.How do I know how late a replica is compared to the source?

https://dev.mysql.com/doc/refman/8.0/en/faqs-replication.html

Original: https://www.cnblogs.com/xuliuzai/p/15216768.html
Author: 东山絮柳仔
Title: MySQL alter table时执行innobackupex全备再谈Seconds_Behind_Master

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508191/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Centos7环境使用Mysql离线安装包安装Mysql5.7

    服务器环境:centos7 x64 需要安装:mysql5.7+ 1)检查mysql组合用户是否存在 2)若不存在,则创建mysql组和用户 对于版本选择,您可以选择以下两种方式:…

    数据库 2023年5月24日
    083
  • 重构

    参数过长 影响: 方法不易被理解、使用,方法签名容易不稳定,不易维护 解决方法:反复使用提炼方法+内联方法,消除多余参数 ​ 尽量把方法移进相关的类中 ​ 如实体类中的get方法在…

    数据库 2023年6月16日
    0212
  • jQuery基础

    作者导言: 引用偶像刘德华的一句话 “学到的就要教人,赚到的就要给人”! 以下是关联的web前端基础知识文章,通过这些文章,您既可以系统地学习和了解这些知识…

    数据库 2023年6月14日
    094
  • 记一次部署系列:prometheus配置通过alertmanager进行邮件告警

    1、修改配置 首先在prometheus中配置alertmanager地址,并配置告警规则文件,如下,然后重启prometheus。 规则文件如下:rules.yml &#8211…

    数据库 2023年6月9日
    077
  • javaScript知识大全(基础)!!

    定义方法:即把函数放在对象里面,对象只有属性和方法 var a={ name:"111", birth:2000, age:function (){ var n…

    数据库 2023年6月16日
    082
  • 数据库操作

    数据库操作 数据库基础数据库是一种 存储结构, 允许使用各种格式 输入、处理、检索 数据,且不用在每次需要数据时 重新输入数据。 select 语句: select语句 用于查询数…

    数据库 2023年6月16日
    0104
  • 学习笔记——Django项目中请求与响应(json数据)

    2022-10-04 测试json数据与Django项目与pycharm连接,在”postman”软件中。”postman”是一个接…

    数据库 2023年6月14日
    0150
  • 695.岛屿的最大面积

    695.岛屿的最大面积 给你一个大小为 m x n 的二进制矩阵 grid 。 岛屿 是由一些相邻的 1 (代表土地) 构成的组合,这里的「相邻」要求两个 1 必须在 水平或者竖直…

    数据库 2023年6月16日
    081
  • 手把手教你定位线上MySQL慢查询问题,包教包会

    1. 慢查询日志的作用 默认情况下不启用慢速日志。建议手动启用,以便我们更轻松地定位在线问题。 [En] Slow log is not enabled by default. I…

    数据库 2023年5月24日
    0100
  • MySQL锁:03.InnoDB行锁

    传送门:MySQL锁:01.总览传送门:MySQL锁:02.InnoDB锁传送门:MySQL锁:03.InnoDB行锁 InnoDB 行锁 锁排查可以用的视图和数据字典 InnoD…

    数据库 2023年6月16日
    0135
  • BFS与DFS套路总结

    概述 深度优先遍历和广度优先搜索和广度优先搜索是解决图问题最常见的方式,并且在leetcode中有许多相关的变体,但万变不离其宗,其本质结构或者算法框架时固定的,因此本文BFS和D…

    数据库 2023年6月11日
    0110
  • Redis学习(1)—Redis概述

    什么是NoSQL NoSQL:Not Only SQL,意思不仅仅是SQL,它是属于 非关系型数据库。那什么是关系型数据库?数据结构是一种有行有列的数据库。 NoSQL数据库是为了…

    数据库 2023年6月14日
    082
  • Linux中MySQL的安装以及卸载

    一.MySQL MySQL 是一种开放源代码的关系型数据库管理系统,开发者为瑞典MySQL AB公司。在2008年1月16号被Sun公司收购。而2009年,SUN又被Oracle收…

    数据库 2023年5月24日
    0106
  • 牛客SQL刷题第三趴——SQL必知必会

    【问题】编写 SQL 语句,从 Products 表中检索产品名称(prod_name)和描述(prod_desc),仅返回在描述中以先后顺序同时出现 toy 和 carrots …

    数据库 2023年5月24日
    076
  • Sencha Cmd 常用命令

    1、获取帮助 sencha help generate app 2、创建应用程序 sencha -sdk e:\ext\ext6 generate app -classic Sim…

    数据库 2023年6月9日
    092
  • 数据库的主从搭建

    数据库(mariadb)主从搭建 1,准备两台虚拟机(本文使用centos7演示)! 一:启动虚拟机配置环境安装数据库(俩节点都要执行操作) setenforce 0 Origin…

    数据库 2023年6月16日
    0145
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球