The code is as follows:
[Java]View PlainCopy
- #!/bin/bash
- #Check MySQL Slave ' s runnning Status
- #Crontab time :
- Mysqlport= ' Netstat-na|grep "LISTEN" |grep "3306" |awk-f[:""]+ ' {print $} '
- mysqlip= ' ifconfig eth0|grep "inet addr" | awk-f[: ""]+ ' {print $4} '
- status=$ (/usr/bin/mysql-uroot-p11111-s/var/lib/mysql/mysql.sock-e "show slave status\g" | grep-i "Running")
- Io_env= ' echo $STATUS | grep IO | awk ' {print $} '
- Sql_env= ' echo $STATUS | grep SQL | awk ' {print $} '
- Data= ' date +'%y-%m-%d%h:%m:%s '
- function Checkmysqlstatus () {
- if [ "$MYSQLPORT" = = "3306"]
- Then
- /usr/bin/mysql-uroot-p11111--connect_timeout=5-e "show databases;" &>/dev/null 2>& 1
- if [$?-ne 0]
- Then
- Echo "Server: $MYSQLIP MySQL is off, please try the restart MySQL by manual!" >/var/log/mysqlerr
- Mail-s "warn! Server: $MYSQLIP MySQL is down. "Admin@yourdomain. com </var/log/mysqlerr
- Else
- echo "MySQL is running ..."
- Fi
- Else
- Mail-s "warn! Server: $MYSQLIP MySQL is down. "Admin@yourdomain. com
- Fi
- }
- Checkmysqlstatus
- If [ "$IO _env" = "Yes"-a "$SQL _env" = "yes"]
- Then
- echo "MySQL Slave is running!"
- Else
- echo "####### $DATA #########" >>/data/mysql/mysql_slave_status.log
- echo "MySQL Slave is not running!" >>/data/mysql/mysql_slave_status.log
- echo "MySQL Slave is not running!" | mail-s "warn! $MYSQLIP MySQL Slave is not running. "Admin@yourdomain. com
- Fi
Recommended to run every 10 minutes
*/10 * * * * root/bin/sh/root/mysql_slave_status.sh
Principle:
1) Monitor the running state of MySQL;
2) Slave machine IO and SQL status must be yes, indispensable;
Note that you use it in conjunction with your own actual environment.
Monitor MySQL master-slave synchronization configuration to monitor the script from the library running state