Tutorials

Error Establishing a Database Connection in WordPress: 7 Fixes That Work (2026)

By Daniel Reyes•October 2, 2026•4 min read

Disclosure: some links on this page are affiliate links. We may earn a commission at no extra cost to you. How we test and rate hosts.

Our top pick for most WordPress sites
Bluehost: free domain, free SSL, 24/7 support, 30-day money-back guarantee.
Check Bluehost Deal →

The error: "Error establishing a database connection" means WordPress can't talk to MySQL — the page is blank, the message is stark, and every visitor sees it. The fix in ~70% of cases: wrong credentials in wp-config.php. The order that works: check credentials → test MySQL → repair database → check server load. Here are all 7 fixes, ordered by how often each solves it.

Fix 1: Verify wp-config.php Credentials (70% of Cases)

The four lines that matter, usually right after install or a migration gone wrong:

define( 'DB_NAME', 'your_db_name' ); define( 'DB_USER', 'your_db_user' ); define( 'DB_PASSWORD', 'your_db_password' ); define( 'DB_HOST', 'localhost' );

Cross-check these against your hosting panel's database values — not what you think they are. Common mistakes: DB_HOST isn't always localhost (some hosts use a server address like mysql.host.com), passwords get rotated during migrations, and the DB_USER needs privileges on DB_NAME. Fix any mismatch, save, reload.

Fix 2: Test Whether MySQL Is Actually Running

Credentials can be perfect while MySQL is dead. Quick tests:

  • Create a test file testdb.php with <?php $link = mysqli_connect('localhost','user','pass'); if (!$link) die(mysqli_error()); echo 'OK'; ?> — load it in browser
  • Or check other sites on the same host — if they also show the error, it's the server, not your config
  • Or check your host's status page / control panel for MySQL service alerts

If MySQL is down and you can't restart it (shared hosting), contact support — this is a host problem, not a WordPress problem. If it's intermittent under traffic, you don't have a config problem, you have a capacity problem — upgrade your hosting.

Fix 3: Repair the Database (Corruption Signature)

If wp-admin works but frontend shows the error — or you see "one or more database tables are unavailable" — tables are corrupted. WordPress has a built-in repair:

  1. Add to wp-config.php: define( 'WP_ALLOW_REPAIR', true );
  2. Visit yoursite.com/wp-admin/maint/repair.php
  3. Click "Repair Database" (or "Repair and Optimize")
  4. Remove the WP_ALLOW_REPAIR line after — it bypasses login checks

This fixes corrupted wp_options, wp_posts, and other table damage without touching data.

Fix 4: Check the Database User's Privileges

Credentials can be right while the user lacks permission on that specific database. In cPanel → MySQL Databases (or your host's equivalent), verify the DB_USER is assigned to DB_NAME with ALL PRIVILEGES. A migration that copies the database but forgets the user mapping produces exactly this error.

Fix 5: Rule Out Server Overload / Resource Limits

If the error is intermittent — fine at 3am, broken during traffic spikes — it's not credentials. Shared hosting MySQL can hit connection limits under load. Signs: error appears and disappears, correlates with traffic, other sites on the host slow too. The fix isn't config — it's moving off oversold shared hosting. A managed WordPress host with proper MySQL headroom eliminates this class of error entirely.

Fix 6: Check wp-config.php Syntax (The Silent Killer)

A missing semicolon, an unclosed quote, or a stray character in wp-config.php kills PHP parsing entirely — and can surface as a database error because the file never fully loads. Look for: every line ending in ;, quotes properly closed, no characters before <?php or after ?>. If you recently edited the file, restore from backup or re-download a clean copy.

Fix 7: Restore From Backup (Last Resort)

If the database is unrecoverable, restore from your host's backup (most offer one-click restore) or your backup plugin. If you've been running without backups, this is the expensive lesson — get a backup plugin before you need it, not after.

Why Cheap Hosting Causes This More Than Code

WordPress.org's own docs note the error is often "hosting-related, not WordPress-related." Oversold shared servers hit MySQL connection limits; hosts that throttle under load produce this intermittently; migrations that don't sync credentials cause it persistently. If you're getting this error repeatedly on a healthy install, the hosting is the problem. A host with isolated resources and proper MySQL headroom — Bluehost's WP Cloud tiers run WordPress on dedicated infrastructure — makes this class of error rare rather than routine.

Tired of Debugging Database Errors?

Bluehost's managed WordPress runs on WP Cloud — isolated containers, proper MySQL headroom, no shared-server overload errors. From $3.99/mo with migration included.

Get Bluehost Managed WP →

Related troubleshooting: WordPress white screen of death, fix "critical error" message, 500 internal server error, WordPress maintenance guide.

Find Your Perfect Hosting Plan

What are you building?

Tell us about your project so we can match the perfect hosting plan.

Question 1 of 425% complete
Interactive Tool

Hosting Cost Calculator

See exactly how much you'll spend on hosting over time. Compare Bluehost vs popular alternatives and discover your potential savings.

Used to calculate transaction fees (Shopify charges 2% on sales)

Shopify Basic (3yr)
$1,044
Bluehost (3yr)
$444
You Save
$600
58% less than Shopify Basic

Frequently Asked Questions

What causes 'Error establishing a database connection'?

WordPress can't reach the MySQL database — usually wrong credentials in wp-config.php (70% of cases), a crashed MySQL service, a corrupted database, or server overload on cheap shared hosting. It's almost never WordPress core itself.

How do I fix the database connection error fast?

Check wp-config.php first: verify DB_NAME, DB_USER, DB_PASSWORD, DB_HOST against your hosting panel's values. If credentials are right, test if MySQL is running, then try repairing the database with wp-config.php's WP_ALLOW_REPAIR flag. That sequence fixes ~90% of cases.

Is it dangerous to repair the database?

The built-in repair (define WP_ALLOW_REPAIR + visit /wp-admin/maint/repair.php) is safe — it fixes corrupted tables without deleting data. Still, take a backup first if you can access one, and remove the WP_ALLOW_REPAIR line after.

Why does the error appear on frontend but wp-admin works?

That's a specific signature: frontend dies but /wp-admin loads = the database connection works partially, often a corrupted table or a plugin querying badly. Repair the database; wp-admin working means credentials are fine.

Can server overload cause this error?

Yes — shared hosting throttling under traffic spikes can kill the MySQL connection temporarily. If it appears intermittently under load, the fix isn't credentials — it's hosting capacity. That's a signal to upgrade.

How do I prevent this from happening again?

Keep wp-config.php credentials synced after any host migration, monitor uptime to catch overload patterns, use a caching plugin to reduce database load, and pick a host with enough MySQL headroom — cheap oversold shared hosting causes this more than any code issue.

Ready to launch?

Get Bluehost from $3.99/month with a free domain, free SSL, and 30-day money-back guarantee.

Start for $3.99 →

Related Articles