Example of mysql master-slave database status detection function implemented by PHP, mysql master-slave
This example describes the mysql Master/Slave database status detection function implemented by PHP. We will share this with you for your reference. The details are as follows:
Instance:
<? Php/*** check whether multiple Master/Slave databases have crashed * create a two-dimensional array of slave database $ slave_db, which contains configuration data for each slave server */header ("Content-Type: text/html; charset = UTF-8 "); set_time_limit (0); $ slave_db = array ('db1' => array ('hostname' => '2017. 0.0.1 ', 'Port' => 3306, 'database' => 'test', 'username' => 'root', 'Password' => '123 ', 'charset' => 'utf8',), 'db2 '=> array ('hostname' => '123. 0.0.2 ', 'Port' => 3306, 'database' => 'test', 'username' => 'root ',' Password '=> '000000', '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. = "the slave database ($ host) cannot be connected! <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. = "dropped from database ($ host! <Br/> ";} mysql_free_result ($ result); mysql_close ($ slave_link);} // if the error message is not blank, an error message is sent if (! Empty ($ content) {$ title = 'error in Master/Slave database status detection '; $ 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. an error occurred while sending the email when detecting the status of the php Master/Slave database! '. "\ 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';