本文实例讲述了PHP实现的MysqL主从数据库状态检测功能。分享给大家供大家参考,具体如下:
实例:
PHP;">
array(
'hostname' => '127.0.0.1','port' => 3306,'database' => 'test','username' => 'root','password' => '111111','charset' => 'utf8',),'db2'=>array(
'hostname' => '127.0.0.2',);
$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 )无法连接 !
"; $content .= MysqL_error() . "
"; 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 )挂掉了!
"; } MysqL_free_result($result); MysqL_close($slave_link); } //若报错信息不为空,发送报错邮件 if(!empty($content)) { $title = '主从数据库状态检测报错 '; $content = date("Y-m-d H:i:s",time()) . "
" . $content; $sendurl = "http://localhost/api.ftrend.com/test.PHP?title=".$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("
","\n",$content); $message .= $content; error_log($message,3,"error.log"); } }
"; $content .= MysqL_error() . "
"; 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 )挂掉了!
"; } MysqL_free_result($result); MysqL_close($slave_link); } //若报错信息不为空,发送报错邮件 if(!empty($content)) { $title = '主从数据库状态检测报错 '; $content = date("Y-m-d H:i:s",time()) . "
" . $content; $sendurl = "http://localhost/api.ftrend.com/test.PHP?title=".$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("
","\n",$content); $message .= $content; error_log($message,3,"error.log"); } }
PHP;">
PHP
$title = $_GET['title'];
$content = $_GET['content'];
$content = str_replace("
",$content); error_log($title."\n",'error.log'); error_log($content."\n",'error.log'); echo 'ok';
",$content); error_log($title."\n",'error.log'); error_log($content."\n",'error.log'); echo 'ok';
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《PHP常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。