There are few WordPress errors more alarming than this one:
Your entire website goes blank. No homepage, no admin dashboard, nothing. Just that single, ominous line — and no hint of where to start fixing it.
The WordPress database is the heart of your website. Every post, page, comment, setting, and user account lives in the database. When WordPress can't connect to it, the entire site stops functioning.
The good news: this error is almost always caused by one of a small handful of problems — and every single one of them is fixable without losing your content. In this complete guide, we'll walk through every cause and every solution, step by step.
WordPress is a dynamic CMS — it doesn't serve static HTML files. Every time someone visits your site, WordPress connects to a MySQL (or MariaDB) database, queries it for the right content, and assembles the page on the fly.
If that connection fails at any point, WordPress can't render anything and displays the "Error Establishing a Database Connection" message instead.
This error can manifest in several ways:
Each variation points to a slightly different root cause — which we'll cover below.
wp-config.php file doesn't match what your hosting provider has configured, WordPress can't connect.localhost as the database host, but some hosts use a specific IP address or hostname. Using the wrong value will prevent connection.wp-config.php itself has a syntax error or was accidentally edited, WordPress may fail to read the credentials from it.wp-config.php credentials changed by an attacker.Start here — this resolves the majority of database connection errors. Your wp-config.php file stores the four credentials WordPress uses to connect to the database:
If any one of these is wrong, the connection fails immediately.
Step 1: Open wp-config.php
Connect to your server via FTP or your hosting file manager and navigate to your WordPress root directory. Open the file named wp-config.php.
Look for these four lines:
define( 'DB_NAME', 'your_database_name' );
define( 'DB_USER', 'your_database_username' );
define( 'DB_PASSWORD', 'your_database_password' );
define( 'DB_HOST', 'localhost' );
Step 2: Verify the credentials against your hosting account
Now log in to your hosting control panel (cPanel, Plesk, etc.) and find the database section — usually called MySQL Databases or Database Manager.
Cross-check every value:
localhost on shared hosting. Some hosts use 127.0.0.1 or a custom hostname like mysql.yourdomain.comStep 3: Update any incorrect values
If any credential doesn't match, correct it in wp-config.php, save the file, and reload your site. This single step fixes the database connection error in a large percentage of cases.
MyDatabase and mydatabase are different. Double-check capitalization carefully.Even if your credentials are correct, the database or user may have been accidentally deleted, or the user may not have been properly assigned to the database.
In cPanel:
If the user is not linked to the database:
Reload your site and test.
Sometimes the issue isn't your credentials — it's that the MySQL server itself has crashed or stopped running.
Signs that this is the cause:
What to do:
On a VPS or dedicated server where you have root access, you can check and restart MySQL yourself:
# Check MySQL status
sudo systemctl status mysql
# Restart MySQL if it's stopped
sudo systemctl restart mysql
# For MariaDB
sudo systemctl restart mariadb
If your credentials are definitely correct but the connection still fails, the database host value may be wrong. localhost works for most shared hosting, but not always.
Try replacing localhost in your wp-config.php with each of the following, saving and testing after each:
define( 'DB_HOST', '127.0.0.1' );
define( 'DB_HOST', 'localhost:3306' );
If you're on a specific managed host, check their documentation for the correct database host value. For example:
yoursitename.mysql.wpengine.comIf your credentials are correct but WordPress still can't operate properly — or if your wp-admin shows a message about unavailable database tables — your database may be corrupted.
WordPress has a built-in database repair tool that you can activate with a single line in wp-config.php.
Step 1: Enable the repair tool
Open wp-config.php and add this line just before the /* That's all, stop editing! */ comment:
define( 'WP_ALLOW_REPAIR', true );
Save the file.
Step 2: Run the repair tool
Open your browser and navigate to:
https://yourdomain.com/wp-admin/maint/repair.php
Replace yourdomain.com with your actual domain.
You'll see a page with two options:
Click Repair Database first. WordPress will scan all tables and attempt to repair any that are corrupted.
Step 3: Disable the repair tool
This is important. Once the repair is complete, remove the line you added from wp-config.php immediately:
define( 'WP_ALLOW_REPAIR', true ); // Remove this line
The repair page is publicly accessible — anyone can visit it. Leaving it enabled is a security risk.
Step 4: Repair via phpMyAdmin if the built-in tool doesn't work
phpMyAdmin will attempt to repair each table and report the results.
A single typo in wp-config.php — a missing quote, an extra space, or an accidental character — can prevent WordPress from reading the file entirely, causing a database connection error.
Common mistakes to look for:
Wrong (missing closing quote):
define( 'DB_NAME', 'my_database );
Correct:
define( 'DB_NAME', 'my_database' );
Wrong (extra space in password):
define( 'DB_PASSWORD', 'mypassword ' );
Correct:
define( 'DB_PASSWORD', 'mypassword' );
Also make sure there is no whitespace or blank lines before the opening <?php tag at the very top of the file. Any character before <?php can break PHP's ability to parse the file.
If you're not sure whether the file is correct, you can download a fresh copy of wp-config-sample.php from the WordPress repository on GitHub, use it as a template, and carefully re-enter your credentials.
On shared hosting plans, your account is allocated a maximum number of simultaneous database connections — typically between 10 and 25. During a traffic spike or if a plugin is opening connections inefficiently, you can hit this limit and start seeing connection errors for some visitors.
Signs this is the cause:
How to fix it:
If you've tried every fix above and nothing has worked — or if your database is so corrupted that the repair tool can't help — restoring from a clean backup is the most reliable path to recovery.
How to restore a WordPress backup:
If you use UpdraftPlus:
If you use your hosting provider's backup system:
After restoring, update your wp-config.php to confirm all credentials still match, then reload your site.
If your database connection error appeared suddenly and without any changes on your part, a malware infection may have modified your wp-config.php file, changed your database credentials, or corrupted your database tables.
Steps to check for malware:
wp-config.php for unauthorized changeswp-config.php manually via FTP and look for any credentials that don't match what's in your hosting panel — a clear sign of tamperingIf malware is confirmed, follow a full WordPress malware removal process, change all passwords (WordPress admin, database, FTP, hosting panel), and consider engaging a professional security service.
If you've worked through every fix in this guide and the database connection error persists, it's time to contact your hosting provider. Provide them with:
Good hosting providers can check:
.htaccess file to prevent unauthorized access to your database credentials file:<files wp-config.php>
order allow,deny
deny from all
</files>
| Cause | Fix |
|---|---|
| Wrong database credentials | Check and correct DB_NAME, DB_USER, DB_PASSWORD, DB_HOST in wp-config.php |
| User not assigned to database | Add user to database with All Privileges in cPanel |
| MySQL server is down | Contact host or restart MySQL on VPS |
| Wrong database host value | Try 127.0.0.1 or check host-specific documentation |
| Corrupted database tables | Use WordPress built-in repair tool or phpMyAdmin |
| Syntax error in wp-config.php | Check file for typos, missing quotes, or whitespace before <?php |
| Connection limit exceeded | Enable caching, optimize queries, or upgrade hosting plan |
| Malware attack | Run security scan, check credentials, change all passwords |
| Nothing works | Restore from a clean backup |
wp-config.php with the new one.
wp-config.php there without FTP. For everything else, a file manager or FTP client is the most reliable approach.
The WordPress database connection error is scary — but it's rarely a sign of catastrophic data loss. In most cases, it comes down to a credential mismatch in wp-config.php or a temporarily unavailable MySQL server, both of which can be fixed in just a few minutes.
Work through the fixes in this guide from top to bottom. Start with your credentials, check whether your database server is running, repair any corrupted tables, and use debug tools to identify anything unusual. In the vast majority of cases, you'll have your site back online well before reaching the bottom of this list.
If the problem keeps coming back, or if you suspect a security breach, it's time to bring in a professional. Our WordPress development team has resolved database connection issues across hundreds of websites — from simple credential fixes to full malware recoveries.
Contact us today and we'll get your database — and your website — back online fast.