php在linux下檢測mysql同步狀態的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了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 <<
 
<!-- 30分鐘自動重新整理 -->
 
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 "
<table border="">
<tbody>
<tr>
<td>instance</td>
<td>result</td>
<td>Slave_IO_Running</td>
<td>Slave_SQL_Running</td>
<td>Master_Host</td>
<td>Master_Port</td>
<td>Replicate_Do_DB</td>
<td>memo</td>
</tr>
<tr>
<td>instance</td>
<td>result</td>
<td>Slave_IO_Running</td>
<td>Slave_SQL_Running</td>
<td>Master_Host</td>
<td>Master_Port</td>
<td>Replicate_Do_DB</td>
<td>Slave_IO_State</td>
<td>Last_IO_Errno</td>
<td>Last_IO_Error</td>
<td>Last_SQL_Errno</td>
<td>Last_SQL_Error</td>
</tr>
<tr>
<td>{$host}</td>
<td>{$res['result']}</td>
<td>{$res['Slave_IO_Running']}</td>
<td>{$res['Slave_SQL_Running']}</td>
<td>{$res['Master_Host']}</td>
<td>{$res['Master_Port']}</td>
<td>{$res['Replicate_Do_DB']}</td>
<td>{$memo}</td>
</tr>
<tr>
<td>{$host}</td>
<td>{$res['result']}</td>
<td>{$res['Slave_IO_Running']}</td>
<td>{$res['Slave_SQL_Running']}</td>
<td>{$res['Master_Host']}</td>
<td>{$res['Master_Port']}</td>
<td>{$res['Replicate_Do_DB']}</td>
<td>{$res['Slave_IO_State']}</td>
<td>{$res['Last_IO_Errno']}</td>
<td>{$res['Last_IO_Error']}</td>
<td>{$res['Last_SQL_Errno']}</td>
<td>{$res['Last_SQL_Error']}</td>
</tr>
</tbody>
</table>
n";
 echo <<
 
END;
}else{
 die("no mysql instances defined.");
}

check_status_api.php:
複製代碼 代碼如下:
/*
 * 檢查mysql伺服器的同步狀態
 */
function check_mysql_replication_status($host, $username, $password)
{
 //預設狀態未知
 $r = array(
  "result" => -1
  );
 try{
  $dbh = @mysql_connect($host, $username, $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_IO_Running"] == "Yes") && ($r["Slave_SQL_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:
複製代碼 代碼如下:
//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程式設計有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.