MySQL-8.0.42 主从延迟常见原因及解决方法
目录
1 查看从库延时方法
2 如何分析主从延迟
3 主从延迟的常见原因及解决方法
1 查看从库延时方法
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_Running: Yes #两个yes说明主从复制正常
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0 #表示延时时间,单位是秒,0表示没有延时
2 如何分析主从延迟
2.1 从库服务器的负载情况:从CPU、内存、磁盘IO和网络的资源使用情况入手,重点关注CPU和磁盘IO。
#查看CPU和内存使用信息
root@u24-mysql-52:~# top
top - 21:12:55 up 26 min, 1 user, load average: 0.00, 0.01, 0.05
Tasks: 214 total, 1 running, 213 sleeping, 0 stopped, 0 zombie
%Cpu(s): 7.1 us, 7.1 sy, 0.0 ni, 82.1 id, 3.6 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 3866.7 total, 2817.9 free, 859.0 used, 416.0 buff/cache
MiB Swap: 3866.0 total, 3866.0 free, 0.0 used. 3007.7 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1600 root 20 0 11912 5660 3484 R 14.3 0.1 0:00.05 top
1 root 20 0 22104 13076 9492 S 0.0 0.3 0:09.19 systemd
参数说明
load average: 0.00, 0.01, 0.05
#1 分钟、5 分钟、15 分钟的平均负载
%Cpu(s): 7.1 us, 7.1 sy, 0.0 ni, 82.1 id, 3.6 wa, 0.0 hi, 0.0 si, 0.0 st
#us (user):用户空间进程的 CPU 占用百分比(如应用程序)。
#sy (system):内核空间进程的 CPU 占用百分比(如系统调用)。
MiB Mem : 3866.7 total, 2817.9 free, 859.0 used, 416.0 buff/cache
#total:总内存/交换空间大小。
#free:完全未使用的内存。
#used:已使用的内存(包括缓存和缓冲区)。
#buff/cache:内核缓存和缓冲区占用的内存(可被快速释放供程序使用)。
#avail Mem:估算的可用内存(包含缓存和缓冲区的可回收部分)。
#查看磁盘IO
root@u24-mysql-52:~# iostat
Linux 6.11.0-26-generic (u24-mysql-52) 05/30/2025 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
0.74 0.05 1.30 0.06 0.00 97.85
Device tps kB_read/s kB_wrtn/s kB_dscd/s kB_read kB_wrtn kB_dscd
loop0 0.00 0.00 0.00 0.00 10 0 0
sda 5.97 189.95 57.82 0.00 465358 141664 0
sr0 0.02 0.85 0.00 0.00 2092 0 0
#查看网络
#显示网络软件栈统计和状态
root@u24-mysql-52:~# netstat -alutp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 _localdnsstub:domain 0.0.0.0:* LISTEN 646/systemd-resolve
tcp 0 0 _localdnsproxy:domain 0.0.0.0:* LISTEN 646/systemd-resolve
tcp 0 0 u24-mysql-52:57448 192.168.254.51:mysql ESTABLISHED 1469/mysqld
tcp6 0 0 [::]:33060 [::]:* LISTEN 1469/mysqld
tcp6 0 0 [::]:ssh [::]:* LISTEN 1/init
tcp6 0 0 [::]:mysql [::]:* LISTEN 1469/mysqld
tcp6 0 140 u24-mysql-52:ssh 192.168.254.1:63340 ESTABLISHED 1298/sshd: root@pts
udp 0 0 _localdnsproxy:domain 0.0.0.0:* 646/systemd-resolve
udp 0 0 _localdnsstub:domain 0.0.0.0:* 646/systemd-resolve
2.2 主从复制状态
#主库状态
mysql> show master status \G
*************************** 1. row ***************************
File: mysql-bin.000010
Position: 197
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 5d7fd409-3bd6-11f0-9d66-000c29704177:1
1 row in set (0.00 sec)
#从库状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for source to send event
Master_Host: 192.168.254.51
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000010
Read_Master_Log_Pos: 197
Relay_Log_File: u24-mysql-52-relay-bin.000005
Relay_Log_Pos: 413
Relay_Master_Log_File: mysql-bin.000010
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: 197
Relay_Log_Space: 886
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: 51
Master_UUID: 5d7fd409-3bd6-11f0-9d66-000c29704177
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 5d7fd409-3bd6-11f0-9d66-000c29704177:1
Executed_Gtid_Set: 5d7fd409-3bd6-11f0-9d66-000c29704177:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
Master_public_key_path:
Get_master_public_key: 1
Network_Namespace:
1 row in set, 1 warning (0.00 sec)
重点关注参数
Master_Log_File: mysql-bin.000010
Relay_Master_Log_File: mysql-bin.000010
Relay_Master_Log_File: mysql-bin.000010
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Seconds_Behind_Master: 0
如果主库(File,Position)大于从库(Master_Log_File, Read_Master_Log_Pos),说明IO线程存在延迟。
如果从库(Relay_Master_Log_File,Exec_Master_Log_Pos) < (Master_Log_File,Read_Master_Log_Pos),说明SQL线程存在延迟。
2.3 主库binlog的写入量多少,如果写入量大于从库IO读取量也会出现延迟。
3 主从延迟的常见原因及解决方法
3.1 IO线程存在延迟
#网络延迟
检查网络问题,启用binlog的压缩传输
#磁盘IO存在瓶颈
关闭binlog
#网络问题
在主库使用rsync 拷贝一个文件到从库,简单可以判断
3.2 SQL线程存在延迟
#主库写入量过大,而SQL线程单线程重放
解决方法:可以启动并行复制,默认参数值可以,通过修改slave_parallel_workers数量来提高
slave_parallel_type=LOGICAL_CLOCK
slave_parallel_workers=4
slave_preserve_commit_order=ON
binlog_transaction_dependency_tracking=WRITESET_SESSION
transaction_write_set_extraction=XXHASH64
binlog_transaction_dependency_history_size=25000
binlog_format=ROW
#大事务
指二进制日志格式为row的情况下,操作涉及的记录数比较多,特别是update和delete操作
解决方法: 建议把大事务拆分,每一次小批量执行
#从库上有查询操作
消耗系统资源和锁等待,常见从库的查询操作导致阻塞主库DDL操作
mysql> show processlist;
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
| 5 | system user | connecting host | NULL | Connect | 7574 | Waiting for source to send event | NULL |
| 6 | system user | | NULL | Query | 6409 | Replica has read all relay log; waiting for more updates | NULL |
| 7 | event_scheduler | localhost | NULL | Daemon | 7574 | Waiting on empty queue | NULL |
| 10 | system user | | NULL | Connect | 7574 | Waiting for an event from Coordinator | NULL |
| 11 | system user | | NULL | Connect | 7574 | Waiting for an event from Coordinator | NULL |
| 12 | system user | | NULL | Connect | 7574 | Waiting for an event from Coordinator | NULL |
| 13 | system user | | NULL | Connect | 7574 | Waiting for an event from Coordinator | NULL |
| 16 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+-----------------+-----------------+------+---------+------+----------------------------------------------------------+------------------+
8 rows in set, 1 warning (0.00 sec)
#从库上存在备份
备份的全局读锁阻塞sql线程重放。
mysql> show processlist;
#磁盘IO存在瓶颈
从库关闭binlog日志