目的:定時監控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! **********************************************************