How to fix "error establishing a database connection" in WordPress

What this WordPress error means and how to fix it, from checking database credentials and server status to repairing tables and handling traffic spikes.

By the UK Technology Hub teamPublished 4 minute read

The short answer

This error means WordPress can't talk to its database. The most common causes are wrong database details in wp-config.php, a database server that's down or overloaded, a corrupted database, or hosting resource limits being hit. Check the credentials first, then confirm the database server is running with your host, repair tables if needed and look at traffic or resource spikes.

WordPress stores almost everything in a database: posts, pages, settings, users. When it can't connect, it shows "Error establishing a database connection" and nothing else. Here's how to find and fix the cause.

Step 1: Check whether it affects the whole site

Visit the front end and the admin area (/wp-admin). If the admin shows a different message, such as tables needing repair, that points towards database corruption. If both show the same error, it's likely a connection or server problem.

Step 2: Check the database credentials

Open wp-config.php using your host's file manager or SFTP and check:

  • DB_NAME: the database name.
  • DB_USER: the database username.
  • DB_PASSWORD: the password.
  • DB_HOST: usually "localhost," but some hosts use a different hostname or port.

Compare them with the details in your hosting control panel. Credentials often change after a migration, a password reset or a hosting move. Fix any mismatch, save and reload.

Step 3: Check the database server

If the credentials are correct, the database server may be down or overloaded:

  • Check your host's status page.
  • Look for database service alerts in your control panel.
  • Contact hosting support and ask whether the database server is running.

If other sites on the same hosting account show the same error, it's almost certainly a server issue.

Step 4: Test the connection

Your host's database tools, such as phpMyAdmin, can confirm whether you can log in with the same credentials. If you can't, the user may lack permissions or the password may be wrong.

Step 5: Repair the database

Database tables can become corrupted. WordPress includes a repair tool:

  1. Add the WP_ALLOW_REPAIR setting, set to true, to wp-config.php.
  2. Visit /wp-admin/maint/repair.php.
  3. Run "Repair Database."
  4. Remove the setting afterwards, because the repair page doesn't require a login while it's enabled.

Many hosts also offer repair tools in their control panels.

Step 6: Check resource limits and traffic

On shared hosting, a traffic spike, bot attack or heavy plugin can exhaust database connections. Signs include errors that come and go. Solutions:

  • Add caching so fewer requests reach the database.
  • Block abusive bots with a firewall or CDN.
  • Optimise slow plugins and queries.
  • Upgrade hosting if you've outgrown your plan.

Step 7: Restore from backup

If the database is badly damaged and repair doesn't work, restore the most recent clean backup. Check for lost content created since that backup.

If you see a blank page instead, read our guide to the WordPress white screen of death. Server-level errors may show as a 500 error; see our guide to fixing 500 internal server errors.

Prevention

  • Keep regular, tested database backups.
  • Use caching to reduce database load.
  • Choose hosting with adequate resources for your traffic.
  • Monitor uptime and get alerts.
  • Clean up unused plugins, old revisions and spam regularly.
  • Document database credentials securely, especially after migrations.

Checking from the command line

If you have SSH access, you can test the database connection directly with your host's database client, using the same credentials as wp-config.php. A successful login confirms the credentials and server are working, pointing towards a WordPress-specific issue such as corrupted tables. A failed login shows the exact error message, such as access denied or unknown host, which narrows down the cause.

After a migration

This error commonly appears after moving a site to new hosting. Check that the database was imported completely, the database user exists on the new server with the right permissions, DB_HOST matches the new host's requirements and the table prefix in wp-config.php matches the imported tables.

When to call your host

If credentials are correct and the database server doesn't respond, contact your host with the exact time of the errors and any messages from logs. Server-level issues, such as crashed database services or resource limits, can only be fixed by the host.

Keeping it from happening again

Set up uptime monitoring that checks a real page, so you're alerted within minutes rather than hearing from customers.

How we help

Our maintenance and support team diagnoses and fixes database errors and sets up monitoring and backups. Our CMS development team optimises WordPress databases, and our web development team can move sites to more reliable hosting.

Frequently asked questions

Did I lose my WordPress content?

Usually not. The error means WordPress can't connect, not that data is gone. Check credentials and the server first.

Why does the error come and go?

Intermittent errors often mean the database server is overloaded by traffic, bots or heavy plugins.

Is it safe to use the WordPress repair tool?

Yes, but remove the WP_ALLOW_REPAIR setting afterwards because the repair page is accessible without logging in while it's enabled.

This guide is general information, not legal, tax or financial advice, and details such as prices, features and rules can change. Product and company names mentioned are trademarks of their respective owners and are used only to identify those products; UK Technology Hub is not affiliated with or endorsed by them.

Related guides

How to fix the WordPress white screen of death

A calm, step-by-step guide to diagnosing and fixing a blank white WordPress page, from recovery mode and debug logs to plugins, themes and memory limits.

All bug fixes & troubleshooting guides