WordPress Error Establishing a Database Connection: Fix

Illustration of a database with a broken connection, representing the WordPress database connection error

The “Error establishing a database connection” message means WordPress could not talk to its MySQL database. Check the four login values in wp-config.php first. If they are right, repair the database tables, then ask your host whether the database server is running.

Most guides tell you to “check your credentials” and stop there. That works only when the credentials are the problem. This guide sorts the error into four causes, gives you a quick test for each, and tells you which fix to try first so you do not waste an hour on the wrong one.

What the Database Connection Error Means

WordPress stores your posts, pages, comments and settings in a database. Every page load asks that database for content. If the request fails, WordPress shows this error instead of your site.

The error has four common causes:

  • The login details in wp-config.php are wrong or out of date.
  • The database host address is wrong.
  • The database tables are damaged.
  • The database server is down or overloaded.

The error usually appears on your whole site. If only the dashboard breaks, jump to the section on the admin-only error below.

Quick Diagnosis: Which Cause Is Yours?

Match what you see to the most likely cause. This saves time because each cause has a different fix.

What you see Most likely cause Start with
Error appeared right after a host or database password change Wrong DB_PASSWORD or DB_USER Fix 1
Error appeared right after a site migration Wrong DB_HOST or DB_NAME Fix 1 and Fix 2
Front page works, but /wp-admin shows a repair message Damaged database tables Fix 3
Error comes and goes, and your host shows high load Database server overloaded or down Fix 4
Nothing changed, error is sudden and constant Server issue or damaged core files Fix 4, then Fix 5

Before You Start: Back Up First

Back up your files and your database before you change anything. Most hosting panels have a backup tool or a one-click export in phpMyAdmin. If the site is already down, a backup of the current state still protects you from a mistake while fixing it.

Fix 1: Check the Login Details in wp-config.php

The wp-config.php file holds the details WordPress uses to log in to the database. Open it through your host’s file manager or an FTP client. It sits in the root folder of your site, next to the wp-admin and wp-content folders.

Look for these four lines:

Constant What it holds
DB_NAME The name of the database
DB_USER The database username
DB_PASSWORD The password for that user
DB_HOST The server where the database lives

Now open the MySQL Databases section of your hosting panel. Compare each value against what the panel shows. The database name and username are often long and include a prefix from your hosting account, so a missing prefix is a common mistake.

Worked example: You change your database user’s password in the hosting panel for security reasons. WordPress still has the old password inside wp-config.php. The next page load fails and the error appears. The fix is to paste the new password into the DB_PASSWORD line and save the file. The site returns as soon as the file is saved.

If you are not sure the password is right, set a new one in the hosting panel and paste that same value into wp-config.php. This is faster than guessing.

Fix 2: Check the Database Host (DB_HOST)

On many hosts the value is localhost. Some hosts use a different address for the database server, so the correct value depends on your host. You can find it in your hosting dashboard, in the database section, or in your host’s documentation.

A wrong DB_HOST is most common after you move a site to a new host or copy it from a local test setup. If you migrated recently, confirm the host value with your new provider before you try anything else.

Fix 3: Repair the Database Tables

WordPress has a built-in repair tool. It is turned off by default. To use it, add one line to wp-config.php, above the comment that says to stop editing:

define( 'WP_ALLOW_REPAIR', true );

Then follow these steps:

  1. Save wp-config.php.
  2. Visit yoursite.com/wp-admin/maint/repair.php in your browser.
  3. Click “Repair Database” or “Repair and Optimize Database”.
  4. Wait for the results, then reload your site.
  5. Remove the line from wp-config.php straight away.

Do not skip step 5. The repair page does not ask you to log in. While the line stays in your file, anyone who knows the address can open the page. Remove the line as soon as the repair finishes.

When Only the Dashboard Breaks

Sometimes your public pages load but /wp-admin shows a message that the database tables are unavailable or need repair. This points to damaged tables rather than a connection problem. Fix 3 is the right place to start in that case.

Fix 4: Check Whether the Database Server Is Running

If your login details are correct and the repair does not help, the database server itself may be down or overloaded. You cannot fix this from inside WordPress. Your host controls the database server on shared plans.

Open a support chat or ticket and give them this information. It speeds up the answer:

  • Your domain name and the time the error began.
  • The text of the error exactly as it appears.
  • A note that wp-config.php values match your hosting panel.
  • A request to check whether MySQL or MariaDB is running and whether your account hit a resource limit.

If this error comes back often, your hosting plan may be too small for your traffic. Our guide on choosing hosting for a fast WordPress site explains what to look for.

Fix 5: Replace Damaged WordPress Core Files

Core files can become damaged after a failed update or a hacked site. To replace them, download a fresh copy of WordPress from wordpress.org. Upload the new wp-admin and wp-includes folders to your site and overwrite the old ones.

Do not overwrite the wp-content folder or the wp-config.php file. The wp-content folder holds your themes, plugins and uploads. The wp-config.php file holds your database details. Replacing either one can erase your work.

How to Prevent This Error

You cannot stop every database error, but you can lower the risk.

  • Update wp-config.php whenever you change a database password. This is the most common avoidable cause.
  • Keep automatic backups. A recent backup turns a serious failure into a short restore.
  • Remove unused plugins. Heavy plugins can put extra load on the database. See our comparison of WordPress caching plugins, because caching reduces how often WordPress queries the database.
  • Use uptime monitoring. A monitor emails you when your site goes down, so you find out before your visitors do.

While your site is back up, run a link check too. Our guide to fixing broken links in WordPress shows how to find pages that no longer work.

The Short Version

  1. Back up your site.
  2. Check DB_NAME, DB_USER, DB_PASSWORD and DB_HOST in wp-config.php against your hosting panel.
  3. Run the built-in repair tool, then remove the repair line.
  4. Ask your host if the database server is running.
  5. Replace wp-admin and wp-includes with fresh copies as a last step.

How This Guide Was Put Together

This guide is based on the steps documented by WordPress and by major hosting providers. We compared several of those guides and kept the steps they agree on: the four wp-config.php constants, the WP_ALLOW_REPAIR repair tool and its no-login warning, and the advice to back up first. We did not invent any statistics. For the official reference on wp-config.php settings, see the WordPress wp-config.php documentation.

Frequently Asked Questions

What causes “Error establishing a database connection” in WordPress?

The most common cause is wrong database login details in wp-config.php, often after a password change or a site move. Other causes are a wrong database host, damaged tables, or a database server that is down or overloaded. A quick check of the four wp-config.php values rules out the most common cause first.

Will I lose my content when I see this error?

Usually no. The error means WordPress cannot reach the database, not that the database is empty. Your posts and pages are still stored there. Take a backup before you change anything, then work through the fixes in order, starting with wp-config.php.

Is it safe to leave WP_ALLOW_REPAIR in wp-config.php?

No. The repair page does not ask for a login, so anyone with the address can open it while the line is active. Add the line only when you run the repair. Remove it as soon as the repair finishes, and then reload your site to confirm everything works.

Why does only my WordPress dashboard show a database error?

If your public pages load but /wp-admin fails, the database tables are often damaged or need repair. WordPress reads different tables for the dashboard than for some front-end requests. Run the built-in repair tool described in Fix 3 and then test the dashboard again.

When should I contact my hosting provider?

Contact your host when your wp-config.php values match the hosting panel and the repair tool does not help. They can check whether MySQL or MariaDB is running and whether your account reached a resource limit. Give them your domain, the exact error text and the time it started.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *