SQL_Remaining_Delay这个参数

本文详细探讨了MySQL只读实例设置延迟复制后,Seconds_Behind_Master、SQL_Delay和SQL_Remaining_Delay参数的理解。在启动时,由于延迟时间未完全达到,Seconds_Behind_Master显示为0,而SQL_Remaining_Delay则逐渐减小,直至达到预设延迟。通过监控 Slave_SQL_Running_State,可以观察到延迟复制的进程。最终,在延迟时间过后,Seconds_Behind_Master显示出正确的延迟值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

在部署一个只读实例,测试的时候,设置了延时6个小时,启动起来看到情况如下,show slave status\G中关于延时相关的值:

        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: 123
                  Master_UUID: xxxxxx-xxxx-11eb-9a26-xxxxxxxxx
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 22600
          SQL_Remaining_Delay: 4110
      Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl:
			   

 

SQL_Delay这个值是准确的,6个小时是22600,但是Seconds_Behind_Master这个参数确实0,并不像我预料中的是22600,理论上,设置好延时复制后,Seconds_Behind_Master的值不应该是0而是延迟6个小时的值。

 

另外发现了一个SQL_Remaining_Delay这个的值在变化,是抖动式的,一会高一会低,但是总体是往下走的下降趋势。查看了下官网文章:https://dev.mysql.com/doc/refman/5.7/en/replication-delayed.html,里面有如下解释

  • SQL_Delay: A nonnegative integer indicating the number of seconds that the replica must lag the source.

  • SQL_Remaining_Delay: When Slave_SQL_Running_State is Waiting until MASTER_DELAY seconds after master executed event, this field contains an integer indicating the number of seconds left of the delay. At other times, this field is NULL.

  • Slave_SQL_Running_State: A string indicating the state of the SQL thread (analogous to Slave_IO_State). The value is identical to the State value of the SQL thread as displayed by SHOW PROCESSLIST.

 

总算明白了,就是在启动的时候,当前部署的只读库的最新数据离主库小于6小时,那么这个SQL_Remaining_Delay:当 Slave_SQL_Running_State 等待,直到MASTER_DELAY秒后,Master执行的事件,此字段包含一个整数,表示有多少秒左右的延迟。在其他时候,这个字段是0。

 

那么这种时候,只有等只读实例慢慢恢复,当SQL_Remaining_Delay慢慢变小到0的时候,Seconds_Behind_Master就会从0变成22600的。

 

过了3分小时候,再进来看slave状态,就变成了我们想要的那个样子:

Seconds_Behind_Master: 22602
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: 123
                  Master_UUID: xxxxxx-xxxx-11eb-9a26-xxxxxxxxx
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 22600
          SQL_Remaining_Delay: 2
      Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 			   

 

这段代码运行的结果一般是 MySQL 数据库的主从复制状态信息中包含 "running" 的行。具体来说,它首先通过执行 MySQL 的 `show slave status` 命令获取 MySQL 数据库的主从复制状态信息,然后使用 grep 命令过滤出包含 "running" 字符串的行。 如果 MySQL 数据库的主从复制正常运行,那么运行这段代码应该会输出如下内容(具体输出格式可能会有所不同): ``` *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: localhost Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 348 Relay_Log_File: mysqld-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 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: 348 Relay_Log_Space: 154 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: 1 Master_UUID: 0000-00a2-0000-0000-000000000001 Master_Info_File: /usr/local/mysql/data/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL 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: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 ``` 其中,最后一行信息中可能会包含 "running" 字符串,表示主从复制正在运行。如果 MySQL 数据库的主从复制没有运行或者出现异常,那么运行这段代码不会输出任何信息。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值