Tag Archives: internal server error

Simple MySQL Heartbeat monitoring

I had an interesting request today: create a script that would return a page header 200 or 500 for a heartbeat monitoring on a MySQL server for a site. The reason behind it was that the server was returning way too many MySQL connection errors during a small period of time for it to considered a simple app error.

This is what I have came for to help with the issue:


<!--?php
$dsn = "mysql:dbname={$dbName};host={$host}";
$user = 'mysql_username';
$pass = 'mysql_user_password';
try {
$dbh = new PDO($dsn, $user, $pass);
$dbh = null;
header("HTTP/1.1 200 OK");
} catch (Exception $e) {
header("HTTP/1.1 500 Internal Server Error");
}

With this simple code we can monitor the page status and if it returns 500 set an alert to tell us that MySQL server is down.

There are, off course, better ways to do this, but if you don’t have too many resources or is using a shared server, this might be a good solution meanwhile.