Maintenance

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

What "Error establishing a database connection" means in WordPress, its usual causes, checking wp-config.php safely, repairing tables and when to ask your host.

"Error Establishing a Database Connection" in WordPress: How to Fix It
On this page
  1. What the error means
  2. The usual causes
  3. Step 1: Rule out the hosting server
  4. Step 2: Check the database details in wp-config.php
  5. Step 3: Repair corrupted tables
  6. Step 4: Restore the database from a backup
  7. After a migration: the most common case
  8. How to stop it happening again
Key takeaways
  • The error means WordPress cannot reach its database, usually through wrong wp-config.php details or a struggling server.
  • Rule out the host first, then match the database name, user, password and host against your hosting panel.
  • Use WordPress's repair tool for damaged tables, remove it straight after, and restore the database only as a last step.

Instead of your homepage, a single line of text: "Error establishing a database connection". It usually appears without warning, often right after a migration or a hosting change, and it takes the whole site offline. The good news is that your content is normally still there. WordPress just can't reach it. Here's how to work out why, step by step.

What the error means

A WordPress site has two parts: the files (WordPress itself, your theme and plugins) and a database that stores your pages, posts, settings, users and, on a store, your orders. Every time a page loads, WordPress connects to the database using four details saved in a file called wp-config.php:

  • DB_NAME: the database's name
  • DB_USER: the database username
  • DB_PASSWORD: that user's password
  • DB_HOST: the server the database lives on

If any of these is wrong, or the database server doesn't respond, WordPress stops and shows this error. A related message in the dashboard, "One or more database tables are unavailable", points to damaged tables rather than a failed connection.

The usual causes

CauseTypical clue
Wrong details in wp-config.phpRight after a migration, a host change, or someone changing the database password
Database server down or overloadedThe error comes and goes, or other sites on the same hosting are down too
Hosting limits reachedHappens during traffic spikes on busy shared hosting
Database or user removedAfter hosting changes, an expired plan or a suspended account
Corrupted tablesAfter a server crash or failed update; the dashboard mentions tables needing repair
Tampered wp-config.phpRare, but possible after a hack; unfamiliar code in the file

Step 1: Rule out the hosting server

Many of these errors are on the host's side and fixed by them. Check your host's status page or support chat, and see whether other websites on the same hosting account are also failing. If the error appears on and off, especially at busy times, the database server is likely struggling or hitting connection limits.

Look in your hosting panel for resource limit warnings or a suspension notice too. For a wider checklist of what to look at when a site won't open, see website down: what to check, step by step.

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

If the host says everything is running, the connection details are the next suspect. Using your hosting file manager or SFTP, download a copy of wp-config.php before changing anything. Then compare it with the MySQL Databases section of your hosting panel:

  1. Database name: does a database with exactly that name exist? Many panels add your account name as a prefix, and the full name must match
  2. Username: does that user exist, and is it assigned to this database with all privileges?
  3. Host: often "localhost", but some hosts use a separate database server name. Your hosting panel or the host's documentation will tell you
  4. Password: panels rarely show existing passwords, so reset the database user's password in the panel and paste the new one into wp-config.php

Watch for stray spaces and keep the quote marks around each value intact. Save the file and reload the site. Never paste the contents of wp-config.php into public forums or screenshots, because it contains working passwords.

Step 3: Repair corrupted tables

If the site connects but the dashboard says tables are unavailable, WordPress has a built-in repair tool:

  1. Take a database backup first, from your hosting panel or phpMyAdmin
  2. Add this line to wp-config.php, above the line that says to stop editing: define( 'WP_ALLOW_REPAIR', true );
  3. Visit yoursite.com/wp-admin/maint/repair.php and choose Repair Database
  4. Remove the line straight afterwards, because that repair page works without anyone logging in

Your host can also check and repair tables from their side. If repair doesn't fix things, restoring the database is the next step.

Step 4: Restore the database from a backup

If the database was deleted or is badly damaged, restore the latest good copy from your host's backups or your backup plugin; see the backup and restore guide.

On a WooCommerce store or booking site, remember that anything received after the backup was taken, such as orders, bookings and enquiries, won't be in the restored copy. Note them from your emails and payment gateway dashboard before you restore.

After a migration: the most common case

Moving to new hosting is the classic trigger. The new host gives you a new database name, user and password, and wp-config.php still has the old ones. Other migration-related issues:

  • The database import failed partway, leaving tables missing
  • DNS is still pointing some visitors to the old server, where the hosting may already be cancelled
  • If you see the WordPress installation screen instead of the error, the table prefix in wp-config.php probably doesn't match the imported tables

Keep the old hosting active until the new site is fully tested and DNS has settled.

How to stop it happening again

  • Store your database details securely, in a password manager, not in an email thread
  • Choose hosting with enough resources for your traffic; see choosing WordPress hosting in India
  • Use caching, which reduces how often pages need the database
  • Keep automatic daily backups that include the database, stored off the server
  • Set up uptime monitoring so you hear about outages before your customers do

Site down with a database error, or planning a move and want to avoid one? See WordPress maintenance and support and WordPress migration.

Need help with your website?

I'm Sameer, a freelance WordPress developer building fast, SEO-friendly websites since 2020. Tell me what you need and I'll reply with a plan and a fixed quote within 24 hours.

Found this useful? Share it:
Contact

Let's build your next website

Available for freelance projects, agency white-label work and long-term maintenance. Feel free to pass this along to your team or company.

Your details are emailed to me, then WhatsApp opens so we can chat right away.

Chat now