脚本查看mysql主从_用shell脚本监控MySQL主从同步

#!/bin/bash

#-------------CopyRight-------------# Name:MySQL Check master and slave

# Version Number:1.00# Type:sh

# Language:bash shell

# Date:2018-05-09# Author:xubing

# QQ:442656067# Email:eeexu123@163.com

# Blog:https://www.cnblogs.com/eeexu123/

Port=3307 //MySQL端口User="root" //MySQL用户Password="oldboy123" //MySQL用户密码Mysql_sock="/data/${Port}/mysql.sock" //mysql.sock随着每次MySQL启动而生成Mysql_cmd="/application/mysql/bin/mysql -u${User} -p${Password} -S $Mysql_sock -e" //MySQL非交互命令Error_file="/tmp/mysql_check_error.log" //错误输入文件#source functions libary

./etc/init.d/functions

#check mysql server //检查MySQL是否启动

[-e $Mysql_sock ]||{

echo"The Mysql server no start"exit1}

#function ingore errors //忽略错误函数

skip_errors(){

array=(1158 1159 1008 1007 1062)

flag=0

for num in ${array[*]}do

if [ "$1" = "$num"];then //如果有对应的错误号,MySQL就跳过此错误号

${Mysql_cmd}"stop slave;set global sql_slave_skip_counter = 1;start slave;"echo"Last_IO_Errno:$1">>$Error_fileelseecho"Last_IO_Errno:$1">>$Error_file

((flag++)) //如果此错误号不在array数组中,将此错误号写入错误文件中,并循环五次

fi

doneif [ $flag =${#array[@]} ];then //发送邮件

echo"**********`date +%F_%T`************">>$Error_file

uniq $Error_file|mail -s "Mysql Slave error" eeexu123@163.com

fi

}

#check mysql slave //检查从库是否正常

check_mysql(){

array1=(`${Mysql_cmd} "show slave status\G"|egrep "Slave_IO_Running|Slave_SQL_Running|Last_SQL_Errno|Seconds_Behind_Master"|awk '{print $NF}'`)if [ "${array1[0]}" = "Yes" -a "${array1[1]}" == "Yes" -a "${array1[2]}" = 0];then

action"Mysql Slave" /bin/true

elseaction"Mysql Slave" /bin/false

if [ "${array1[0]}" != "Yes"];then //将上述错误的列写入到错误文件中

${Mysql_cmd}"show slave status\G"|grep "Slave_IO_Running">>$Error_file

elif ["${array1[1]}" != "Yes"];then

${Mysql_cmd}"show slave status\G"|grep "Slave_SQL_Running"|grep -v "Slave_SQL_Running_State">>$Error_fileelse [ "${array1[2]}" != 0]

${Mysql_cmd}"show slave status\G"|grep "Seconds_Behind_Master">>$Error_file

fi

skip_errors ${array1[3]} //发送邮件

fi

}

main(){while true

docheck_mysql

sleep60done

}

main

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值