Skip to content

Instantly share code, notes, and snippets.

@davidrenne
Created April 24, 2012 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrenne/2479283 to your computer and use it in GitHub Desktop.
Save davidrenne/2479283 to your computer and use it in GitHub Desktop.
Slave server DOWN management
<?php
//1. Create a file that contains your constants or paths of locations of files
//2. Update your database classes before they make a connection to look for this $contants['slave_on'] path. If file doesnt exist, only make a connection to your master server. Tweak the settings of the seconds_behind_master if 120 seconds is too much for your reporting of slave data.
//3. Create a monitoring script to ping this URL to see if the connection is "OK". Upon error it will unlink the file and recreate it once the slave's IO is back up
include('yourconstants.php');
$conn = mysql_connect('server','username','password');
$singleRow = mysql_query("SHOW SLAVE STATUS");
$row = mysql_fetch_array($singleRow);
$slaveInError = ((strtoupper($row['Slave_IO_Running']) == 'NO' || intval($row['Seconds_Behind_Master']) > 120) || is_null($row['Seconds_Behind_Master']));
if ($slaveInError && file_exists($contants['slave_on']))
{
echo "ERROR";
unlink($contants['slave_on']);
mail("developers@yourwebsite.com","Slave Flag Turned off","A monitor detected that the slave went down for whatever reason. The file that controls the slave being on '".$contants['slave_on']."' has taken the slave out of rotation");
}
else
{
if ($slaveInError)
{
echo "ERROR";
}
else
{
if (!file_exists($contants['slave_on']) && intval($row['Seconds_Behind_Master']) < 5)
{
file_put_contents($contants['slave_on'],'on');
mail("developers@yourwebsite.com","Slave Flag Turned On","Previously the slave was turned off, but now replication is back on and also between zero and 5 seconds. We recreated the file '".$contants['slave_on']."'.");
}
echo "OK";
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment