HOWTO: Status Check for a Website Database


Many people know about services that allow you to ping your website to be sure it’s always online. The idea behind those is that they check every 5/10 mins sending a GET request to the given website and if they get an answer 200 they send you an email.

If you have a website that is based on a database, for example a WordPress blog, sometime you need to check the status of both. It can happen that the website is ON but the database behind is not. In this case you wouldn’t receive any email but users would not be able to access correctly to see the content. It happened to me, while trying to access to some websites, to see the following screen:


How to avoid this? I want to show how to set up the free service called StatusCake to check if your WordPress database is on. Here are the steps you need to do:

  1. Make an account on StatusCake.
  2. Create a PHP file which connects to the database and returns a 404 response if the db is offline.
  3. Set up StatusCake to ping the file.

I am going to skip the step one, because the registration is pretty straightforward.

The second step consists in uploading a file called db-test.php on your website. Such a file has the following lines of code:

<!DOCTYPE html>
  <title>DB TEST: <?php echo $_SERVER['SERVER_NAME']; ?></title>
   try {
    $PDO = new PDO('mysql:', 'username', 'password');
   } catch(PDOException $e) {
  echo "DB status : OK";

The php code tries to connect to the database with a username and password. If it fails, it returns an error 404 (the http_response_code function is available in PHP > 5.4), otherwise it shows the message ‘DB status : OK’.

Now we can set up StatusCake to ping the file we just created.

Screen Shot 2015-02-11 at 4.54.16 PM

Select ‘Create new test’ and add the URL you just created to the ‘Website URL or IP’. Add your email to ‘Contact Group(s)’ and leave all the other settings to the defaults.

I also recommend to add an extra check to see if the rest of the website is online. To do so simply make another test where you insert your website URL in the ‘Website URL or IP’ field.

If you have done everything correctly you will receive the following email whenever your database is offline:

Screen Shot 2015-02-11 at 4.51.26 PM

Finally, you can use your creativity to tune this trick (of sending a 404 error) for any event you want to monitor. I hope this helps improve your website monitoring efforts.


Leave a Reply

Your email address will not be published. Required fields are marked