How does the MySQL master-slave database state detection function work with PHP?

Source: Internet
Author: User
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 ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.