2

I have a drupal 7 site. Recently I changed my mysql password but forgot to update the settings.php file. My website showed Cannot connect to mysql server for 3 days before I noticed and changed the settings.php file. My question is

Is there a way to display a custom HTML page (Like a maintenance page) when there is a mysql connection error ? Is there a module for that purpose ?

3 Answers 3

1

Having a look in drupal_bootstrap() for D7 it looks like hook_boot comes after the database "phase" so you'd need to put something in the cache phase or the configuration phase if you want to trigger a redirect before Drupal tries to bootstrap the database.

Easiest way would probably be to attempt to open a connection to the database yourself near the bottom of settings.php with the connection string defined earlier in settings.php and then if you detect a fail adapt this approach for "fast 404 pages" - http://2bits.com/drupal-planet/reducing-server-resource-utilization-busy-sites-implementing-fast-404s-drupal.html

You could print some html straight to the screen and exit().

1

As per David Meister's answer I've posted the code below for those who are new to Drupal. Copy and paste it at the bottom of the settings.php file. I know this is not a very elegant code but it gets the job done. If you have a better code please post your answer below.

$mysqli = new mysqli($databases['default']['default']['host'],$databases['default']['default']['username'],$databases['default']['default']['password'],$databases['default']['default']['database']);

    if (mysqli_connect_error()) {
     echo "Your HTML here";
     exit();
    }
0

I have added this code in settings.php and working fine.

try {
  require_once DRUPAL_ROOT . '/includes/database/database.inc';
  Database::getConnection();
}
  catch (Exception $e) {
  header("Location: $base_url/YOUR_HTML_PAGE.html");
  die;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.