您好,欢迎访问一九零五行业门户网

php在linux下检测mysql同步状态的方法

这篇文章主要介绍了php在linux下检测mysql同步状态的方法,是linux下使用php检测mysql同步状态的实用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
本文实例讲述了php在linux下检测mysql同步状态的方法。分享给大家供大家参考。具体分析如下:
这里通过两个实例来介绍mysql同步状态检测实现方法。代码如下:
复制代码 代码如下:
#!/bin/sh
#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实例代码,代码如下:
check_rep.php:
复制代码 代码如下:
if(emptyempty($_request[key])) die(':) missing key');
if($_request[key] != 'xupeng') die(':) error key');
include(mysql_instance.php);
include(check_status_api.php);
define(username, 用户名);
define(password, 密码);
define(debugmode, false);
$instances = get_instances();
if($instances){
 echo
end;
 echo
n;
 if(!debugmode){
  echo
n;
 }else{
  echo
n;
 }
 foreach($instances as $host){
  $res = check_mysql_replication_status($host, username, password);
  if(!debugmode){
   switch($res[result]){
    case -4:
     $memo = 未知异常;
     break;
    case -3:
     $memo = 查询失败;
     break;
    case -2:
     $memo = 无法连接端口;
     break;
    case -1:
     $memo = 状态未知;
     break;
    case 0:
     $memo = ok;
     break;
    case 1:
     $memo = 同步失败;
     if($res[slave_io_running] yes){
      $memo .= $res[last_io_error] . ( .  $res
[last_io_errno] . );
     }
     if($res[slave_sql_running] yes){
      $memo .= $res[last_sql_error] . ( .  $res
[last_sql_errno] . );
     }
     break;
    case 2:
     $memo = 数据库未设置同步;
     break;
   }
   echo
n;
  }else{
   echo
n;
  }
 }
 echo
instance
result
slave_io_running
slave_sql_running
master_host
master_port
replicate_do_db
memo
instance
result
slave_io_running
slave_sql_running
master_host
master_port
replicate_do_db
slave_io_state
last_io_errno
last_io_error
last_sql_errno
last_sql_error
{$host}
{$res['result']}
{$res['slave_io_running']}
{$res['slave_sql_running']}
{$res['master_host']}
{$res['master_port']}
{$res['replicate_do_db']}
{$memo}
{$host}
{$res['result']}
{$res['slave_io_running']}
{$res['slave_sql_running']}
{$res['master_host']}
{$res['master_port']}
{$res['replicate_do_db']}
{$res['slave_io_state']}
{$res['last_io_errno']}
{$res['last_io_error']}
{$res['last_sql_errno']}
{$res['last_sql_error']}
n;
 echo  
end;
}else{
 die(no mysql instances defined.);
}
check_status_api.php:
复制代码 代码如下:
其它类似信息

推荐信息