对于MySQL数据库主从复制延迟的监控,我们可以借助percona的有力武器pt-heartbeat来实现。pt-heartbeat通过使用时间戳方式在主库上更新特定表,然后在从库上读取被更新的时间戳然后与本地系统时间对比来得出其延迟。本文主要是通过脚本来定期检查从库与主库复制的延迟度并发送邮件,供大家参考。
有关pt-heartbeat工具的安装可以参考:percona-toolkit的安装及简介
有关pt-heartbeat工具的介绍可以参考:使用pt-heartbeat监控主从复制延迟
1、脚本概述
a、脚本定期使用--check方式单次检查当前的延迟性(定期的方式可以使用cron job比如每1分钟或5分钟)
b、通过设定指定的延迟阀值来判断当时的延迟性是否在可控范围
c、一旦当前的延迟大于指定阀值,则马上使用--monitor方式不停的监控其延迟性并写入到日志文件
d、对于--monitor方式,其进程运行超过30分钟,自kill其进程,以避免无限期运行导致日志过大,空间不够用
2、脚本内容
[mysql@SZDB run]$ more ck_slave_lag.sh #!/bin/bash #set -x if [ $# -ne 3 ];then echo "usage:" echo "ck_slave_lag.sh <Servier-id> <MaxLag> <LogDir>" exit 0; fi # Author : Leshami # Blog : http://www.php.cn/ ServerID=$1 MaxLag=$2 LogDir=$3 Timestamp=`date +%Y%m%d_%H%M%S` Rentition=7 LogFile=$LogDir/slave_lag_$Timestamp.log LagDetail=$LogDir/slave_lag_Detail_$Timestamp.log mailadd=leshami@12306.cn echo $ServerID echo $MaxLag echo $LogDir echo $LogFile echo $LagDetail echo $mailadd if [ ! -d $LogDir ];then mkdir -p $LogDir fi Lag=`/usr/bin/pt-heartbeat --user=monitor --password=xxx -S /tmp/mysql.sock -D test --master-server-id=$ServerID --check` Lag=`echo ${Lag%.*}` #Lag=3 echo $Lag ptStatus=`ps -ef|grep pt-heart|grep daemonize` echo $ptStatus if [ $Lag -gt $MaxLag ]; then echo "The current date is `date` at `hostname`." >>$LogFile echo "The current lag log file is $LogFile." >>$LogFile echo "The current replication lag is $Lag." >>$LogFile echo "The replication lag is larger than max lag $MaxLag." >>$LogFile if [ -z "$ptStatus" ] ; then echo "Start a monitor daemon with below command: " >>$LogFile echo "pt-heartbeat --user=monitor --password=xxx -S /tmp/mysql.sock -D test " >>$LogFile echo " --master-server-id=11 --monitor --print-master-server-id --daemonize --log=$LagDetail" >>$LogFile /usr/bin/pt-heartbeat --user=monitor --password=xxx -S /tmp/mysql.sock -D test \ --master-server-id=$ServerID --monitor --print-master-server-id --daemonize --log=$LagDetail echo "More detail please check lag log from $LagDetail." >>$LogFile cat $LogFile | mutt -s "Found slave lag on `hostname`." $mailadd fi fi if [ -n "$ptStatus" ] ; then STime=`ps -ef|grep pt-heart|grep daemonize |gawk '{print $5}'` Pid=`ps -ef|grep pt-heart|grep daemonize |gawk '{print $2}'` STime=`date '+%Y%m%d'`" "$STime s_STime=`date -d "$STime" '+%s'` s_ETime=`date +%s` DiffSec=`expr $s_ETime - $s_STime` echo $STime echo $s_STime echo $s_ETime echo $DiffSec if [ "$DiffSec" -gt 1800 ]; then echo "kill -9 $Pid" kill -9 $Pid fi fi # Remove history slave lag log. find $LogDir -name "*slave_lag*" -ctime +$Rentition -delete exit
3、部署参考
[mysql@SZDB run]$ crontab -l #check slave lag */1 * * * * /run/ck_slave_lag.sh 11 3 /log/SlaveLag
以上就是MySQL 主从延迟监控脚本(pt-heartbeat)的内容,更多相关内容请关注PHP中文网(www.php.cn)!

掌握添加MySQL用户的方法对于数据库管理员和开发者至关重要,因为它确保数据库的安全性和访问控制。1)使用CREATEUSER命令创建新用户,2)通过GRANT命令分配权限,3)使用FLUSHPRIVILEGES确保权限生效,4)定期审计和清理用户账户以维护性能和安全。

chosecharforfixed-lengthdata,varcharforvariable-lengthdata,andtextforlargetextfield.1)chariseffity forconsistent-lengthdatalikecodes.2)varcharsuitsvariable-lengthdatalikenames,ballancingflexibilitibility andperformance.3)

在MySQL中处理字符串数据类型和索引的最佳实践包括:1)选择合适的字符串类型,如CHAR用于固定长度,VARCHAR用于可变长度,TEXT用于大文本;2)谨慎索引,避免过度索引,针对常用查询创建索引;3)使用前缀索引和全文索引优化长字符串搜索;4)定期监控和优化索引,保持索引小巧高效。通过这些方法,可以在读取和写入性能之间取得平衡,提升数据库效率。

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

tostorestringsefliceflicyInmySql,ChooSetherightDataTypeBasedyOrneOrneEds:1)USEcharforFixed-LengthStstringStringStringSlikeCountryCodes.2)UseVarcharforvariable-lengtthslikenames.3)USETEXTCONTENT.3)

选择MySQL的BLOB和TEXT数据类型时,BLOB适合存储二进制数据,TEXT适合存储文本数据。1)BLOB适用于图片、音频等二进制数据,2)TEXT适用于文章、评论等文本数据,选择时需考虑数据性质和性能优化。

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

mySqlStringDatatatPessHouldBechoseBeadeDataCharacteristicsAndUsecases:1)USECHARFORFIXED LENGTHSTRINGSTRINGSLIKECOUNTRYCODES.2)USEDES.2)usevarcharforvariable-lengtthstringstringstringstringstringstringstringslikenames.3)usebinaryorvarrinaryorvarinarydatalbonydatalgebgeenfopical.4)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

记事本++7.3.1
好用且免费的代码编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具