内容为本身的一点总结,若有不对欢迎狠劲儿拍砖
mysql
本文来自http://yijiu.blog.51cto.com/转载请经博主赞成ios
监控主从复制正常与否sql
相比各位都应该知道,监控主从是否工做正常,涉及命令以下:
shell
show slave status\G;
那么,咱们须要关注的参数以下:
bash
1. 首先查看SQL和IO线程是否为YES状态(想必各位都明白了)ide
2. 是否有延迟 是否大于0 #通常生成环境延迟是否大于500秒,若是大于500则报警,如大于1000则严重报警函数
#好比传递一个sql到slave上,binlog中在eventhear中存在time stamp时间戳,在下条binlog拿到的时间会进行比较,若是当前时间是多少则显示多少,若是更新很是频繁500秒会产生更多的sql积累在其中spa
至少生产中监控就是这么实现的以及包括nagios的监控插件也是这么实现的插件
主要关注的值本文来自http://yijiu.blog.51cto.com/转载请经博主赞成,线程
1. Master_Log_File Read_master_log_Pos 2. Relay_Master_Log_File Exec_Master_log_pos 3. Seconds_Behind_master
判断一个库主要观察以上几点,若是主库挂了,Seconds_Behind_master 会已经成为NULL了
那么这样如何去观测从库是否日志同步完成,以下所示
经过shell实现监控同步的方法
废话很少说了直接上菜
1.利用status去观测是否已经同步完成
判断公式
如下为判断依据,判断如下值
Master_Log_File 和 Relay_Master_Log_File 的值必须相等
判断同步的偏移量
Read_master_log_Pos 和 Exec_Master_log_pos 的值必须相等
根据以上为最基础的判断依据,是否可将其从库提高为主库,就会在从库中判断master log file 是否读到的位置同样并找到一个最靠前的一个节点提高为主
shell内容以下所示:本文来自http://yijiu.blog.51cto.com/转载请经博主赞成
#!/bin/bash user='root' password='mypass' thread_status=`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql_3308.sock -e 'show slave status\G'|grep -i yes|wc -l` status=(`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql_3308.sock -e 'show slave status\G'| egrep -i "Master_Log_File|Relay_Master_Log_File|Read_master_log_Pos|Exec_Master_log_pos|Seconds_Behind_Master" |awk -F':' '{print $2}'`) echo ${status[4]} if [[ "$thread_status" != 2 ]]; then echo "the Replication is Fault , at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog exit 1 fi if [[ "${status[4]}" > '300' ]];then echo "yan chi guo gao, at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog exit 2 fi if [[ ${status[0]} == ${status[2]} ]] && [[ ${status[1]} == ${status[3]} ]]; then echo 'The Replication is Normal' exit 0 else echo "the Replication is Fault , at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog exit 2 fi
本文来自http://yijiu.blog.51cto.com/转载请经博主赞成
2.依赖于程序检测 本文来自http://yijiu.blog.51cto.com/转载请经博主赞成
好比程序在master创建表,并随意设置字段,并在master上获取一个时间并写入
now的时间在程序中自行获得并记录,最后在slave中执行select 查看结果是否与时间对应一致
若是时间同样则认为正常,若是master上的时间减去slave上的时间 出现了延迟,那么证实延迟存在的,可是这种方法存在缺陷,好比主库挂了那么则不可用
#!/bin/bash user='root' password='mypass' /usr/local/mysql/bin/mysql -u"$user" -p"$password" -e 'replace into master.repl_heart set t=now(),id=1;' >/dev/null 2>&1 master_select=`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql.sock -e 'select t from master.repl_heart where id=1;' | awk '{print $2}' | tail -1` slave_select=`/usr/local/mysql/bin/mysql -u"$user" -p"$password" -S /tmp/mysql_3308.sock -e 'select t from master.repl_heart where id=1;' | awk '{print $2}' | tail -1` master_date=`date -d "$master_select" +%s` slave_date=`date -d "$slave_select" +%s` delay=`echo "$master_date"-"$slave_date" | bc` if [[ $master_date == $slave_date ]];then echo 'is ok' elif [[ $delay -le 500 ]];then echo cun zai yan chi "$delay" else echo "the Replication delay too large "$delay" , at $(date)" > $catalog echo `uname -n` | mail umail@qq.com < $catalog fi
须要注意的是:复制中,若是是行格式,就是主库的时间;若是不是行式,这个方法能够把now()这个内置函数在s脚本中生成再写入
以上,为监控mysql主从的两种shell的写法,若有不足,麻烦指出,感谢各位