1.一般的做法就是根据Seconds_Behind_Master的值来判断slave的延迟状态。

可以通过监控show slave status\G命令输出的Seconds_Behind_Master参数的值来判断,是否有发生主从延时。

mysql>show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waitingformaster to send event

Master_Host:192.168.1.101Master_User: slave

Master_Port:3306Connect_Retry:60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos:761249Relay_Log_File: mysql-relay-bin.000008Relay_Log_Pos:761408Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: huanqiu,huanpc

Replicate_Ignore_DB: mysql

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno:0Last_Error:

Skip_Counter:0Exec_Master_Log_Pos:761249Relay_Log_Space:761620Until_Condition: None

Until_Log_File:

Until_Log_Pos:0Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert: No

Last_IO_Errno:0Last_IO_Error:

Last_SQL_Errno:0Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id:101Master_UUID: b667a58f-d6e0-11e6-8c0a-fa163e2d66ac

Master_Info_File:/data/mysql/data/master.infoSQL_Delay:0SQL_Remaining_Delay: NULL

Slave_SQL_Running_State: Slave has read all relay log; waitingfor the slave I/O thread to update it

Master_Retry_Count:86400Master_Bind:

Last_IO_Error_Timestamp:

Last_SQL_Error_Timestamp:

Master_SSL_Crl:

Master_SSL_Crlpath:

Retrieved_Gtid_Set:

Executed_Gtid_Set:

Auto_Position:0

1 row in set (0.00sec)

ERROR:

No query specified

以上是show slave status\G的输出结果,需要监控下面三个参数:

1)Slave_IO_Running:该参数可作为io_thread的监控项,Yes表示io_thread的和主库连接正常并能实施复制工作,No则说明与主库通讯异常,多数情况是由主从间网络引起的问题;

2)Slave_SQL_Running:该参数代表sql_thread是否正常,YES表示正常,NO表示执行失败,具体就是语句是否执行通过,常会遇到主键重复或是某个表不存在。

3)Seconds_Behind_Master:是通过比较sql_thread执行的event的timestamp和io_thread复制好的event的timestamp(简写为ts)进行比较,而得到的这么一个差值;

NULL—表示io_thread或是sql_thread有任何一个发生故障,也就是该线程的Running状态是No,而非Yes。

0 — 该值为零,是我们极为渴望看到的情况,表示主从复制良好,可以认为lag不存在。

正值 — 表示主从已经出现延时,数字越大表示从库落后主库越多。

负值 — 几乎很少见,我只是听一些资深的DBA说见过,其实,这是一个BUG值,该参数是不支持负值的,也就是不应该出现。

-----------------------------------------------------------------------------------------------------------------------------

Seconds_Behind_Master的计算方式可能带来的问题:

relay-log和主库的bin-log里面的内容完全一样,在记录sql语句的同时会被记录上当时的ts,所以比较参考的值来自于binlog,其实主从没有必要与NTP进行同步,也就是说无需保证主从时钟的一致。其实比较动作真正是发生在io_thread与sql_thread之间,而io_thread才真正与主库有关联,于是,问题就出来了,当主库I/O负载很大或是网络阻塞,io_thread不能及时复制binlog(没有中断,也在复制),而sql_thread一直都能跟上io_thread的脚本,这时Seconds_Behind_Master的值是0,也就是我们认为的无延时,但是,实际上不是,你懂得。这也就是为什么大家要批判用这个参数来监控数据库是否发生延时不准的原因,但是这个值并不是总是不准,如果当io_thread与master网络很好的情况下,那么该值也是很有价值的。之前,提到Seconds_Behind_Master这个参数会有负值出现,我们已经知道该值是io_thread的最近跟新的ts与sql_thread执行到的ts差值,前者始终是大于后者的,唯一的肯能就是某个event的ts发生了错误,比之前的小了,那么当这种情况发生时,负值出现就成为可能。

-----------------------------------------------------------------------------------------------------------------------------

