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

前端之家收集整理的这篇文章主要介绍了php在linux下检测mysql同步状态的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP在linux下检测MysqL同步状态的方法分享给大家供大家参考。具体分析如下:

这里通过两个实例来介绍MysqL同步状态检测实现方法代码如下:

代码如下:
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
<div class="codetitle"><a style="CURSOR: pointer" data="74274" class="copybut" id="copybut74274" onclick="doCopy('code74274')"> 代码如下:
<div class="codebody" id="code74274">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_IOErrno"] . ")";
}
if($res["Slave
sqlRunning"] <> "Yes"){
$memo .= $res["Last
sql_Error"] . "(" . $res

["Last_sql_Errno"] . ")";
}
break;
case 2:
$memo = "数据库未设置同步";
break;
}
echo "

n";
}else{
echo "

n";
}
}
echo "
<table border="">

sql_Runningsql_Runningsql_Errnosql_Errorsql_Running']}sql_Running']}sql_Errno']}sql_Error']} END;
}else{
die("no MysqL instances defined.");
}

check_status_api.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="52248" class="copybut" id="copybut52248" onclick="doCopy('code52248')"> 代码如下:
<div class="codebody" id="code52248">/*

  • 检查mysql服务器的同步状态
    /
    function check_mysql_replication_status($host,$username,$password)
    {
    //默认状态未知
    $r = array(
    "result" => -1
    );
    try{
    $dbh = @MysqL_connect($host,$password);
    if(!$dbh){
    //无法连接
    $r["result"] = -2;
    return($r);
    }
    $query = "SHOW SLAVE STATUS";
    $res = @MysqL_query($query,$dbh);
    $err = @MysqL_error();
    if($err){
    //无法连接
    $r["result"] = -3;
    return($r);
    }
    $row = MysqL_fetch_array($res);
    $r = $row;
    if(($r["Slave_IORunning"] == "Yes") && ($r["Slavesql_Running"] == "Yes"))
    {
    $r["result"] = 0;
    }else{
    if(!emptyempty($row)){
    $r["result"] = 1;
    }else{
    $r["result"] = 2;
    }
    }
    }catch(Exception $e){
    $r["result"] = -4;
    }
    return($r);
    }

MysqL_instance.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="91769" class="copybut" id="copybut91769" onclick="doCopy('code91769')"> 代码如下:
<div class="codebody" id="code91769">//GRANT REPLICATION CLIENT ON .* TO '用户名'@'监控主机ip' IDENTIFIED BY '密码'; $MysqL_instances =

array();
$MysqL_instances[] = "远程ip:端口";

function get_instances() { global $MysqL_instances; return $MysqL_instances; }

将以上三个PHP文件放在虚拟目录中,然后通过URL访问.

访问方式:http://ip/check_repl.PHP?key=xupeng

希望本文所述对大家的PHP程序设计有所帮助。

linuxlinuxmysqlphp同步状态方法方法方法检测检测检测

猜你在找的PHP相关文章