Skip to content

Error Establishing a Database Connection in WordPress: How to Fix It

Every page shows "Error establishing a database connection"? Check the database server, wp-config.php details, damaged tables, and server limits, step by step.

Server room used for website hosting

Your site shows a plain white page with one sentence: “Error establishing a database connection.” Every page, including wp-admin, shows the same thing. WordPress cannot reach the database where all your posts, pages, settings, and orders are stored.

The message is frightening, but in most cases your data is safe. The problem is usually wrong connection details, a database server that is down or overloaded, or a damaged table. This guide walks through the causes from most to least common.

Before you start: do not reinstall WordPress or create a new database. That can make recovery much harder. Take a backup of your files first if you can.

Step 1: Check whether the database server is down

If the error appeared suddenly and you did not change anything, the database server may be down or overloaded. This is common on shared hosting during traffic spikes.

  • Check your host’s status page or support channel for outages.
  • Log in to your hosting control panel and open phpMyAdmin (or your host’s database tool). If you cannot open the database there either, the problem is on the server.
  • If other sites on the same hosting account show the same error, it is almost certainly the server. Contact your host.

Step 2: Verify the database details in wp-config.php

WordPress reads four values from wp-config.php. Open the file in File Manager or via SFTP and find:

define( 'DB_NAME', 'database_name' );
define( 'DB_USER', 'database_user' );
define( 'DB_PASSWORD', 'database_password' );
define( 'DB_HOST', 'localhost' );

Compare each value with the database and user listed in your hosting control panel:

  • DB_NAME must match an existing database exactly, including any prefix your host adds (for example account_wp123).
  • DB_USER must exist and be assigned to that database with all privileges.
  • DB_PASSWORD must be current. If anyone changed the database user’s password in the control panel, update it here too. When in doubt, set a new password for the database user and copy it into wp-config.php.
  • DB_HOST is usually localhost, but some hosts use a separate server name or a port such as localhost:3306. Your host’s documentation lists the correct value.

This is the most common cause after a site migration or a password change.

Step 3: Test the connection outside WordPress

To confirm whether the credentials work, create a temporary file called dbtest.php in your site’s root folder:

<?php
$link = mysqli_connect( 'localhost', 'database_user', 'database_password', 'database_name' );
echo $link ? 'Connected' : 'Failed: ' . mysqli_connect_error();

Open yoursite.com/dbtest.php in a browser. “Connected” means the details are correct and the problem is elsewhere. “Failed” with “Access denied” means the username or password is wrong. Delete the file immediately afterwards, because it contains your database password.

Step 4: Repair corrupted tables

If wp-admin shows a different message such as “One or more database tables are unavailable”, tables may be damaged. WordPress has a built-in repair tool. Add this line to wp-config.php:

define( 'WP_ALLOW_REPAIR', true );

Visit yoursite.com/wp-admin/maint/repair.php and click Repair Database. When it finishes, remove the line again. The repair page does not require a login while that setting is on.

You can also repair tables in phpMyAdmin: select the database, tick all tables, and choose Repair table.

Step 5: Check the site URL and table prefix

After migrations, two less obvious problems can cause connection-style errors:

  • Table prefix: $table_prefix in wp-config.php (usually wp_) must match the prefix of the tables in the database. If the tables are named wp7x_options, the prefix must be wp7x_.
  • Site URL: if the siteurl and home values in the wp_options table still point to the old domain, you may see redirects or errors. Check them in phpMyAdmin.

Step 6: Look at server resources

If the error comes and goes, the database server is likely running out of connections or memory under load. Common causes are traffic spikes, bots, heavy plugins, and a missing page cache. Adding page caching, blocking abusive bots, and upgrading the hosting plan are the usual long-term fixes. Your host can tell you whether you are hitting limits.

Need your site back now? Our WordPress Fix diagnoses database connection errors, repairs damaged tables, and corrects configuration after migrations. Fixed price of $49, confirmed before we start, with our Service Guarantee. Request a fix.

Frequently asked questions

Have I lost all my content?

Very unlikely. The error means WordPress cannot connect to the database, not that the database is empty. Once the connection is fixed, your content returns.

Why does the error only happen sometimes?

Intermittent errors usually mean the database server is overloaded or hitting its connection limit. Caching and better hosting fix this; changing credentials does not.

The error started right after I moved my site. Why?

The new host’s database name, user, password, or host differ from the old ones, and wp-config.php still has the old values. See our guide to moving WordPress to a new host.

Still Stuck? We Can Fix It for You

Tell us what is going on. We confirm the scope and price before any work begins.