"Error establishing a database connection" is WordPress telling you it cannot communicate with your MySQL database. The site won't load at all, front end and admin both fail. Here's how to diagnose which part of the connection is broken and fix it, working from the most common cause to the least.
WordPress stores all your content, settings, and user data in a MySQL database. When the site loads, it immediately tries to connect to that database using the credentials stored in wp-config.php. If that connection fails for any reason, you get this error instead of your site.
There are four possible causes: wrong database credentials in wp-config.php, the MySQL server is down or overloaded, the database itself is corrupted, or your database user doesn't have the correct permissions assigned. Each has a different fix, so the goal is to isolate which one you're dealing with before making any changes.
One quick check before diving in: visit yoursite.com/wp-admin/. If the admin shows the same error, the problem is definitely database-side. If wp-admin loads but the front end errors, that's a different issue (likely a theme or plugin). A full database connection error affects both areas equally.
Open wp-config.php via FTP or your host's file manager. Look at these four values:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_username' );
define( 'DB_PASSWORD', 'your_password' );
define( 'DB_HOST', 'localhost' );
Log into your hosting control panel (cPanel or equivalent) and go to the MySQL Databases section. Confirm that the database name, username, and password in wp-config.php match exactly what exists in your panel. Database names and usernames are case-sensitive on many systems, mysite_db and Mysite_DB are different databases.
DB_HOST is almost always localhost on shared hosting. Some managed hosting environments use a remote database host, if yours does, the correct hostname will be documented in your control panel or in your host's knowledge base. Occasionally the value needs to include a port number, such as localhost:3306.
To confirm whether the credentials themselves are valid, create a temporary test file via FTP in your WordPress root. Name it something like dbtest.php and add the following, replacing the values with those from your wp-config.php:
<?php
$link = mysqli_connect( 'localhost', 'your_username', 'your_password', 'your_database_name' );
if ( ! $link ) {
echo 'Connection failed: ' . mysqli_connect_error();
} else {
echo 'Connected successfully';
mysqli_close( $link );
}
?>
Visit yoursite.com/dbtest.php in your browser. If it says "Connected successfully," the credentials are correct and the problem lies elsewhere. If it shows a connection error, the credentials or host value in wp-config.php are wrong. Delete this file immediately after testing, never leave database credentials exposed in a publicly accessible file.
If the credentials are correct, the MySQL server itself may be down. Check your host's status page for any reported database outages. You can also try connecting to the database directly via phpMyAdmin in your control panel, if phpMyAdmin itself gives a connection error, the MySQL service is down and you need to contact your host. This is a server-side issue you cannot resolve yourself.
On shared hosting, MySQL can also become temporarily unavailable due to server overload, particularly on overcommitted shared servers where many sites are running heavy queries simultaneously. If the error is intermittent and resolves itself after a few minutes, this is a likely cause. A host with proper resource isolation shouldn't exhibit this behaviour regularly.
If you can connect via phpMyAdmin but the site still errors, individual database tables may be corrupted. This can happen after a server crash, a failed update, or a power interruption during a write operation. WordPress has a built-in repair tool. Add this line to wp-config.php temporarily:
define( 'WP_ALLOW_REPAIR', true );
Then navigate to yoursite.com/wp-admin/maint/repair.php and click "Repair Database." The tool will scan and repair corrupted tables. Remove the WP_ALLOW_REPAIR line from wp-config.php immediately after the repair completes, leaving it in place allows anyone to run the repair tool on your database.
You can also run a repair directly in phpMyAdmin: select your WordPress database, check all tables, and use the "Repair table" option from the dropdown at the bottom. Both approaches achieve the same result.
In your hosting control panel under MySQL Databases, verify that your database user has been assigned to the correct database and has "All Privileges" granted. It's common, particularly after migrating a site, for the database user to exist and the password to be correct, but the user hasn't been linked to the database. Without that link, WordPress cannot authenticate even if the credentials themselves are right.
In cPanel, scroll down to "Add User To Database" in the MySQL Databases section, select the correct user and database, and tick "All Privileges." Save the changes and test your site immediately.
Contact your host's support with the specific error and confirmation that your wp-config.php credentials are correct and that the manual connection test failed. A database that is consistently unavailable or corrupted beyond what the repair tool can fix may require restoration from a backup. This is one of the most important reasons daily automated backups are not optional for any production site, a backup from yesterday is far preferable to rebuilding your database from scratch.
HostBible takes automatic daily backups of your files and database. If something breaks, roll back in minutes without contacting support.
View Hosting Plans