mysql主從監控

	要求:檢測myslq從庫狀態,跳過固定的錯誤號,每隔30秒檢測一次,若是符合條件自動跳過或者是重啓從庫
	1)取出mysql從庫的關鍵字
	[root@localhost scripts]# mysql -u root -p123qq.com3307 -S /data/3307/mysql.sock -e "show slave status\G"|grep -E "Running|Seconds_Behind_Master|Last_SQL_Errno"|awk '{print $NF}'
	Yes
	Yes
	0
	0
	2)把錯誤號定義在數組裏面
	3)while ture
	根據思路調試出以下腳本。。。。。
	[root@localhost scripts]# cat check_mysql_slave.sh 
	#/bin/bash
	#Date:     2015-12-14 22:37
	#Author:   create by  李興利
	#Mail:     1162572407@qq.com
	#Function: This scripts  function is check mysql slave is ok
	#Version:  1.1
	qq="564039852@qq.com"
	cmd="mysql -u root -p123qq.com3307 -S /data/3307/mysql.sock -e"
	ip=`ifconfig eth2|sed -n 's#^.*ddr:\(.*\) Bc.*$#\1#gp'`
	skip=`$cmd "stop slave;set global sql_slave_skip_counter=1;start slave;"`
	error_numb=(1158 1159 1008 1007 1062)
	while true
	do
	status=(`$cmd "show slave status\G"|grep -E "Running|Seconds_Behind_Master|Last_SQL_Errno"|awk '{print $NF}'`)
	
	   if [ "${status[0]}"  == "Yes" -a "${status[1]}" == "Yes" -a "${status[2]}" == "0" ]
	       then
	           echo  "mysql slave is ok"
	       else
	           for ((n=0;n<${#error_numb[*]};n++))
	           do
	              if [ "${status[3]}" == "${error_numb[n]}" ];then
	              ${skip}
	              else
	               $cmd "stop slave;start slave;"
	              fi
	           done
	           echo  "mysql salve is not ok"
	           echo "${ip} mysql is not ok"|mail -s "mysql slave error at `date +%F%T`" $qq
	   fi
	sleep 3
	done
	[root@localhost scripts]# 
	測試結果以下
	[root@localhost scripts]# sh check_mysql_slave.sh 
	mysql salve is not ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok
	mysql slave is ok


更多內容請訪問 李興利博客
相關文章
相關標籤/搜索