Email VPS
Troubleshooting and repair

Error establishing a database connection, how to find the cause

The database connection fails

When WordPress shows nothing but the sentence Error establishing a database connection, the site files are intact and the content is in the database. The connection between the two just is not happening. That is good news because nothing has been lost, and bad news in the sense that the site is completely unavailable until the connection is back.

In short

  • The content is safe in the database even though all you see is an error.
  • The usual causes are wrong credentials, an overloaded database server and a full disk.
  • Do not reinstall WordPress. It does not fix the connection and it can destroy the content.

1. Check whether the admin gives a different error

Open /wp-admin. If it shows the same error, the connection is missing entirely. If the admin says the database needs repairing, this is a damaged table rather than a connection problem, so go straight to step 4.

2. Check the credentials in wp-config.php

The wp-config.php file in the site root holds the four lines that create the connection. They are the database name, the user, the password and the host. The single most common cause is that somebody changed the database password in the hosting panel and did not update it here. Compare the values with the panel character by character. Our guide to managing the database shows where to find them.

The host is usually localhost. If someone replaced it with an IP address or a remote host name during a migration, the connection breaks even when everything else is right.

3. Rule out overload

A database server can refuse new connections when too many are already open. You notice this as a site that works intermittently and fails at busy hours. The cause is often a heavy plugin or a traffic spike. If the symptom comes and goes by the hour, it is load rather than credentials.

4. Repair a damaged table

An unexpected restart can leave a table half written. WordPress has a repair mode for this. Add the line define('WP_ALLOW_REPAIR', true); to wp-config.php and open /wp-admin/maint/repair.php. Remove the line right after the repair, because that page works without logging in.

5. Check the disk space

A full disk brings the database down without any other warning, because it cannot write its own temporary files. Look at the space left in your hosting panel. The most common consumer is a backup plugin that has been storing its archives on the same disk.

6. If the error started after a migration

During a migration it is easy to forget either the database import or the user privileges in the new place. Check that the database exists, that it has tables and that the user has rights to that exact database.

What not to do

Do not reinstall WordPress. A fresh install creates an empty database and can write over the old one, and then the content really is gone. Take a backup before you try anything, see our guide on backing up a site.

If you would rather not do it yourself

We work out which one it is, a broken connection or a damaged database, and get the site back. We also check why the connection dropped, because otherwise the same cause returns on the next spike.

See how we repair a broken site »