只读实例(slave主从)延迟排查

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 本文分享的方法适用于实时查看只读延迟(主从延迟),即需要在延迟发生的时候查看才能确认问题,历史延迟不适用,以下环境已经开启并行复制。
作者:手辨


实为吾之愚见,望诸君酌之!闻过则喜,与君共勉


以下方法适用于实时查看只读延迟(主从延迟),即需要在延迟发生的时候查看才能确认问题,历史延迟不适用,以下环境已经开启并行复制。

第一章:如何判断无延迟

第一步:主实例信息获取

主实例(主节点)高权限账户执行:

1,show processlist

2451b6268c7a72110d1461870ca8be4ec8ecb3bc.png
2,show master status\G

7ede228c8ad41b85eb1d3d02291756d7c7264c0f.png

3,show variables like ‘%uuid%’;

4302cee140dacf133cd2c4b7c993c9ab8f37e3b8.png

第二步:只读实例信息获取

只读实例(slave从库)高权限账户执行:

1, show processlist

aafb4079462fe04a04c17a92c960db89aed7c20f.png

2, show slave status\G

mysql>show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 10.151.83.26

Master_User: replicator

Master_Port: 3106

Connect_Retry: 60

Master_Log_File: mysql-bin.000379

Read_Master_Log_Pos: 53064

Relay_Log_File: slave-relay.000375

Relay_Log_Pos: 53237

Relay_Master_Log_File: mysql-bin.000379

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 53064

Relay_Log_Space: 53527

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 999098802

Master_UUID: a23bf85f-f20b-11e8-8268-7cd30abda240

Master_Info_File: mysql.slave_master_info

SQL_Delay: 0

SQL_Remaining_Delay:

Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates

Master_Retry_Count: 86400

Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Last_SQL_Error_Gtid:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set: a23bf85f-f20b-11e8-8268-7cd30abda240:391216-838127

Executed_Gtid_Set: 937ca3af-f20b-11e8-9447-7cd30ab8a5d8:1-178841,

a23bf85f-f20b-11e8-8268-7cd30abda240:1-838127

Auto_Position: 1

Replicate_Rewrite_DB:

Channel_Name:

Master_TLS_Version:

3,show variables like ‘%uuid%’;

e1992786156498a06ab6a65c9e56b639bcff3dcd.png

第三步:对比确认

1, 确认主实例的进程状态是否为Master has sent all binlog to slave; waiting for more updates(图片红框标识处),如果是该状态,说明master已经把所有的binlog发送给了slave,主实例正常

2, 确认只读实例(slave从库)的进程状态是否为:Waiting for master to send event以及

Slave has read all relay log; waiting for more updates,如果是改状态,说明slave以及应用了所有master发送的binlog,正在等待master继续发送

3, 对比只读实例执行show slave status\G得到的Executed_Gtid_Set与主实例执行show master status\G得到的Executed_Gtid_Set是否一致或相近,如果一致,则可以说明主实例执行的事务,只读实例也已经执行。

通过如上3步的对比,可以确认主实例与只读实例(slave从库)无延迟

第二章 延迟原因以及解决

通过第一章的结论,可以得出只读实例无延迟,当出现只读延迟的时候,应该如何确认原因以及解决,请参考下面的具体例子:

2.1:大批量更新

2.1.1复现方法:

主实例执行大批量写入等更新操作: insert into MOCK_DATA3 select * from aaaaa.MOCK_DATA,其中MOCK_DATA表数量1000w行,导致延迟出现

2.1.2延迟出现:

show processlist结果:

21 system user Connect 41786 Waiting for master to send event

22 system user Connect Waiting for Slave Workers to free pending events

23 system user Connect 236 Executing event

24 system user Connect 571 Waiting for an event from Coordinator

25 system user Connect 41786 Waiting for an event from Coordinator

26 system user Connect 41786 Waiting for an event from Coordinator

27 system user Connect 41786 Waiting for an event from Coordinator

28 system user Connect 41786 Waiting for an event from Coordinator

29 system user Connect 41786 Waiting for an event from Coordinator

30 system user Connect 41786 Waiting for an event from Coordinator

show slave status\G结果:

Master_SSL_Key :

Seconds_Behind_Master : 0

Master_SSL_Verify_Server_Cert : No

Last_IO_Errno : 0

Last_IO_Error :

Last_SQL_Errno : 0

Last_SQL_Error :

Replicate_Ignore_Server_Ids :

Master_Server_Id : 999098802

Master_UUID : a23bf85f-f20b-11e8-8268-7cd30abda240

Master_Info_File : mysql.slave_master_info

SQL_Delay : 0

SQL_Remaining_Delay : undefined

Slave_SQL_Running_State : Waiting for Slave Workers to free pending events

Master_Retry_Count : 86400

Master_Bind :

Last_IO_Error_Timestamp :

Last_SQL_Error_Timestamp :

