Home / Fix guides / Broken links, errors and uptime

How to fix 'Error establishing a database connection' in WordPress

This error means WordPress cannot reach its database, so it cannot build any page. The usual causes are wrong login details in wp-config.php, a database server that is down or overloaded, or damaged database tables.

The message 'Error establishing a database connection' means WordPress tried to talk to its database and got no usable answer. Without the database it has no pages, posts or settings to show, so visitors see a plain page with that one sentence. Most cases are fixable in under an hour, and the steps below work whether or not you are a developer.

What the error means and what people see

WordPress keeps your content, settings and user accounts in a MySQL or MariaDB database. Every page view asks that database for information. If the question gets no answer, WordPress stops and prints the error.

Sometimes only the public site is affected. Sometimes /wp-admin shows the same message. If /wp-admin instead says one or more database tables are unavailable, or offers a repair, the tables are probably damaged. That case is covered in the fix steps.

Why it matters

  • Sales: visitors cannot browse, book, buy or contact you. For a shop, every hour down is lost orders.
  • Trust: many customers assume the business has closed or been hacked.
  • Search: this error usually comes with a server error code. A short outage is rarely a problem, but if search engines keep hitting errors for days, they may crawl less and temporarily drop pages.
  • Security: if nobody changed anything and the error appeared on its own, check whether the site was tampered with. Attackers sometimes damage or delete databases or change files.

How to check if your site has it

These checks are free:

  • Open the site in a private window and on your phone using mobile data. This rules out your own cache.
  • Visit yoursite.com/wp-admin. Note whether it shows the same error or a different one.
  • Log in to your hosting control panel. Check that the database still exists, and look at disk usage and resource usage, because a full disk or a hit limit can stop the database.
  • Look at your host's status page for a database outage.
  • Ask what changed recently: a host move, a password reset, a plugin or update, or an unpaid hosting bill.

This error often appears at night or on weekends, so a monitor that checks your site regularly helps. Week 29 of SiteMonday is an uptime monitoring report: see week 29. More on the general idea is in website down monitoring.

How to fix it

Take a backup of your files and database first if you can still reach them. Then work through these steps in order.

  1. Check your hosting account. Make sure it is paid, not suspended, and not over its disk or resource limits. If the host's database server is down, only the host can fix it. Contact support and tell them the exact error.
  2. Open `wp-config.php`. Use your host's File Manager or SFTP. The file is normally in the site's root folder (sometimes one level above). Find 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' );
  1. Compare them with your host panel. The database name, user and password must match exactly. If you are unsure of the password, set a new one for the database user in the panel and paste it into wp-config.php. Keep the single quotes around it.
  2. Check `DB_HOST`. On many hosts it is localhost, but some give a separate server name. Your host's help pages will tell you which one applies.
  3. Test the login in phpMyAdmin. Most hosts offer it in the control panel. If you can open the database and see tables (often starting with wp_, though the prefix can differ), the database itself is fine. If it is empty or missing, restore from a backup.
  4. Repair the tables if needed. Add this line to wp-config.php, then visit yoursite.com/wp-admin/maint/repair.php and run the repair:
define( 'WP_ALLOW_REPAIR', true );

Remove that line afterwards. The repair page can be opened without logging in while it is enabled.

  1. Rule out a plugin. Using File Manager or SFTP, rename wp-content/plugins to plugins-off for a test. If the site comes back, rename it back and then deactivate plugins one at a time to find the heavy one.
  2. Ask your host to look at the database server. On shared hosting you cannot restart it yourself. Support can check whether it is overloaded or crashed.

Common hosting platforms

Menu names change between versions and plans, so treat this as a guide to what to look for:

Host or panelWhere to look
cPanel hostsMySQL Databases and phpMyAdmin; see the cPanel guide
SiteGroundSite Tools, usually under Site, then MySQL
HostingerhPanel, usually under Databases, then Management
Managed WordPress hostsOften no direct database access, so contact support

If you moved hosts recently, the old database name, user and password almost never carry over, so update wp-config.php to the new values.

How to confirm the fix worked

  • Reload the home page and several inner pages in a private window.
  • Log in to /wp-admin and open a few posts or pages.
  • Test whatever earns you money: a contact form, a booking or a checkout.
  • Confirm WP_ALLOW_REPAIR is removed from wp-config.php.
  • Clear any caching plugin or host cache, then check again an hour later and the next day.

How to stop it coming back

  • Keep tested backups. A backup you have never restored may not work. See untested backups.
  • Change database passwords carefully. Update wp-config.php in the same minute, and record where the details are kept.
  • Watch your resources. Repeated errors at busy times often mean the plan is too small or a plugin is too heavy.
  • Use uptime alerts so you hear about it before customers do.
  • Keep WordPress, themes and plugins updated and remove ones you no longer use.
  • Copy `wp-config.php` before editing so you can undo a typo.

SiteMonday finds and explains problems, but making the changes is up to you or your developer.

Step by step for your platform

Questions

Is the data in my WordPress site lost when I see this error?

Usually not. The content normally still sits in the database, and WordPress just cannot reach it. Data is only at risk if the database was deleted or badly damaged, which is why a backup matters.

Why did this error appear when I changed nothing?

The database server may have crashed or hit a resource limit, the hosting account may have a problem, or the tables may have become damaged. Your host can usually tell you which from their server logs.

Can a plugin cause this error?

Yes, a faulty or very heavy plugin can overload the database or damage tables. Renaming the `plugins` folder for a test shows quickly whether a plugin is involved.

Is this the same as the white screen of death or a 500 error?

No, though they look similar. This error is specifically about the database. See [WordPress white screen](/fix/wordpress-white-screen/) and [HTTP 500 error](/fix/http-500-error/) for the other cases.

Should I leave WP_ALLOW_REPAIR in wp-config.php?

No. Remove it as soon as the repair is done, because the repair page can be opened by anyone while the line is present.

When should I contact my host?

Contact them if the database server is down, your account is at its limits, or the correct details in `wp-config.php` still fail. Give them the exact error message and the time it began.

Related problems