mysql主从监控脚本

转自https://blog.51cto.com/oldboy/1652086
参考一

#!/bin/sh
#oldboy linux training
#2015-05-17
#说明:本脚本来自老男孩linux21期学员张耀开发!
# Source function library.
. /etc/init.d/functions

# Defined variables
MysqlUser=root
MysqlPass=oldboy123
MysqlPort=3307
Mysqlsock=/data/$MysqlPort/mysql.sock
ErrorNo=(1158 1159 1008 1007 1062)
errorlog=/tmp/error_skip.log
MysqlCmd="/application/mysql/bin/mysql -u$MysqlUser -p$MysqlPass -S $Mysqlsock"

# Judge mysql server is ok?
[ -S $Mysqlsock ] ||{
	echo "Maybe MySQL have someting wrong"
	exit 1
}

# Defined skip error Functions
function error_skip(){
	local flag
	flag=0
	for num in ${ErrorNo[@]}
	  do
	    if [ "$1" == "$num" ];then
	       $MysqlCmd -e'stop slave;set global sql_slave_skip_counter=1;start slave;'
	       echo "$(date +%F_%R) $1" >>$errorlog
	    else
           echo "$(date +%F_%R) $1" >>$errorlog
	       ((flag++))
	    fi
	done
	[ "$flag" == "${#ErrorNo[@]}" ] &&{
		action "MySQL Slave" /bin/false
		uniq $errorlog|mail -s "MySQL Slave is error" 12345678@qq.com
	}
}

# Defined check slave Functions
function check_slave(){
	MyResult=`$MysqlCmd -e'show slave status\G'|egrep '_Running|Behind_Master|SQL_Errno' |awk '{print $NF}'`
	array=($MyResult)
  if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[2]}" == "0" ]
    then
    action "MySQL Slave" /bin/true
  else
    error_skip ${array[3]}
  fi
}

# Defined main Functions
function main(){
	while true
	   do
	     check_slave
	     sleep 60
	done
}
main

参考二

#!/bin/sh
#oldboy linux
#2015-05-17
#说明:本脚本来自老男孩教育21期学员李新宇!
USER=root
PASSWORD=123456
PORT=3307
error=(1158 1159 1008 1007 1062)
MYSQLCMD="mysql -u$USER -p$PASSWORD -S /data/$PORT/mysql.sock"

is_run(){
  [ `lsof -i:$PORT|wc -l` -lt 2 ]&&{
    echo "mysql server is stopped"
    exit 1
  }
}

status_array(){
 status=($($MYSQLCMD -e "show slave status\G"|egrep "_Running|Last_Errno|Behind_Master"|awk '{print $NF}'))
}

status_error(){
for((i=0;i<${#error[*]};i++))
do
  if [ "$1" == "${error[$i]}" ]
    then
      $MYSQLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
  else 
      echo "MySQL slave is failed, errorno is $1"
  fi
done
}

judge_slave(){
  status_array
  if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a "${status[3]}" == "0" ]
  then
    echo "MySQL slave is ok"
  else
    status_error ${status[2]}
  fi
}

main(){
    counter=$(netstat -na|grep "LISTEN"|grep "3307"|wc -l)
    if [ "${counter}" -ne 0 ]; then
	    judge_slave
    else
        echo "$(date +%F_%R:%S) mysql server is stopped" >> $errorlog 2>&1
        exit 1
    fi
}
main
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值