简单来说,就是监控slave同步状态中的:

1)Slave_IO_Running、Slave_SQL_Running状态值,如果都为YES,则表示主从同步;反之,主从不同步。

2)Seconds_Behind_Master的值,如果为0,则表示主从同步不延时,反之同步延时。

2.上面根据Seconds_Behind_Master的值来判断slave的延迟状态,这么做在大部分情况下尚可接受,但其实是并不够准确的。对于Slave延迟状态的监控,还应该做到下面的考虑:

首先,我们先看下slave的状态:

mysql> show slave status\G;

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

slave_IO_State: Waiting for master to send event

***

Master_Log_File: mysql-bin.000327

Read_Master_Log_Pos: 668711237

Relay_Log_File: mysql-relay-bin.002999

Relay_Log_Pos: 214736858

Relay_Master_Log_File: mysql-bin.000327

slave_IO_Running: Yes

slave_SQL_Running: Yes

***

Skip_Counter: 0

Exec_Master_Log_Pos: 654409041

Relay_Log_Space: 229039311

***

Seconds_Behind_Master: 3296

***

可以看到 Seconds_Behind_Master 的值是 3296,也就是slave至少延迟了 3296 秒。

我们再来看下slave上的2个REPLICATION进程状态:

mysql> show full processlist\G;

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

Id: 6

User: system user

Host:

db: NULL

Command: Connect

Time: 22005006

State: Waiting for master to send event

Info: NULL

*************************** 2. row ***************************

Id: 7

User: system user

Host:

db: NULL

Command: Connect

Time: 3293

State: Updating

Info: UPDATE ** SET ** WHERE **

可以看到SQL线程一直在执行UPDATE操作,注意到 Time 的值是 3293,看起来像是这个UPDATE操作执行了3293秒,一个普通的SQL而已,肯定不至于需要这么久。

实际上,在REPLICATION进程中,Time 这列的值可能有几种情况:

1)SQL线程当前执行的binlog(实际上是relay log)中的timestamp和IO线程最新的timestamp的差值,这就是通常大家认为的 Seconds_Behind_Master 值,并不是某个SQL的实际执行耗时;

2)SQL线程当前如果没有活跃SQL在执行的话,Time值就是SQL线程的idle time;

而IO线程的Time值则是该线程自从启动以来的总时长(多少秒),如果系统时间在IO线程启动后发生修改的话,可能会导致该Time值异常,比如变成负数,或者非常大。

来看下面几个状态:

设置pager,只查看关注的几个status值

mysql> pager cat | egrep -i 'system user|Exec_Master_Log_Pos|Seconds_Behind_Master|Read_Master_Log_Pos';

这是没有活跃SQL的情况,Time值是idle time,并且 Seconds_Behind_Master 为 0

mysql> show processlist; show slave status\G;

| 6 | system user | | NULL | Connect | 22004245 | Waiting for master to send event | NULL |

| 7 | system user | | NULL | Connect | 13 | Has read all relay log;**

Read_Master_Log_Pos: 445167889

Exec_Master_Log_Pos: 445167889

Seconds_Behind_Master: 0

多执行几次,看下是否有活跃的SQL

mysql> show processlist; show slave status\G;

| 6 | system user | | NULL | Connect | 22004248 | Waiting for master to send event | NULL |

| 7 | system user | | NULL | Connect | 16 | Has read all relay log;**

Read_Master_Log_Pos: 445167889

Exec_Master_Log_Pos: 445167889

Seconds_Behind_Master: 0

发现有活跃SQL了,Time值是和 Seconds_Behind_Master 一样,即SQL线程比IO线程“慢”了1秒

mysql> show processlist; show slave status\G;

| 6 | system user | | NULL | Connect | 22004252 | Waiting for master to send event | NULL |

| 7 | system user | | floweradmin | Connect | 1 | Updating | update **

Read_Master_Log_Pos: 445182239

Exec_Master_Log_Pos: 445175263

