Moydul.
AboutBlogHire Me
Moydul.

Freelance WordPress Professional & Support Engineer with 12+ years of experience building secure, fast, and reliable websites.

Quick Links

  • → About
  • → Blog
  • → Terms of Service

Services

  • → WordPress Development
  • → Malware Removal
  • → Migration Services
  • → Maintenance
  • → Server Administration
  • → WordPress Support
  • → WordPress Troubleshooting

Contact

  • contact@moydul.com
  • +880 1841-181888
Hire on Upwork →

© 2026 Moydul Haque. Made with in Bangladesh.

HomeBlogGuide

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

By Moydul Haque November 26, 2026 7 min read

Few WordPress errors are as alarming as Error establishing a database connection. Your whole site — front end and admin — is replaced by that one plain message, because WordPress literally cannot load anything without its database. The good news: in most cases your content is still sitting safely in the database. The connection has failed, not the data. This guide works through the causes in the order I check them when a client brings me a site with this error.

What this error actually means

WordPress is PHP code plus a MySQL (or MariaDB) database. The code holds almost nothing; your posts, pages, settings, users and WooCommerce orders all live in the database. When a page is requested, WordPress uses the credentials stored in wp-config.php to connect. If any part of that handshake fails — wrong credentials, a database server that is down, or a corrupted table — you get this error instead of your website.

Step 1: Check whether it's just the front end

Try opening yoursite.com/wp-admin. If you see a different message there — something like 'One or more database tables are unavailable. The database may need to be repaired.' — WordPress is telling you the connection works but a table is damaged. Skip to Step 4. If wp-admin shows the same connection error, the problem is earlier in the chain: credentials or the server itself.

Step 2: Verify the database credentials in wp-config.php

This is the most common cause, especially after a site migration, a hosting change or a password reset. Open wp-config.php in your hosting file manager or via FTP and 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' );

Now open your hosting control panel (cPanel, hPanel or similar) and go to the MySQL Databases section. Compare the database name and username character by character, and confirm that user is actually assigned to that database with ALL PRIVILEGES. If the password was changed at any point, reset it in the panel and paste the new one into wp-config.php. Also check DB_HOST: most hosts use localhost, but some use a separate database server address — your host's MySQL page will show the correct value.

Step 3: Test whether the database server is responding

In the same hosting panel, open phpMyAdmin and try to log in / open your database. If phpMyAdmin also fails or times out, the database server itself is down or overloaded — that is a hosting problem, not a WordPress problem. Check whether other sites on the same account load, look at your host's status page, and open a support ticket. On cheap shared hosting, this error also appears when your site briefly exceeds its resource limits (a traffic spike or a runaway plugin can do it) — in that case the error often clears on its own within minutes, but it will keep coming back until the underlying load is dealt with.

Step 4: Repair the database

If credentials are correct and the server responds, a corrupted table is the likely culprit — wp_options and wp_posts are the usual suspects after an unclean server shutdown. WordPress has a built-in repair tool. Add this line to wp-config.php, just above the 'That's all, stop editing!' comment:

define( 'WP_ALLOW_REPAIR', true );

Then visit yoursite.com/wp-admin/maint/repair.php and click Repair Database (or Repair and Optimize). When it finishes, remove the line from wp-config.php immediately — while it is present, anyone can open that repair page without logging in.

Take a backup before repairing if you possibly can. Most hosts offer one-click backups in the control panel, and a database export from phpMyAdmin takes a minute. Repairs are routine, but there is no reason to work without a safety net.

Step 5: Check the less obvious causes

  • Database size or user quota exceeded — some hosts cap database size; a bloated wp_options table (autotransients and old transients) can push you over. Your host's MySQL page shows the size.
  • Wrong table prefix — after a manual migration, the $table_prefix in wp-config.php must match the prefix of the imported tables, or WordPress behaves as if the database is empty/broken.
  • Recently changed hosting or PHP — if the error started right after a server move, suspect DB_HOST first, then credentials.
  • Hacked site — occasionally the database user is deleted or wp-config.php is tampered with. If you find changes you didn't make, treat it as a security incident, not just a connection problem.

When to stop and get help

If you have verified the credentials, confirmed the server is up, and a repair did not fix it — or if phpMyAdmin shows tables marked as crashed that repair cannot fix — further guessing risks real data loss. This is exactly the kind of fault I diagnose on my WordPress troubleshooting service: I check the server logs, the database state and wp-config together, and you get a fixed site plus an explanation of what failed. And if this error keeps recurring, a maintenance plan with uptime monitoring means I usually know before you do.

Frequently Asked Questions

Does 'Error establishing a database connection' mean I've lost my data?

Almost never. The error means WordPress cannot connect to the database — the posts, pages and settings stored in it are normally untouched. Data loss only becomes a risk if the database itself is severely corrupted or the host has a disk failure, which is why a backup before repair work is smart.

How long does it take to fix a database connection error?

Credential problems take minutes once you have hosting access. A table repair typically takes under an hour. If the database server itself is down, the timeline belongs to your hosting provider.

Why does the database connection error keep coming back?

Recurring errors usually mean resource limits on shared hosting (traffic spikes or a heavy plugin), a slowly corrupting table, or an overloaded wp_options table. The fix is to find and remove the underlying load, not to keep repairing the symptom.

Rather not fight this error yourself?

Send me the error message and what changed before it appeared. Most WordPress faults are diagnosed the same day — you approve the fixed price before any repair starts.

Get WordPress Troubleshooting Help
Monthly Care Plan — Now Available

WordPress Site Maintenance & Support for $75/m

Stop worrying about updates, hacks and downtime. I keep your WordPress site fast, secure and backed up — so you can focus on your business.

  • Weekly core, theme & plugin updates
  • Daily backups & uptime monitoring
  • Malware scans & security hardening
  • Priority fixes for errors & small tasks
$75/month

No long-term contract · Cancel anytime

Get Started TodayAsk a Question

More WordPress Guides

WordPress Critical Error: What 'There Has Been a Critical Error on This Website' Means — and How to Fix It

The critical error message hides a specific PHP fault behind a vague sentence. Here's how to use recovery mode and debug logs to find the exact plugin, theme or PHP issue causing it.

Read the guide

WordPress Site Broken After an Update? Do This First

The update finished and now something's wrong — layout shattered, a feature dead, or a full error screen. The order you do things in decides whether it's a 10-minute fix or a lost day.

Read the guide