Skip to main content

How to Fix "Error Establishing a Database Connection" in WordPress

Your site is down and every page shows one line of text. The fix is rarely as scary as the blank white page suggests. This guide walks through each cause in the order most likely to get you back online fast.

"Error establishing a database connection" means exactly what it says: WordPress tried to reach its database and could not. Every WordPress page — posts, products, settings, even the login screen — is built from data stored in a MySQL database. When WordPress cannot open that connection, it has nothing to render, so it shows this single line instead of your site.

The good news: this is one of the most fixable WordPress errors. There are only a handful of root causes, and you can rule each one out in a few minutes. Work through the sections below in order. The first two fixes resolve the large majority of cases.

What the Error Actually Means

WordPress connects to your database using four pieces of information stored in a file called wp-config.php: the database name, the database username, the password, and the database host. When you load a page, WordPress reads those four values, then asks the database server for your content.

The connection can fail for one of these reasons:

  • One of the four credentials in wp-config.php is wrong
  • The database server is down, overloaded, or out of memory
  • The database itself is corrupted
  • The credentials are correct but the database user lost its permissions

Your job is to figure out which one. Start with the fastest checks.

Start Here: The Two-Minute Checks

Is the admin area broken too?

Visit yourdomain.com/wp-admin. The result tells you a lot:

  • If wp-admin shows a message like "One or more database tables are unavailable. The database may need to be repaired," your credentials work but a table is corrupted. Jump to the repair section.
  • If wp-admin shows the same "Error establishing a database connection" message, the connection is failing entirely. Keep reading.

Are other sites on the same server also down?

If you host more than one site on the same plan, check another one. When every site is down at once, the database server itself is the problem, not your individual configuration. That points you straight to the database server section.

Did anything change just before it broke?

Think back to the last few minutes before the error appeared. A plugin update, a theme change, a host migration, or an edit to wp-config.php are the usual triggers. The most recent change is your prime suspect.

Fix 1: Check Your Database Credentials

This is the single most common cause, especially right after migrating a site to a new host. The credentials that worked on the old server almost never match the new one.

Open wp-config.php in the root of your WordPress installation using your host's file manager or an SFTP client. Look for these four lines:

define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_user' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );

Now compare each value against what your hosting control panel reports. In most panels you will find the database name and user under a "Databases" or "MySQL Databases" section. Confirm three things:

  • The database name and user exist and are spelled exactly as in the file, including any host-specific prefix.
  • The password is current. If you are unsure, reset the database user's password in the control panel, then paste the new value into wp-config.php.
  • The host value is correct. localhost works on most shared hosts, but some providers use a specific hostname such as 127.0.0.1, a port, or a dedicated database server address. Your host's documentation lists the right value.

Save the file after any change, then reload your site. If the error clears, you are done.

Fix 2: Confirm the Database User Has Permissions

Sometimes the credentials are correct but the database user is no longer attached to the database — a common side effect of restoring a backup or recreating a user. The connection details look right, yet the user has no rights to read the tables.

In your control panel, open the MySQL Databases tool and check that your database user is assigned to your database with "All Privileges." If it is missing, add the user back to the database and grant full privileges. Reload your site afterward.

If you want to confirm the credentials independently of WordPress, create a small test file named dbtest.php in your site root:

<?php
$link = mysqli_connect( 'localhost', 'your_database_user', 'your_database_password' );
if ( ! $link ) {
    die( 'Connection failed: ' . mysqli_connect_error() );
}
echo 'Connected successfully';

Load yourdomain.com/dbtest.php in your browser. "Connected successfully" confirms the credentials and the server are fine, which means the problem is the database name or a corrupted table. A connection error points back to the credentials or the server. Delete dbtest.php as soon as you finish testing — never leave it on a live site.

Fix 3: Repair a Corrupted Database

A database table can become corrupted after a crash, a server power loss, or an interrupted update. WordPress ships with a built-in repair tool for exactly this situation.

Add this line to wp-config.php, just above the line that reads /* That's all, stop editing! */:

define( 'WP_ALLOW_REPAIR', true );

Now visit yourdomain.com/wp-admin/maint/repair.php. You do not need to be logged in — that is why this setting is dangerous to leave on. Choose "Repair Database" for a faster pass, or "Repair and Optimize Database" for a thorough one. When the tool finishes, remove the line you added from wp-config.php so the page is no longer publicly reachable.

Reload your site. If a corrupted table was the cause, the repair usually clears it.

Fix 4: When the Database Server Is Down or Overloaded

If your credentials are correct but the connection still fails — and especially if several sites went down together — the database server is the culprit. This happens when a traffic spike exhausts the server's memory, or when too many connections pile up faster than the server can clear them.

On shared hosting you have limited control here. Take these steps:

  • Contact your host. Ask whether the MySQL service is running and whether your account hit a resource or connection limit. This is the fastest path on shared plans.
  • Check your hosting dashboard for resource graphs. A CPU or memory line pinned at 100 percent explains a database that keeps dropping connections.
  • Look for a runaway plugin. A poorly built plugin can flood the database with queries until the server gives out. If the error started after you activated something, that plugin is worth suspecting.

When this error appears repeatedly under normal traffic, the server is simply too small or too crowded for your site. That is a hosting problem, not a WordPress one — and no amount of config editing will fix an undersized server.

How to Prevent It From Happening Again

Once your site is back, a few habits keep this error from returning:

  • Keep a recent backup. A current database backup turns corruption from a crisis into a five-minute restore.
  • Stage your changes. Test plugin and theme updates on a staging copy before they touch the live database.
  • Watch your resource usage. If your dashboard regularly shows memory near its ceiling, you have outgrown your plan.
  • Choose hosting built for WordPress. Quality managed hosting isolates your database, sets sane connection limits, and gives the server enough memory to absorb traffic spikes.

When You Want Someone to Just Fix It

If you have worked through every fix and the error persists — or you would rather not touch wp-config.php at all — we can take it from here. Our team has resolved this exact error on hundreds of sites, and our managed WordPress hosting is tuned to prevent it: isolated databases, generous memory limits, and automatic daily backups so a corrupted table is never more than one restore away.

Book a free call and we will get your site back online.

Related reading: Why Is My WordPress Site Slow?How to Migrate a WordPress SiteVortex Media WordPress Hosting