CentOS下編寫shell指令碼來監控MySQL主從複製的教程_Mysql

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.