You open your site and instead of your homepage you get one line of white-screen dread: "Error establishing a database connection." No design, no menu, nothing. It's one of the most common WordPress errors we see on support tickets, and the good news is that it's almost always fixable in under 15 minutes once you know where to look.

This guide walks through the real causes in order of likelihood, and exactly what to check on a cPanel-hosted WordPress site.

What This Error Actually Means

WordPress stores every post, page, setting, and user account in a MySQL (or MariaDB) database. When WordPress loads, it reads wp-config.php to get the database name, username, password, and host, then tries to connect. If that connection fails for any reason, WordPress can't fetch anything — so it shows this generic error instead of your site.

The message is generic on purpose. It doesn't tell you why the connection failed, which is why so many people panic and assume their site is hacked or their content is gone. In almost every case, your content is completely untouched — it's the connection that's broken, not the data.

Symptom: What You'll See

  • The front end shows only the database connection error, often with no styling at all
  • The WordPress admin (/wp-admin) shows the same error, or a similar "One or more database tables are unavailable" message
  • Sometimes only the site is affected while phpMyAdmin loads fine, or vice versa

Cause 1: Wrong Credentials in wp-config.php

This is the single most common cause, especially right after a migration, a password reset, or a hosting change. Open wp-config.php via cPanel File Manager or SFTP and check these four lines near the top:

define( 'DB_NAME', 'yourdb_name' );
define( 'DB_USER', 'yourdb_user' );
define( 'DB_PASSWORD', 'yourdb_password' );
define( 'DB_HOST', 'localhost' );

In cPanel, log in and go to MySQL® Databases. Confirm three things:

  1. The database name in DB_NAME matches exactly (cPanel usually prefixes it, like cpaneluser_wp)
  2. The user in DB_USER is actually assigned to that database under "Add User to Database"
  3. The password in DB_PASSWORD is current — if you're not sure, reset it in cPanel and update wp-config.php to match
If you just reset the database password in cPanel, remember it does not automatically update wp-config.php. You have to edit the file yourself.

Cause 2: Wrong or Changed DB_HOST

On shared hosting, DB_HOST is almost always localhost. But if you migrated from a different host, restored an old backup, or you're on certain managed setups, the database might live on a different internal hostname or a non-standard port (like localhost:3307). Check with your hosting provider or in cPanel's database section if localhost doesn't work.

Cause 3: Corrupted or Crashed Database Tables

If credentials check out but the error persists, the database itself might have crashed — often after an abrupt server restart, a failed plugin update, or running out of disk space mid-write. WordPress has a built-in repair tool for this.

Add this line to wp-config.php, above the "That's all, stop editing!" comment:

define( 'WP_ALLOW_REPAIR', true );

Then visit https://yourdomain.com/wp-admin/maint/repair.php and click Repair Database (or Repair and Optimize). Once it finishes, remove that line from wp-config.php — leaving it in place lets anyone with the URL run repairs on your site.

You can also repair tables directly from cPanel's phpMyAdmin: select your database, tick all tables, and choose Repair table from the dropdown at the bottom.

Cause 4: MySQL Service Is Down or Overloaded

On a VPS or dedicated server, the MySQL/MariaDB service itself might have stopped or hit a connection limit. SSH in and check:

systemctl status mysql
# or
systemctl status mariadb

If it's not running, start it and check the error log for why it stopped:

systemctl start mysql
tail -50 /var/log/mysql/error.log

A common culprit is the server running out of RAM, which kills MySQL via the OOM killer. If that's happening repeatedly, it's usually a sign you need more memory, or a bloated database that needs cleanup (old revisions, spam comments, transients).

Cause 5: Too Many Connections

On busy sites, WordPress may fail to connect because MySQL has hit its max_connections limit — often from a traffic spike, a bad plugin opening connections and not closing them, or a bot crawl. Check current connections:

mysqladmin -u root -p status

If this is a recurring issue rather than a one-off, it's worth reviewing which plugin is chatty with the database, or moving to a VPS plan with more headroom.

Quick Diagnostic Checklist

CheckWhereFix
DB name/user/password matchwp-config.php vs cPanel MySQL DatabasesUpdate wp-config.php or reset password
DB_HOST valuewp-config.phpUsually localhost; confirm with host
Tables crashed/corruptedphpMyAdmin or repair.phpRun repair tool
MySQL service statusSSH: systemctl status mysqlRestart service, check error log
Connection limit hitmysqladmin statusOptimize plugins, upgrade resources

Prevention: Keep This From Happening Again

  • Take backups before every password reset or migration so you always have a known-good wp-config.php and database to fall back on
  • Use a database management plugin or scheduled task to clean up post revisions, transients, and spam comments — a leaner database is less likely to crash
  • Monitor disk space on your server; MySQL can corrupt tables if it runs out of space mid-write
  • Avoid editing wp-config.php by hand on a live site when possible — stage changes and double-check syntax, since a stray character can break the whole file
  • Set file permissions correctly: wp-config.php should typically be 640 or 600, not world-writable

If you've gone through all of this and the site still won't connect, it's worth opening a support ticket with your host — there could be a server-level issue (like a MySQL crash affecting multiple accounts) that needs to be fixed at the infrastructure level rather than inside your WordPress install.

Frequently Asked Questions

Will I lose my posts and pages if I see this error?

No, in almost all cases your content is completely intact. This error means WordPress can't reach the database, not that the database has been deleted. Once the connection issue is fixed, your site loads normally.

Why does this error show up right after I migrate my site?

Migrations usually involve moving to a new database with a different name, username, or password, but wp-config.php still has the old credentials. Always update wp-config.php to match the new database details after any migration.

Can a plugin or theme cause this error?

Indirectly, yes. A poorly coded plugin can open too many database connections or run heavy queries that crash tables under load. If the error started right after installing or updating a plugin, that's a good place to look after ruling out credentials.

Is it safe to run the WordPress repair tool on a live site?

Yes, repairing tables is generally safe and doesn't delete data — it fixes structural issues in the tables. Still, it's good practice to have a recent backup before running any repair, just in case.

What if wp-config.php looks correct but the error still happens?

Confirm the MySQL/MariaDB service is actually running on the server, and that you haven't hit a connection limit. On shared hosting, this usually means contacting your host, since you won't have direct access to the database service.