This article mainly introduces the PHP implementation of the MySQL master-slave database state detection function, combined with specific examples of PHP detection of multiple MySQL master-slave database connection status related implementation skills, the need for friends can refer to the next
This paper describes the MySQL master-slave database state detection function implemented by PHP. Share to everyone for your reference, as follows:
Instance:
<?php/** * Detects if multiple master and slave databases are hung up * establish a two-dimensional array from the database $slave_db, containing the configuration data from each server */header ("content-type:text/html; Charset=utf-8 "); set_time_limit (0); $slave _db = Array (' DB1 ' =>array (' hostname ' = ' 127.0.0.1 ', ' port ' = 33 , ' database ' = ' test ', ' username ' = ' root ', ' password ' = ' 111111 ', ' charset ' = ' utf8 ', ' DB2 ' =>array (' hostname ' = ' 127.0.0.2 ', ' port ' = ' 3306 ', ' database ' = ' test ', ' username ' = ' r ') Oot ', ' 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. = "Cannot connect from database ($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. = "Hangs from the database ($host)! <br/> "; } mysql_free_result ($result); Mysql_close ($slave _link);} If the error message is not empty, send an error message if (!empty ($content)) {$title = ' master and slave database status detection error '; $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 master and slave database status detection error, Mail failed to send! '. ' \ 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 ';