mysql主從資料庫狀態檢測功能使用php如何??

來源:互聯網
上載者:User
這篇文章主要介紹了PHP實現的mysql主從資料庫狀態檢測功能,結合具體執行個體形式分析了php檢測多個mysql主從資料庫連接狀態的相關實現技巧,需要的朋友可以參考下

本文執行個體講述了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';

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.