MySQL5.7和8.0支持通过 replication_applier_status 表获同步延迟时间,当从库出现延迟后,该表中的字段 REMAINING_DELAY 记录延迟秒数,当没有延迟时,该字段值为NULL,官方对该字段介绍如下:
REMAINING_DELAY If the replica is waiting for DESIRED_DELAY seconds to pass since the source applied an event, this field contains the number of delay seconds remaining. At other times, this field is NULL. (The DESIRED_DELAY value is stored in the replication_applier_configuration table.)
官方介绍:https://dev.mysql.com/doc/mysql-perfschema-excerpt/5.7/en/performance-schema-replication-applier-status-table.html
因此,我们可以监控该字段判断是否有延迟,当有延迟时,通知相关人处理。
最简单的办法是通过现有的监控工具来监控。
第一步:
注册监控账号,有邮箱就能注册,很简单,网址:https://dataalert.cn/
第二步:
选择数据源,点击右上角添加


注意:数据源中的数据库需要指定 performance_schema
第三步:
选择监控配置,点击右上角添加


上面配置表示从早上8:00到晚上20:59之间,每10分钟监控一次 replication_applier_status 表中的 REMAINING_DELAY 字段的值。
第四步:
配置异常告警,点击告警规则进行配置


上面配置表示,只要监控的REMAINING_DELAY值大于1,就会进行报警。
第五步:
配置报警接收人,先配置订阅者,再到监控配置中选择订阅

注:订阅地址为钉钉群的自定义机器人Webhook,获取方式参考(只看文章中的步骤一): https://open.dingtalk.com/document/robots/custom-robot-access


选择上一步配置好的订阅者即可,此时,当有告警时会通知到对应的钉钉群,可以在群里直接进行告警后续出处理。
至此,监控搞定~
你也可以在首页看到相关统计数据即告警信息等。

当然,该方法除了监控MySQL同步延迟外,还可监控所有基于数据库的数据,如订单量监控,新增用户数监控等各种业务指标监控,对监控的数据也可以配置报告,定时推送监控指标。
传送门:https://dataalert.cn
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |