How does php check the synchronization status of mysql in linux?
This article describes how to check the synchronization status of mysql using php in linux. It is a practical technique for detecting the synchronization status of mysql using php in Linux and has some reference value, for more information, see
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 <
<! -- Automatically refresh within 30 minutes -->
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"
<Table border = "">
<Tbody>
<Tr>
<Td> instance </td>
<Td> result </td>
<Td> Slave_IO_Running </td>
<Td> Slave_ SQL _Running </td>
<Td> Master_Host </td>
<Td> Master_Port </td>
<Td> Replicate_Do_DB </td>
<Td> memo </td>
</Tr>
<Tr>
<Td> instance </td>
<Td> result </td>
<Td> Slave_IO_Running </td>
<Td> Slave_ SQL _Running </td>
<Td> Master_Host </td>
<Td> Master_Port </td>
<Td> Replicate_Do_DB </td>
<Td> Slave_IO_State </td>
<Td> Last_IO_Errno </td>
<Td> Last_IO_Error </td>
<Td> Last_ SQL _Errno </td>
<Td> Last_ SQL _Error </td>
</Tr>
<Tr>
<Td> {$ host} </td>
<Td >{$ res ['result']} </td>
<Td >{$ res ['slave _ IO_Running ']} </td>
<Td >{$ res ['slave _ SQL _Running ']} </td>
<Td >{$ res ['master _ host']} </td>
<Td >{$ res ['master _ port']} </td>
<Td >{$ res ['replicate _ Do_DB ']} </td>
<Td >{$ memo} </td>
</Tr>
<Tr>
<Td> {$ host} </td>
<Td >{$ res ['result']} </td>
<Td >{$ res ['slave _ IO_Running ']} </td>
<Td >{$ res ['slave _ SQL _Running ']} </td>
<Td >{$ res ['master _ host']} </td>
<Td >{$ res ['master _ port']} </td>
<Td >{$ res ['replicate _ Do_DB ']} </td>
<Td >{$ res ['slave _ IO_State ']} </td>
<Td >{$ res ['last _ io_errno']} </td>
<Td >{$ res ['last _ IO_Error ']} </td>
<Td >{$ res ['last _ SQL _errno']} </td>
<Td >{$ res ['last _ SQL _Error ']} </td>
</Tr>
</Tbody>
</Table>
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.