Seconds_Behind_Master: 1

再次查看下状态

mysql> show processlist; show slave status\G;

| 6 | system user | | NULL | Connect | 22004254 | Waiting for master to send event | NULL |

| 7 | system user | | floweradmin | Connect | 1 | Updating | update **

Read_Master_Log_Pos: 445207174

Exec_Master_Log_Pos: 445196837

Seconds_Behind_Master: 1

检查到此,可以说下如何正确判断slave的延迟情况:

1)首先看 Relay_Master_Log_File 和 Master_Log_File 是否有差异;

2)如果Relay_Master_Log_File 和 Master_Log_File 是一样的话,再来看Exec_Master_Log_Pos 和 Read_Master_Log_Pos 的差异,对比SQL线程比IO线程慢了多少个binlog事件;

3)如果Relay_Master_Log_File 和 Master_Log_File 不一样,那说明延迟可能较大,需要从MASTER上取得binlog status,判断当前的binlog和MASTER上的差距;

因此,相对更加严谨的做法是:

在第三方监控节点上,对MASTER和slave同时发起SHOW BINARY LOGS和SHOW slave STATUS\G的请求,最后判断二者binlog的差异,以及 Exec_Master_Log_Pos 和Read_Master_Log_Pos 的差异。

例如:

在MASTER上执行SHOW BINARY LOGS 的结果是:

+------------------+--------------+

| Log_name | File_size |

+------------------+--------------+

| mysql-bin.000009 | 1073742063 |

| mysql-bin.000010 | 107374193 |

+------------------+--------------+

而在slave上执行SHOW slave STATUS\G 的结果是:

Master_Log_File: mysql-bin.000009

Read_Master_Log_Pos: 668711237

Relay_Master_Log_File: mysql-bin.000009

slave_IO_Running: Yes

slave_SQL_Running: Yes

***

Exec_Master_Log_Pos: 654409041

***

Seconds_Behind_Master: 3296

***

这时候,slave实际的延迟应该是:

mysql-bin.000009 这个binlog中的binlog position 1073742063 和 slave上读取到的binlog position之间的差异延迟,即:

1073742063 - 654409041 = 419333022 个binlog event

并且还要加上 mysql-bin.000010这个binlog已经产生的107374193个binlog event,共

107374193 + 419333022 = 526707215 个binlog event

https://www.cnblogs.com/kevingrace/p/5685486.html

https://www.cnblogs.com/kevingrace/p/6256603.html

https://www.cnblogs.com/kevingrace/p/6261091.html

https://www.cnblogs.com/kevingrace/p/6278200.html

https://www.cnblogs.com/kevingrace/p/5685511.html 当前

https://www.cnblogs.com/kevingrace/p/5685476.html

https://www.cnblogs.com/kevingrace/p/6710136.html 双主

https://www.cnblogs.com/kevingrace/category/796278.html 总

