Home > mysql, scripts > MySQL health check

MySQL health check

There are different ways to monitor a remote service but when it comes to databases, it could get a bit tricky.  It’s inaccurate to do a port check to verify db server availability because at times it’s not enough for TCP port 3306 to respond. What matters is whether you’re able to run a mysql query to retrieve data.

This script can be placed anywhere on your web docroot which you can hit via your website (i.e http://www.yourwebsite.com/db.php).  If the mysql server is up, you’d get a response with db up time, number of threads in use, number of slow queries, average query response in seconds and etc.

You can easily change this script to include other queries.  Copy and paste the following script on your web server.  It can be accessed via http or run via cmd line php.

<?php
$dbhost = 'mysqlhost';
$dbuser = 'user';
$dbpass = 'password';
$please = mysql_connect($dbhost,$dbuser, $dbpass);
if (!$please) {
die('Could not connect: ' . mysql_error());
}
echo "Connected successfully to $dbhost<br>";
print "MySQL status = ".mysql_stat()."\n";
mysql_close($con);
?>
Categories: mysql, scripts Tags:
  1. No comments yet.
  1. No trackbacks yet.