Php method for detecting MySQL synchronization status under Linux _php tutorial

Source: Internet
Author: User

Php method for detecting MySQL synchronization status under Linux


This article mainly introduces PHP under Linux detection MySQL synchronization state method, is Linux under the use of PHP to detect MySQL synchronization status of practical skills, with a certain reference value, the need for friends can refer to the next

In this paper, we describe the method of detecting MySQL synchronization State in PHP under Linux. Share to everyone for your reference. The specific analysis is as follows:

This paper introduces the implementation method of MySQL synchronization state detection by two examples. The code is as follows:

The code is as follows:

#!/bin/sh

#check Mysql_slave Status
#crontab Time 00:10
Mysql_user= "Root"
Mysql_pwd= "123456"
Mysql_slave_log= "/tmp/check_mysql_slave.log"
Email= "1351010****@139.com"

Mysql_port= ' Netstat-na|grep "LISTEN" |grep "3306" |awk-f[: ""]+ ' {print $} '
mysql_ip= ' ifconfig eth0|grep "inet addr" | awk-f[: ""]+ ' {print $4} '
mysql_slave_status=$ (/usr/local/webserver/mysql/bin/mysql-u root-psylc23hua-s/tmp/mysql.sock-e

"Show Slave Statusg" | Grep-i "Running")
Io_env= ' echo $MYSQL _slave_status | grep IO | awk ' {print $} '
Sql_env= ' echo $MYSQL _slave_status | grep SQL | awk ' {print $} '
now=$ (date-d today + '%y-%m-%d%h:%m:%s ')

If ["$MYSQL _port" = "3306"];then
echo "MySQL is running!"
Else
Mail-s "Warn!server: $MYSQL _ip MYSQL is down" "$EMAIL"
Fi

If ["$IO _env" = "yes"-a "$SQL _env" = "yes"];then
echo "Slave is running!"
Else
echo "[$NOW] Slave is not running!" >> "$MYSQL _slave_log"
Cat "$MYSQL _slave_log" | Mail-s "warn! ${mysql_ip}_replicate_error "" $EMAIL "
Fi

Exit 0


PHP instance code, the code is as follows:
check_rep.php:

The code is as follows:

if (Emptyempty ($_request["key")) Die (':) missing key ');
if ($_request["key"]! = ' Xupeng ') die (':) error key ');

Include ("mysql_instance.php");
Include ("check_status_api.php");

Define ("USERNAME", "User name");
Define ("PASSWORD", "password");
Define ("DebugMode", false);

$instances = Get_instances ();

if ($instances) {
Echo <<



END;
echo "
n ";
if (! DebugMode) {
echo "
n ";
}else{
echo "
n ";
}
foreach ($instances as $host) {
$res = Check_mysql_replication_status ($host, USERNAME, PASSWORD);
if (! DebugMode) {
Switch ($res ["result"]) {
Case-4:
$memo = "Unknown exception";
Break
Case-3:
$memo = "Query Failed";
Break
Case-2:
$memo = "Unable to connect to port";
Break
Case-1:
$memo = "State Unknown";
Break
Case 0:
$memo = "OK";
Break
Case 1:
$memo = "Synchronization Failed";
if ($res ["slave_io_running"] <> "Yes") {
$memo. = $res ["Last_io_error"]. "(" . $res

["Last_io_errno"]. ")";
}
if ($res ["slave_sql_running"] <> "Yes") {
$memo. = $res ["Last_sql_error"]. "(" . $res

["Last_sql_errno"]. ")";
}
Break
Case 2:
$memo = "Database not set synchronization";
Break
}
echo "

n ";
}else{
echo "

n ";
}
}
echo "



















































Instance Result Slave_io_running Slave_sql_running Master_host Master_port replicate_do_db Memo
Instance Result Slave_io_running Slave_sql_running Master_host Master_port replicate_do_db Slave_io_state Last_io_errno Last_io_error Last_sql_errno Last_sql_error
{$host} {$res [' result ']} {$res [' slave_io_running ']} {$res [' slave_sql_running ']} {$res [' Master_host ']} {$res [' Master_port ']} {$res [' replicate_do_db ']} {$memo}
{$host} {$res [' result ']} {$res [' slave_io_running ']} {$res [' slave_sql_running ']} {$res [' Master_host ']} {$res [' Master_port ']} {$res [' replicate_do_db ']} {$res [' slave_io_state ']} {$res [' Last_io_errno ']} {$res [' Last_io_error ']} {$res [' Last_sql_errno ']} {$res [' Last_sql_error ']}

n ";
Echo <<

END;
}else{
Die ("No MySQL instances defined.");
}


check_status_api.php:

The code is as follows:

/*
* Check the synchronization status of MySQL server
*/
function Check_mysql_replication_status ($host, $username, $password)
{
Default state Unknown
$r = Array (
"Result" =-1
);
try{
$DBH = @mysql_connect ($host, $username, $password);
if (! $dbh) {
Unable to connect
$r ["result"] =-2;
return ($R);
}
$query = "SHOW SLAVE STATUS";
$res = @mysql_query ($query, $DBH);
$err = @mysql_error ();
if ($err) {
Unable to connect
$r ["result"] =-3;
return ($R);
}
$row = Mysql_fetch_array ($res);
$r = $row;
if ($r ["slave_io_running"] = = "Yes") && ($r ["slave_sql_running"] = = "Yes")
{
$r ["result"] = 0;
}else{
if (!emptyempty ($row)) {
$r ["result"] = 1;
}else{
$r ["result"] = 2;
}
}
}catch (Exception $e) {
$r ["result"] =-4;
}
return ($R);
}


mysql_instance.php:

The code is as follows:

GRANT REPLICATION CLIENT on * * to ' username ' @ ' monitor host IP ' identified by ' password '; $mysql _instances =

Array ();
$mysql _instances[] = "Remote IP: Port";

function Get_instances ()
{
Global $mysql _instances;
return $mysql _instances;
}


Place the above three PHP files in a virtual directory and then access them via a URL.

Access method: Http://ip/check_repl.php?key=xupeng

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/971934.html www.bkjia.com true http://www.bkjia.com/PHPjc/971934.html techarticle The method of detecting MySQL synchronization State in PHP under Linux This article mainly introduces PHP under Linux detection MySQL synchronization state method, is Linux under the use of PHP to detect MySQL synchronization state of the practical skills, with ...

  • Related Article

    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.