本文实例讲述了PHP实现的mysql主从数据库状态检测功能。分享给大家供大家参考,具体如下:

实例:

<"Content-Type: text/html; charset=utf-8");
set_time_limit(0);
$slave_db = array(
  'db1'=>array(
    'hostname' => '127.0.0.1',
    'port' => 3306,
    'database' => 'test',
    'username' => 'root',
    'password' => '111111',
    'charset' => 'utf8',
  ),
  'db2'=>array(
    'hostname' => '127.0.0.2',
    'port' => 3306,
    'database' => 'test',
    'username' => 'root',
    'password' => '111111',
    'charset' => 'utf8',
  ),
);
$content = '';
foreach ($slave_db as $db_key) {
  $host = $db_key['hostname'];
  $port = $db_key['port'];
  $db_user = $db_key['username'];
  $db_pass = $db_key['password'];
  $slave_link = mysql_connect($host,$db_user,$db_pass);
  if(mysql_errno()) {
    $content .= "从数据库( $host )无法连接 ! <br/>";
    $content .= mysql_error() . "<br/>";
    continue;
  }
  $sql = "show slave status";
  $result = mysql_query($sql, $slave_link);
  $row = mysql_fetch_assoc($result);
  $Slave_IO_Running = $row['Slave_IO_Running'];
  $Slave_SQL_Running = $row['Slave_SQL_Running'];
  if ('Yes' == $Slave_IO_Running && 'Yes' == $Slave_SQL_Running) {
  } else {
    $content .= "从数据库( $host )挂掉了! <br/>";
  }
  mysql_free_result($result);
  mysql_close($slave_link);
}
//若报错信息不为空,发送报错邮件
if(!empty($content)) {
  $title = '主从数据库状态检测报错 ';
  $content = date("Y-m-d H:i:s",time()) . "<br/>" . $content;
  $sendurl = "http://localhost/api.ftrend.com/test.php".$title."&content=".$content;
  $result = file_get_contents($sendurl);
  if('ok' != $result) {
    $message = date("Y-m-d H:i:s",time()).'slaveStatus.php主从数据库状态检测报错,邮件发送失败!'."\n";
    $content = str_replace("<br/>", "\n", $content);
    $message .= $content;
    error_log($message,3,"error.log");
  }
}

<"<br/>", "\n", $content);
error_log($title."\n",3,'error.log');
error_log($content."\n",3,'error.log');
echo 'ok';

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php+mysql数据库操作入门教程》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP网络编程技巧总结》及《php常见数据库操作技巧汇总》

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

标签:
PHP,mysql,主从数据库,状态,检测

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com