How to check the synchronization status of mysql in linux using php? linuxmysql_PHP tutorial

Source: Internet
Author: User
How does php check the synchronization status of mysql in linux? linuxmysql. How php detects the synchronization status of mysql in linux. This article describes how php detects the synchronization status of mysql in linux. Share it with you for your reference. The specific analysis is as follows: how does php check the mysql synchronization status in linux? linuxmysql

This article describes how to check the synchronization status of mysql in php in linux. Share it with you for your reference. The specific analysis is as follows:

Here we use two instances to introduce the implementation of mysql synchronization status detection. The code is as follows:

The code is as follows:

#! /Bin/sh

# Check MySQL_Slave Status
# Crontab time
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 $5 }''
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 $2 }''
SQL _ENV = 'echo $ MYSQL_SLAVE_STATUS | grep SQL | awk '{print $2 }''
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


The php instance code is as follows:
Check_rep.php:

The code is as follows:

If (emptyempty ($ _ REQUEST ["key"]) die (':) missing key ');
If ($ _ REQUEST ["key"]! = 'Upeng') die (': error key ');

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

Define ("USERNAME", "USERNAME ");
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 = "unknown state ";
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 = "synchronization not set for database ";
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 the mysql server.
*/
Function check_mysql_replication_status ($ host, $ username, $ password)
{
// The default status is 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 '@ 'monitoring 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 access them through URLs.

Access Method: http: // ip/check_repl.php? Key = xupeng

I hope this article will help you with php programming.

Examples in this article describes how php detects the synchronization status of mysql in linux. Share it with you for your reference. The specific analysis is as follows :...

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.