欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-25 22:01:45
目的:定时监控mysql主从数据库是否同步,如果不同步,记录故障时间,并执行命令使主从恢复同步状态 1、创建脚本文件 vi /home/crontab/chec...

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

1、创建脚本文件

vi /home/crontab/check_mysql_slave.sh  #编辑,添加下面代码

#!/bin/sh

# check_mysql_slave status

ip=eth0 #网卡名称

mysql_binfile=/usr/local/mysql/bin/mysql

mysql_user=root #mysql数据库账号

mysql_pass=123456 #密码

mysql_sockfile=/tmp/mysql.sock

datetime=`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!"

else

echo " $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-13

mstool="/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_stat
slave_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 0
else
   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 2
fi
 
运行后效果,如下:
 
# ./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: yes
now replication is finished!
**********************************************************