CentOS下編寫shell劇本來監控MySQL主從復制的教程。本站提示廣大學習愛好者:(CentOS下編寫shell劇本來監控MySQL主從復制的教程)文章只能為提供參考,不一定能成為您想要的結果。以下是CentOS下編寫shell劇本來監控MySQL主從復制的教程正文
目標:准時監控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! **********************************************************