Last_SQL_Error_Gtid :

Master_SSL_Crl :

Master_SSL_Crlpath :

Retrieved_Gtid_Set : a23bf85f-f20b-11e8-8268-7cd30abda240:1-861466

Executed_Gtid_Set : 937ca3af-f20b-11e8-9447-7cd30ab8a5d8:1-178841,a23bf85f-f20b-11e8-8268-7cd30abda240:1-861455

show engine innodb status\G结果:

---TRANSACTION 2819387, ACTIVE 65 sec inserting

mysql tables in use 1, locked 1

1 lock struct(s), heap size 1136, 0 row lock(s), undo log entries 5919707

MySQL thread id 23, OS thread handle 140536255158016, query id 102150 Executing event

2.1.3 解决方法

建议等待只读实例应用完主库的更新,会自动恢复。

2.2:无主键更新

2.2.1复现方法:

主实例执行较多数据的无主键更新操作:mysql>update MOCK_DATA set id=90 where id<10;,导致延迟出现

2.2.2 延迟出现:

show processlist结果:

1bc75c45d4b3177404e07c7c665b7dc10ae5ec4e.png

show slave status\G结果:

47dd4874f45cb6d85a6bf3f8544e3e5f5355159f.png

2.2.3 解决方法

建议增加有效索引提高更新效率。

2.3:阻塞(ddl)

2.3.1 复现方法

只读实例开启显式事务,执行如下查询,不提交事务:

325bdaee522f36b407b8479dc2f00116e97c9af0.png

主实例执行truncate table mock_data,只读延迟产生

2.3.2延迟产生

show processlist 结果:

4cdb349d62eff3949d121475d3793c98fd9b26ad.png

show slave status\G结果:

0a8358893a45f09bc1d39b6b31a049735f67e46b.png

2.4:Waiting for Slave Worker to release partition

参考: https://yq.aliyun.com/articles/188482?spm=a2c4e.11155435.0.0.120f63b5YRAxtQ

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
消息中间件 编解码 Java
RocketMQ安装和使用
RocketMQ安装和使用
551 1
|
数据库
kettle开发篇-记录集连接
kettle开发篇-记录集连接
299 0
|
12月前
|
SQL 监控 关系型数据库
使用SQL语句查询操作耗时的技巧与方法
在数据库管理和优化过程中,了解SQL查询操作的耗时是至关重要的
1474 0
|
缓存 Linux 开发工具
CentOS 7- 配置阿里镜像源
阿里镜像官方地址http://mirrors.aliyun.com/ 1、点击官方提供的相应系统的帮助 :2、查看不同版本的系统操作: 下载源1、安装wget yum install -y wget2、下载CentOS 7的repo文件wget -O /etc/yum.
255275 0
|
11月前
|
存储 负载均衡 中间件
Nginx反向代理配置详解,图文全面总结,建议收藏
Nginx 是大型架构必备中间件,也是大厂喜欢考察的内容,必知必会。本篇全面详解 Nginx 反向代理及配置,建议收藏。
Nginx反向代理配置详解,图文全面总结,建议收藏
|
12月前
|
监控 API 数据安全/隐私保护
2024年开源API工具盘点,覆盖API全生命周期
2024年经济持续低迷,本文整理一些免费的开源工具,旨在帮助企业组织降低工具的支出成本,能用免费的何必用付费的呢(狗头)?
457 0
|
消息中间件 Linux
linux之centos安装rabbitmq
linux之centos安装rabbitmq
|
存储 NoSQL 大数据
大数据存储:HBase与Cassandra的对比
【7月更文挑战第16天】HBase和Cassandra作为两种流行的分布式NoSQL数据库,在数据模型、一致性模型、数据分布、查询语言和性能等方面各有千秋。HBase适用于需要强一致性和与Hadoop生态系统集成的场景,如大规模数据处理和分析。而Cassandra则更适合需要高可用性和灵活查询能力的场景,如分布式计算、云计算和大数据应用等。在实际应用中,选择哪种数据库取决于具体的需求和场景。希望本文的对比分析能够帮助读者更好地理解这两种数据库,并做出明智的选择。
|
监控 NoSQL Go
Go语言中高效使用Redis的Pipeline
Redis 是构建高性能应用时常用的内存数据库,通过其 Pipeline 和 Watch 机制可批量执行命令并确保数据安全性。Pipeline 类似于超市购物一次性结账,减少网络交互时间,提升效率。Go 语言示例展示了如何使用 Pipeline 和 Pipelined 方法简化代码,并通过 TxPipeline 保证操作原子性。Watch 机制则通过监控键变化实现乐观锁,防止并发问题导致的数据不一致。这些机制简化了开发流程,提高了应用程序的性能和可靠性。
188 0
|
存储 缓存 NoSQL
经验大分享:OHCJava堆外缓存详解与应用
经验大分享:OHCJava堆外缓存详解与应用
301 1