Home >Backend Development >PHP Tutorial >Detecting mysql synchronization status implementation code (php/linux)_PHP tutorial
This article introduces two examples to introduce the MySQL synchronization status detection implementation program. Friends in need can refer to it.
The code is as follows | Copy code | ||||
#check MySQL_Slave Status #crontab time 00:10 MYSQL_USER="root" MYSQL_PWD= "123456" MYSQL_SLAVE_LOG="/tmp/check_mysql_slave.log" EMAIL="1351010****@139.com" MYSQL_PORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'` MYSQL_IP=`ifconfig eth0|grep "inet addr" | awk -F[:" "]+ '{print $4}'' MYSQL_SLAVE_STATUS=$(/usr/local/webserver/mysql/bin/mysql -u root -psylc23hua -S /tmp/mysql.sock -e" show slave statusG" | grep -i "running") IO_ENV=`echo $MYSQL_SLAVE_STATUS | grep IO | awk ' {print $2}'` SQL_ENV=`echo $MYSQL_SLAVE_STATUS | grep SQL | awk ' {print $2}'` NOW=$(date -d today +'%Y-%m-%d %H:%M:%S') if [ "$ MYSQL_PORT" = "3306" ];then echo "mysql is running!" else mail -s "warn!server: $MYSQL_IP mysql is down" "$EMAIL" fi if [ "$IO_ENV" = "Yes" -a "$SQL_ENV" = "Yes" ];then echo "Slave is running!" else echo "[ $NOW ] Slave is not running!" >> "$MYSQL_SLAVE_LOG" cat "$MYSQL_SLAVE_LOG" | mail -s "WARN! ${MySQL_IP}_replicate_error" "$EMAIL"fi exit 0 |
php example code
The code is as follows | Copy code | ||||||||||||||||||||||||||||||||||||||||
check_rep.php if(empty($_REQUEST["key"])) die(':) missing key'); include("mysql_instance.php"); define("USERNAME", "Username"); $instances = get_instances() ; if($instances){ END; n"; n "; $memo .= $res["Last_SQL_Error"] . "(" . $res case 2: n";
n"; echo << END; /* |
Place the above three PHP files in the virtual directory and access them via URL.
Access method: http://ip/check_repl.php?key=xupeng