Fix "Error Establishing a Database Connection"
Your whole site is replaced by one line of text. It looks catastrophic and usually is not — your content is almost certainly intact. WordPress simply cannot reach the database that holds it.
There are four realistic causes. This narrows them down quickly.
First: is it just the front end?
Try yourdomain.com/wp-admin.
- A different error mentioning "database may need to be repaired" → jump to cause 3.
- The same error → continue below.
Cause 1 — Wrong credentials in wp-config.php
The most common cause, and the most common trigger is a site migration or a password change.
Open wp-config.php in cPanel's File Manager (it is in public_html) and find:
define('DB_NAME', 'user_wpdb');
define('DB_USER', 'user_wpuser');
define('DB_PASSWORD', 'the-password');
define('DB_HOST', 'localhost');Now check each against reality in cPanel under Databases → MySQL Databases:
- Does a database with that exact name exist? On cPanel it is prefixed with your account username —
user_wpdb, notwpdb. Migrations from another host frequently carry over the *old* prefix. - Does that user exist, and is it assigned to that database with ALL PRIVILEGES? A user that exists but is not attached to the database fails exactly like a wrong password.
DB_HOSTshould belocalhoston our servers.
If you are unsure of the password, create a new one under MySQL Databases, assign the user to the database with all privileges, and update wp-config.php to match.
Cause 2 — The database server is unreachable
If credentials are definitely right, test whether MySQL is answering at all. Create a file called dbtest.php in public_html:
<?php
$c = mysqli_connect('localhost', 'DB_USER', 'DB_PASSWORD');
if (!$c) { die('FAILED: ' . mysqli_connect_error()); }
echo 'Connected fine';Fill in your real values and load yourdomain.com/dbtest.php.
- "Connected fine" → credentials are correct and the problem is elsewhere; go to cause 3.
- "Access denied" → wrong username or password, or the user is not assigned to the database. Back to cause 1.
- "Can't connect" / "Too many connections" → the server side. This is often a symptom of hitting resource limits — see our guide to fixing the 508 resource limit error, since the same overload causes both.
Delete `dbtest.php` afterwards. It contains your database password in plain text.
Cause 3 — A corrupted table
If /wp-admin said the database needs repair, that is the answer.
Add this line to wp-config.php just above /* That's all, stop editing! */:
define('WP_ALLOW_REPAIR', true);Then visit yourdomain.com/wp-admin/maint/repair.php and choose Repair Database.
Remove that line as soon as you are done. While it is present, the repair page is accessible to anyone without logging in.
If it does not clear, use phpMyAdmin → select the database → tick all tables → Repair table from the dropdown.
Cause 4 — Resource limits
On shared hosting, exceeding your MySQL connection or CPU allowance produces this error intermittently — the site works, then does not, then does again.
The tell is that it comes and goes rather than staying broken. Check Resource Usage in cPanel. If you are hitting ceilings, the database error is a symptom and the 508 guide covers the fix.
While you are fixing it
Put up a holding message rather than leaving the raw error visible. Create .maintenance in public_html containing:
<?php $upgrading = time(); ?>WordPress then shows a maintenance page instead. Delete the file when you are done.
Preventing it
- Take a backup before any migration. This error is overwhelmingly a migration artefact.
- Keep JetBackup restore points. Restoring a database from last night beats debugging at 2am.
- Cache aggressively so you are nowhere near your connection limits under normal load.
Message us on WhatsApp with your domain if none of the four apply. We can read the MySQL error log directly, which names the actual failure rather than the generic message WordPress shows.
Related articles
How to Install Free SSL on WordPress
Issue a free Let's Encrypt certificate with AutoSSL, move WordPress to HTTPS, and clear the mixed-content warnings that follow.
How to Back Up Your WordPress Site
What a complete WordPress backup actually contains, how to take one from cPanel or a plugin, and how to restore it when you need to.
Speed Up WordPress with LiteSpeed Cache
Configure LiteSpeed Cache on NexzaHost hosting — the settings that matter, the ones that break sites, and how to verify caching is actually working.
Was this article helpful?
Still stuck?
Our support team is available 24/7.