Table of Contents
How to use php to detect mysql synchronization status under Linux
Home Backend Development PHP Tutorial PHP method to detect mysql synchronization status under Linux_PHP tutorial

PHP method to detect mysql synchronization status under Linux_PHP tutorial

Jul 13, 2016 am 10:01 AM
linux mysql php Down Synchronize exist method Detection state of

How to use php to detect mysql synchronization status under Linux

This article mainly introduces how to use php to detect mysql synchronization status under Linux. It is the method of using php to detect mysql synchronization under Linux. The practical skills of status have certain reference value. Friends in need can refer to it

The example in this article describes how PHP detects the synchronization status of MySQL under Linux. Share it with everyone for your reference. The specific analysis is as follows:

Here are two examples to introduce the implementation method of mysql synchronization status detection. 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 $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
PHP example 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", "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 = "Status unknown";
break;
case 0:
$memo = "OK";
break;
case 1:
$memo = "Sync 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 is not synchronized";
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)
{
//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 the virtual directory and access them via URL.

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

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/971934.htmlTechArticleHow to detect mysql synchronization status in php under linux This article mainly introduces how to detect mysql synchronization status in php under linux The method is a practical technique for using php to detect the synchronization status of mysql under Linux. It has...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

How to safely store JavaScript objects containing functions and regular expressions to a database and restore? How to safely store JavaScript objects containing functions and regular expressions to a database and restore? Apr 19, 2025 pm 11:09 PM

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

Docker on Linux: Containerization for Linux Systems Docker on Linux: Containerization for Linux Systems Apr 22, 2025 am 12:03 AM

Docker is important on Linux because Linux is its native platform that provides rich tools and community support. 1. Install Docker: Use sudoapt-getupdate and sudoapt-getinstalldocker-cedocker-ce-clicotainerd.io. 2. Create and manage containers: Use dockerrun commands, such as dockerrun-d--namemynginx-p80:80nginx. 3. Write Dockerfile: Optimize the image size and use multi-stage construction. 4. Optimization and debugging: Use dockerlogs and dockerex

Explain the purpose of foreign keys in MySQL. Explain the purpose of foreign keys in MySQL. Apr 25, 2025 am 12:17 AM

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

How does MySQL differ from Oracle? How does MySQL differ from Oracle? Apr 22, 2025 pm 05:57 PM

MySQL is suitable for rapid development and small and medium-sized applications, while Oracle is suitable for large enterprises and high availability needs. 1) MySQL is open source and easy to use, suitable for web applications and small and medium-sized enterprises. 2) Oracle is powerful and suitable for large enterprises and government agencies. 3) MySQL supports a variety of storage engines, and Oracle provides rich enterprise-level functions.

ZipInputStream failed to decompress Chinese file name? How to set the character set correctly? ZipInputStream failed to decompress Chinese file name? How to set the character set correctly? Apr 19, 2025 pm 04:33 PM

Discussion on ZipInputStream character set settings Many developers use ZipInputStream to decompress zip compressed packages containing Chinese file names or folder names, �...

See all articles