Home›Insights›Error Establishing a Database Connection in WordPress: Causes and Fixes
WordPress

Error Establishing a Database Connection in WordPress: Causes and Fixes

WordPress shows 'Error establishing a database connection.' This guide covers the four real causes: wrong credentials in wp-config.php, a database server that is down or overloaded, a corrupted database, and a changed host, with the steps to check each.

I
Inspirable Editorial•7 min read

The message Error establishing a database connection means WordPress tried to reach its MySQL or MariaDB database and could not. WordPress stores every post, page, setting, and user in that database, so without a connection it cannot build any page and shows only this message. There are four common causes: the credentials in wp-config.php are wrong, the database server is down or overloaded, the database is corrupted, or the site moved and the connection details no longer match. You can tell them apart with a few quick checks, in the right order.

WordPress is telling you it cannot talk to its database. The question is whether the database is wrong, down, damaged, or simply not where WordPress is looking.
Site down or broken right now?
Talk to a USA-based WordPress support team. Call and we will start on it.
Call 844-455-2800Send us a message

Check whether the admin shows a different message

Start by checking what you see in the dashboard, because the two parts of the site can fail differently. Visit yourdomain.com/wp-admin. If you see Error establishing a database connection there too, the problem is the connection itself, which is the usual case. If instead the admin says One or more database tables are unavailable, the database is reachable but damaged, which points to cause 3 below. If the front end errors but the admin shows a prompt to repair the database, that is also cause 3. Before you change anything, take note of when it started and whether anything changed first, such as a host migration, a password reset on the hosting account, or a plugin update.

Cause 1: wrong credentials in wp-config.php

Cause 1 is wrong credentials in wp-config.php, and it is the most common after a migration or a password change. Open wp-config.php in the site's root folder and find four values: DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST. Compare each with the database details shown in your hosting control panel, where the database, its user, and the user's assigned privileges are listed. A single character difference, a database user that was deleted or never given access to that database, or a password that was changed on the host and not in wp-config.php will all produce this exact error. DB_HOST is usually localhost, but some hosts use a specific hostname or a port, so use whatever your host documents.

Cause 2: the database server is down or overloaded

Cause 2 is the database server being down or overloaded, which has nothing to do with your files. On shared hosting, a traffic spike or a neighbor's heavy usage can make MySQL refuse connections, and the error then comes and goes. Try loading the site again after a few minutes, check your host's status page, and look in the hosting panel for any resource or connection limit warnings. If other sites on the same server show the same error, it is the server. Contact your host, because only they can restart or fix the database service. A site that errors under load, especially during traffic spikes, may also be on a plan that is too small for it.

Cause 3: a corrupted database

Cause 3 is a corrupted database, often after a crash, a full disk, or an interrupted import. WordPress has a built-in repair tool. Add define( 'WP_ALLOW_REPAIR', true ); to wp-config.php, then visit yourdomain.com/wp-admin/maint/repair.php and choose Repair Database. Remove that line from wp-config.php as soon as you are done: the repair page works without being logged in while the setting is on, so leaving it enabled is a security risk. If the repair reports tables it cannot fix, restore the database from a backup, since the backup from before the corruption is the reliable way out.

Cause 4: you moved or changed hosts

Cause 4 is a move or host change. After migrating to a new host, the database name, user, password, or hostname almost always differ from the old server, and wp-config.php still holds the old values. Re-check all four values against the new host's details. Also confirm the database was actually imported into the new server, and that the table prefix in wp-config.php, the line beginning with table_prefix, matches the prefix of the tables in the imported database. A mismatched prefix makes WordPress look for tables that do not exist under that name. If the error appeared with no migration or change at all, and the credentials are correct and the server is up, check for a compromise; see our guide on the WordPress site is hacked warning signs. For other errors that take a site offline, see the 500 internal server error guide.

Get the database error fixed

If you would rather not troubleshoot the database connection error yourself, or the steps above did not clear it, Inspirable's USA-based team can take it from here. Call 844-455-2800 or contact us and we will find the cause, restore the site, and explain what happened in plain language. See our WordPress support services for how emergency fixes and monthly support work. A WordPress care plan from $49.99 a month, with encrypted backups every 5 hours and managed updates, is what prevents most of these errors in the first place.
Call 844-455-2800Send us a message

Frequently asked questions

I
Inspirable Editorial
Enterprise WordPress development since 2012