本文实例讲述了PHP在linux下检测MysqL同步状态的方法。分享给大家供大家参考。具体分析如下:
这里通过两个实例来介绍MysqL同步状态检测实现方法。代码如下:
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实例代码,代码如下:
check_rep.PHP:
<div class="codetitle"><a style="CURSOR: pointer" data="74274" class="copybut" id="copybut74274" onclick="doCopy('code74274')"> 代码如下: