首页 > 数据库 > MySQL > 正文

CentOS下编写shell脚本来监控MySQL主从复制的教程

2024-07-24 13:08:23
字体:
来源:转载
供稿:网友
这篇文章主要介绍了在CentOS系统下编写shell脚本来监控主从复制的教程,文中举了两个发现故障后再次执行复制命令的例子,需要的朋友可以参考下
 

目的:定时监控MySQL主从数据库是否同步,如果不同步,记录故障时间,并执行命令使主从恢复同步状态

1、创建脚本文件

vi /home/crontab/check_mysql_slave.sh  #编辑,添加下面代码
#!/bin/sh# check_mysql_slave statusip=eth0 #网卡名称mysql_binfile=/usr/local/mysql/bin/mysqlmysql_user=root #MySQL数据库账号mysql_pass=123456 #密码mysql_sockfile=/tmp/mysql.sockdatetime=`date +"%Y-%m-%d/%H:%M:%S"`  #获取当前时间mysql_slave_logfile=/home/logs/check_mysql_slave.log  #日志文件路径,必须提前创建好slave_ip=`ifconfig $ip|grep "inet addr" | awk -F[:" "]+ '{print $4}'`status=$($mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "show slave status/G" | grep -i "running")Slave_IO_Running=`echo $status | grep Slave_IO_Running | awk ' {print $2}'`Slave_SQL_Running=`echo $status | grep Slave_SQL_Running | awk '{print $2}'`if [ "$Slave_IO_Running" = "Yes" -a "$Slave_SQL_Running" = "Yes" ]then echo "Slave is Running!"elseecho " $datetime $slave_ip Slave is not running!" >> $mysql_slave_logfile$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SLAVE STOP;"$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1;"$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "SLAVE START;"$mysql_binfile -u$mysql_user -p$mysql_pass -S $mysql_sockfile -e "EXIT"fi
:wq! #保存退出chmod +x /home/crontab/check_mysql_slave.sh #添加脚本执行权限

2、添加任务计划,修改/etc/crontab

vi /etc/crontab #在最后一行添加*/10 * * * * root /home/crontab/check_mysql_slave.sh #表示每10分钟执行一次:wq! #保存退出

3、重新启动crond使设置生效

/etc/rc.d/init.d/crond restart  #yum install -y vixie-cron安装计划任务,某些系统上可能没有预装chkconfig crond on #设为开机启动service crond start #启动

可以根据日志文件/home/logs/check_mysql_slave.log查看MySQL主从同步状态

PS:接下来这个脚本增加了“当发现同步出现无法同步的时候”会自动提取主库的file号,以及pos,进行同步主库,脚本内容如下:
 

#!/bin/sh#set -x#file is slave_repl.sh#Author by Kevin#date is 2011-11-13mstool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.106 -uroot -pw!zl7POg27 -P 3307"sltool="/usr/local/mysql-3307/bin/mysql -h 192.168.1.107 -uroot -pw!zl7POg27 -P 3307"declare -a slave_statslave_stat=($($sltool -e "show slave status/G"|grep Running |awk '{print $2}'))if [ "${slave_stat[0]}" = "Yes" -a "${slave_stat[1]}" = "Yes" ]   then   echo "OK slave is running"   exit 0else   echo "Critical slave is error"   echo   echo "*********************************************************"   echo "Now Starting replication with Master Mysql!"    file=`$mstool -e "show master status/G"|grep "File"|awk '{print $2}'`     pos=`$mstool -e "show master status/G"|grep "Pos"|awk '{print $2}'`     $sltool -e "slave stop;change master to master_host='192.168.1.106',master_port=3307,master_user='repl',master_password='w!zl7POg27',master_log_file='$file',master_log_pos=$pos;slave start;"    sleep 3    $sltool -e "show slave status/G;"|grep Running  echo  echo "Now Replication is Finished!"  echo  echo "**********************************************************"    exit 2fi
 
运行后效果,如下:
 
# ./slave_stop3307.sh *******************************Now stop Slave Replication!      Slave_IO_Running: No     Slave_SQL_Running: No*******************************# ./slave_repl3307.sh Critical slave is error*********************************************************Now Starting replication with Master Mysql!      Slave_IO_Running: Yes     Slave_SQL_Running: YesNow Replication is Finished!**********************************************************
 


注:相关教程知识阅读请移步到MYSQL教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表