监控mysql主从数据一致,监控MySQL主从同步

脚本监控数据库主从同步

来源:http://oldboy.blog.51cto.com/2561410/1632876

来源:

(生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:

阶段1:开发一个守护进程脚本每30秒实现检测一次。

阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。

阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分

[root@oldboy~]# mysql -uroot -p'oldboy' -S /data/3307/mysql.sock -e "show slavestatus\G;"

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

Slave_IO_State:Waiting for master to send event

Master_Host:10.0.0.179   #当前的mysql master服务器主机

Master_User: rep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File:mysql-bin.000013

Read_Master_Log_Pos: 502547

Relay_Log_File:relay-bin.000013

Relay_Log_Pos:251

Relay_Master_Log_File:mysql-bin.000013

Slave_IO_Running:Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB: mysql

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: 502547

Relay_Log_Space:502986

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:

################################################################

################################################################

################################################################

################################################################

第一关解决

[root@LAMP script]# cat mysqlzcjk.sh

#!/bin/sh

. /etc/init.d/functions

while true

do

num=0

array=($(egrep "_Running|Behind_Master" slave.log |awk '{print $NF}' ))

for status in ${array[*]}

do

if [ "$status" = "Yes"  -o  "$status" = "0"  ]

then

let num=num+1

fi

done

if [ $num -eq 3 ]

then

action "MySQL slave is OK" /bin/true |tee success.log

cat success.log |mail -s "$char" [email protected]

else

action "MySQL slave is not OK" /bin/false | tee error.log

cat error.log |mail -s "$char" [email protected]

fi

sleep 30

done

##在生成情况,用下面命令取主从同步状态

##mysql -uroot -plvnian -e "show slave status\G;"|egrep "_Running|Behind_Master" |awk '{print $NF}'

=================

[root@LAMP script]# sh mysqlzcjk.sh

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is not OK                                      [FAILED]

MySQL slave is OK                                          [  OK  ]

MySQL slave is OK                                          [  OK  ]

#######################################################################

#######################################################################

第二第三关

#!/bin/sh

. /etc/init.d/functions

while true

do

num=0

errorno=(1158 1159 1007 1008 1062)

array=($(egrep "_Running|Behind_Master|SQL_Errno" slave.log |awk '{print $NF}' ))

for status in ${array[*]}

do

if [ "$status" = "Yes"  -o  "$status" = "0"  ]

then

let num=num+1

fi

done

if [ $num -eq 3 ]

then

action "MySQL slave is OK" /bin/true |tee success.log

cat success.log |mail -s "$char" [email protected]

else

action "MySQL slave is not OK" /bin/false | tee error.log

cat error.log |mail -s "$char" [email protected]

for ((i=0;i

if [ $array[3] -eq ${errorno[$i]} ]

then

mysql -uroot -plvnian -e "stop slave && set global sql_slave_skip_counter=1;start slave;"

fi

fi

sleep 10

done

##在生成情况,用下面命令取主从同步状态

##mysql -uroot -plvnian -e "show slave status\G;"|egrep "_Running|Behind_Master|SQL_Errno" |awk '{print $NF}'

#######################################################################

#######################################################################

测试命令:

stop slave sql_thread;

start slave sql_thread;

stop slave io_thread ;

start slave io_thread ;

stop slave;

start slave;

你可能感兴趣的:(监控mysql主从数据一致)