利用PHP怎么对mysql主从数据库状态进行检测?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
实例:
<?php /** * 检测多个主从数据库是否挂掉 * 建立从数据库$slave_db的二维数组,内容包含每个从服务器的配置数据 */ header("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=".$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"); } }
<?php $title = $_GET['title']; $content = $_GET['content']; $content = str_replace("<br/>", "\n", $content); error_log($title."\n",3,'error.log'); error_log($content."\n",3,'error.log'); echo 'ok';
看完上述内容,你们掌握利用PHP怎么对mysql主从数据库状态进行检测的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。