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)

大家都在看

  • Java面试题(二)–MySQL

    1 存储引擎 1、简单描述一个Mysql的内部结构? MySQL的基本架构示意图:大体来说,MySQL可以分为 server层和 存储引擎层两部分。 ① server层包括连接器、…

    数据库 2023年6月16日
    077
  • python pywin32学习笔记

    参考博客链接 参考博客链接 pyhandle windows窗体句柄,int 类型  className 窗体类名  text 窗体标题 窗体置顶  窗体最大化 通过父pyhand…

    数据库 2023年6月11日
    076
  • 最小堆、胜者树和败者树

    前言 本文介绍三种外部排序算法:最小堆、胜者树和败者树,以及为什么外部排序往往用败者树。 最小堆 最小堆有以下特点:1、最小堆是一颗完全二叉树2、每个父节点的值总是小于等于左右孩子…

    数据库 2023年6月9日
    085
  • Postman安装及汉化

    1. 安装postman 各位根据各自需要可以参考下表自行下载,把链接内的”版本号”替换为指定的版本号 操作系统 下载链接 Windows64位 Windo…

    数据库 2023年6月14日
    090
  • Django项目中表的查询的操作

    2022-09-29 shell操作: 我在使用中是pycharm与数据库建立连接的一个工具。 使用的环境: 在此处是用在了虚拟环境中。 使用场景: 一般是在创建的表中插入数据后,…

    数据库 2023年6月14日
    063
  • 笔记-docker学习-1

    一、docker安装 1、CentOS Docker安装 安装命令如下: curl -fsSL https://get.docker.com | bash -s docker –…

    数据库 2023年6月9日
    048
  • Hadoop_mapreduce统计文本单词

    Hadoop MapReduce是一个软件框架,基于该框架能够容易地编写应用程序,这些应用程序能够运行在由上千个商用机器组成的大集群上,并以一种可靠的,具有容错能力的方式并行地处理…

    数据库 2023年6月11日
    068
  • Spring5完整版详解

    1、Spring 1.1简介 2002,首次退出来Spring框架的雏形:interface21框架 Spring框架即以interface21框架为基础,经过重新设计,并不断丰富…

    数据库 2023年6月16日
    073
  • Java Script 循环,数组,对象,判断,阶乘,查找-综合运用合集

    输出100个hello world. for (var i = 1; i console.log("hello world");} 创建一个包含1~100的数组…

    数据库 2023年6月11日
    082
  • MySQL增删改

    数据处理之增删改 插入数据(增) 前提:创建一个空表:id,name,hire_data,salary, 方法一:逐一添加数据 [En] method 1: add data on…

    数据库 2023年5月24日
    057
  • 努力的去帮助他人

    天道运而无所积,故万物成;帝道运而无所积,故天下归;–庄子《天道篇》 知识分享才能成长,财富流动才能更多;努力的去帮助每一个人,自己也会收获更多的快乐;不要为生活琐事在…

    数据库 2023年6月16日
    077
  • win10搜索功能用不了

    这玩意搞了我今天,直接裂开!系统更新根本解决不了 好在查了相关资料才知道,原来微软在 Win10 的更新中,将搜索功能和语音助手 Cortana 进行了拆分,搜索成了一个独立的功能…

    数据库 2023年6月14日
    051
  • MySQL慢查询优化问题-解决办法

    根据发现的问题,找出原因,然后对症下药。 [En] According to the problems found, find the cause, and then prescr…

    数据库 2023年5月24日
    086
  • 一个反直觉的sql

    引子 在《容易引起雪崩的两个处理》里,我提到一个慢查询的问题。本文先从整洁架构的角度讲讲慢查询sql完成的功能以及设计,再介绍对sql进行的实施测试现象以及思考。 设计讲解 眼看着…

    数据库 2023年5月24日
    089
  • Background process terminated with code 1.

    使用vscode过程中,刷新maven依赖过程中,出现了如下错误信息: Background process terminated with code 1. 图1. maven插件…

    数据库 2023年6月14日
    062
  • MySQL约束

    约束指对字段的约束,用于确保数据库的数据满足特定的规则。在MySQL中,数据库的约束包括, NOT NULL,PRIMARY KEY,UNIQUE,FOREIGN KEY,CHEC…

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