MySQL Server DOWN – oh no!

By | February 27, 2008

In the official osCommerce Forum I got into bit of a conversation with a Store Owner, part of the conversation was all about what happens if your MySQL server goes down whilst the webserver is still up and running.

For those who don’t know, MySQL runs on it’s own little server as part of the overall web server.  So it is possible for one to be down whilst the other is functioning OK…

OK, so, in a standard osCommerce shop, what will happen is that a message will be displayed, like this:

Unable to connect to database server!

Which isn’t so great for anything – after all, it is your CUSTOMERS who will see this!  So, my idea was to make this message a little better for you and your customers.

Open up the file /includes/application_top.php and find

tep_db_connect() or die(‘Unable to connect to database server!’);

This line of code is simply saying “Connect to the Database OR kill the connection and show the “unable to…” message.  We are going to change this line of code to;

@tep_db_connect() or header(“Location: busted.html”);

Now we are saying “Connect to the Database OR redirect to a page called busted.html”.  Easy, eh?  Notice that we also put an “at” (@) sign in front of tep_db_connect – this is important as we wish to make sure we suppress any other error messages.

Save the /includes/application_top.php file and upload it to your server to replace the old one (which you might like to backup first!).

The next step is to make a new HTML page called “busted.html”.  On this page you can have anything you like – maybe an error message, maybe a “contact us by phone” message – maybe something else entirely!

However, there is one VERY IMPORTANT CAVEAT – the “busted.html” page MUST NOT connect to your database!  As your database server is down, it would be completely pointless to do so.

Of course, the page can be .php if you wish and it doesn’t have to be called “busted.html” – it can be called anything you like.  Just make sure that the name fo the file and the “Location” line of code match exactly!

You could send yourself an error message by email, or you could even send a signal to the server to restart the SQL server (this would be for very advanced users who have control of their own server).  For most of us, a simple HTML page will suffice.

Why 404 redirect won’t work in this case

If your SQL server is down, pages are still presented by the webserver, so they do not 404 (error out) and redirect.  What this means is that you could go to; and see “Unable to Connect” – this is NOT a 404 and so it won’t redirect to an error page.   Does this make sense?

7 thoughts on “MySQL Server DOWN – oh no!

  1. obewanz

    This is an excellent post… presents a nice alternative to handle things if your DB server loses connection or dies on you.

    BTW: being part of that other conversation, seeing this helps me better understand what the conversation was about, and as such am in full agreement with the solution for this aspect of that conversation.

  2. Bushmaster

    Very good idea. Hopefully the page will not be seen much but better to show them something
    other then the default message.

  3. Gary Post author

    Cheers BM. Always good to think of the customers before anyone and anything else in my opinion!

  4. Dave McMahon

    Great tip, and one I wish I’d known about just over a year ago with our old hosting provider – some days their database server seemed to be down more than it was up…

    Nice to have a neat, friendly fallback rather than the ugly system-generated messages which seem designed to alienate the user/customer.

  5. nikoloz

    This error came up accidentally,

    Is it really host problem,

    because one time I experienced that problem with other forum website

    Warning: mysql_connect() [function.mysql-connect]: Too many connections in /includes/functions/database.php on line 19
    Unable to connect to database server!

  6. Gary Post author

    Nik – yep, usually the sign of too many sites on a server.

Leave a Reply

Your email address will not be published.