Add a script to monitor the slave database running status in the MYSQL master-slave synchronization configuration.
The Code is as follows:
- #! /Bin/bash
- # Check MySQL Slave's Runnning Status
- # Crontab time00:10
-
- MYSQLPORT = 'netstat-na | grep"LISTEN"| Grep"3306"| Awk-F [:""] +'{Print $5 }''
- MYSQLIP = 'ifconfig eth0 | grep"Inet addr"| Awk-F [:""] +'{Print $4 }''
- STATUS = $ (/usr/bin/mysql-uroot-p11111-S/var/lib/mysql. sock-e"Show slave status \ G"| 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"'
-
- Function checkMysqlStatus (){
- If["$ MYSQLPORT"="3306"]
- Then
- /Usr/bin/mysql-uroot-p11111 -- connect_timeout =5-E"Show databases ;"&>/Dev/Null 2> &1
- If[$? -Ne0]
- Then
- Echo"Server: $ MYSQLIP mysql is down, please try to 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"-"$ 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
We recommend that you run the task every 10 minutes.
*/10 * root/bin/sh/root/mysql_slave_status.sh
Principle:
1) Monitor the running status of MYSQL;
2) The IO and SQL statuses of the Server Load balancer must be YES;
Note: use it in combination with your actual environment.