mysql+服务+监控_mysql从服务器监控相关推荐

  1. epel源mysql版本_linux增加epel源,yum安装nignx,脚本安装mysql服务端,shell脚本监控网站页面...

    epel是一种yum仓库,里面提供了更多.更丰富linux软件,但是,它不是默认yum仓库,我们需要手工添加这个yum源. 添加epel源 然后,找到fedora-epel, 在列表里找到:6serv ...

  2. linux增加epel源,yum安装nignx,脚本安装mysql服务端,shell脚本监控网站页面

    epel是一种yum仓库,里面提供了更多.更丰富linux软件,但是,它不是默认yum仓库,我们需要手工添加这个yum源. 添加epel源 参考:http://freeloda.blog.51cto. ...

  3. mysql动静分离_haproxy的web服务负载均衡、动静分离、 MySQL服务负载均衡、状态监控...

    实验环境:基于centos6.6 haproxy-Server:172.16.249.98  hostname:node1 upsteram server1:172.16.249.100 hostna ...

  4. Grafana Prometheus 服务安装部署(Linux服务器监控)

    文章目录 一.概述 二.安装Prometheus 1.安装node_exporter 2.安装Prometheus 三.安装Grafana展示监控 监控进程 一.概述 Prometheus 介绍 Pr ...

  5. mysql replication 监控_MySQL之-Replication监控及自动故障切换的详细分析

    1.服务器规划 Master:192.168.0.152 Slave: 192.168.0.153 192.168.0.154 监控服务器: 192.168.0.154 注意:真实生产环境最好有一台单 ...

  6. 无线监控设置smtp服务器,监控smtp服务器

    监控smtp服务器 内容精选 换一换 华为云提供了云监控服务,您可以使用该服务监控弹性云服务器,在您开通了弹性云服务器后,云监控服务将自动实时监控并根据您设置的告警预置触发告警和通知,帮助您更好地了解 ...

  7. 监控操作系统和服务器,监控操作系统和服务器

    监控操作系统和服务器 内容精选 换一换 监控是保持云耀云服务器可靠性.可用性和性能的重要部分,通过监控,用户可以观察云耀云服务器资源.为使用户更好地掌握自己的云耀云服务器运行状态,公有云平台提供了云监 ...

  8. 服务器可视化监控系统,可视化服务器监控系统

    可视化服务器监控系统 内容精选 换一换 精简视图提供了云服务器资源概况和状态的可视化统计结果,帮助您直观的了解云服务器资源.在精简视图中,您可以快速获取弹性云服务器基本信息.登录信息.配置信息.监控信 ...

  9. linux服务器监控信息,Linux 服务器监控

    概述 文字主要讲述使用linux自带的几个命令监控io.CPU.磁盘.内存.服务器整体信息等. IO监控 iostat命令 主要用于监控系统设备的IO负载情况 查看命令帮助 iostat --help ...

最新文章

  1. iOS-仿膜拜贴纸滚动(物理仿真)
  2. 沃尔沃加入英伟达阵营 2021年开卖自动驾驶汽车
  3. 开源工作流程引擎ccflow多人待办处理模式的详解
  4. SAP内存 和 ABAP内存 的简单介绍说明
  5. 国外学校css profile,CSS Print Profile
  6. latex中erro:extra alignment tab has changed to\cr但列数没错的解决方法
  7. Redis 集群使用(2)
  8. 关于PHP的工作流引擎
  9. Linux 系统的IP与域名解析文件[局域网的DNS]
  10. 从.NET中委托写法的演变谈开去(中):Lambda表达式及其优势
  11. [渝粤教育] 中国地质大学 思想道德修养与法律基础 复习题
  12. 手机12306买卧铺下铺技巧_12306订下铺有什么技巧?
  13. 将数组分为两部分,使得这两部分和最接近,返回这两部分的差值
  14. 一起看 I/O | 全新 Google Play SDK 索引助您为应用选择合适的 SDK
  15. MUI click事件无效 click没反应 click监听不到
  16. DPI和分辨率的转换
  17. DFT,ZT及DTFT之间的关系
  18. SDNU1026 田忌赛马续
  19. mysql搭建集群_Mysql搭建集群Mysql Cluster
  20. VUE中使用百度地图BaiduMap

热门文章

  1. BackBox Linux 3.05 发布
  2. redis解决了分布式系统的session一致性问题
  3. geohash美团附近酒店搜索-技术方案
  4. 16、document的_source元数据以及定制返回结果解析
  5. 中专计算机总结论文,中专计算机毕业论文...doc
  6. win10 flashcs6不能用了_听说这版win10马上不能用了?那就改装“它”,比win7还稳定流畅...
  7. eclipse项目如何变成web项目_Eclipse中将Java项目转换成Web项目的方法
  8. goland配置mysql失败_GoLand配置数据库、远程host以及远程调试
  9. java实验指导答案华软_Java核心编程技术实验指导教程
  10. java抓取屏幕像素,以像素为单位获取屏幕尺寸