概述
因生產環境是MySQL主從,而有時從庫掛掉,缺乏有效的監控。為了及時發現從庫故障,所以簡單寫了個mysql從庫檢測腳本,并發郵件通知,有待完善。
mysql主從監控腳本
其實思路很簡單,查看MySQL主從狀態應該都會,就是進入MySQL,然后show master status和show slave status G,然后我要做的其實就是通過腳本定時進入MySQL然后查看狀態得到信息,再通過grep和awk對文本進行解析,然后關鍵的slave_sql_running、slave_io_running和last_error判斷后,異常則發送郵件。
#!/bin/bash #copyright by hwb #check MySQL_Slave Status #crontab time 00:10 MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $4}'` MYSQLIP=`ifconfig | grep inet |grep -v 'inet6'|grep -v '127.0.0.1'|awk -F ' ' '{print $2}'` STATUS=$(mysql -u root -pXXX -e "show slave statusG" | grep -i "running") IO_env=`echo $STATUS | grep IO | awk ' {print $2}'` SQL_env=`echo $STATUS | grep SQL | awk '{print $2}'` DATA=`date +"%y%m%d-%H:%M:%S"` if [ "$MYSQLPORT" == "3306" ] then echo "mysql is running" else mail -s "warn!server: $MYSQLIP mysql is down" huangwb@fslgz.com fi if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ] then echo "Slave is running!" else echo "####### $DATA #########">> /home/check_mysql_slave.log echo "Slave is not running!" >> /home/check_mysql_slave.log echo "Slave is not running!" | mail -s "warn!$MYSQLIP MySQL Slave is not running" huangwb@fslgz.com fi
輸出結果:
停止mysql后:
設置定時任務
0 */5 * * * sh /home/scripts/mysql.sh