How to Resolve the WordPress Database Connection Error

There are few WordPress errors more alarming than this one:

Error Establishing a Database Connection

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.

What Is a WordPress Database Connection Error?

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:

  • The front end of your site shows "Error Establishing a Database Connection"
  • Your wp-admin shows "One or more database tables are unavailable. The database may need to be repaired."
  • Only your homepage loads but all other pages show the error
  • The error appears and disappears intermittently

Each variation points to a slightly different root cause — which we'll cover below.

What Causes a WordPress Database Connection Error?

  • Wrong database credentials in wp-config.php — The most common cause by far. If the database name, username, password, or host in your wp-config.php file doesn't match what your hosting provider has configured, WordPress can't connect.
  • Corrupted WordPress database — Database tables can become corrupted due to a server crash, a failed update, or a sudden loss of power during a write operation. Corrupted tables prevent WordPress from reading or writing data correctly.
  • Database server is down — Your MySQL or MariaDB server may have crashed or been temporarily shut down by your hosting provider for maintenance.
  • Incorrect database host — Most shared hosting uses localhost as the database host, but some hosts use a specific IP address or hostname. Using the wrong value will prevent connection.
  • Database user lacks permissions — Even if your credentials are correct, the database user may not have been granted the necessary privileges to access the database.
  • Exhausted database connections — On shared hosting plans, there's a limit on the number of simultaneous database connections. During a traffic spike, you can hit this ceiling and start seeing connection errors.
  • Corrupted wp-config.php file — If wp-config.php itself has a syntax error or was accidentally edited, WordPress may fail to read the credentials from it.
  • Malware or hack — A compromised site may have had its wp-config.php credentials changed by an attacker.

How to Resolve a WordPress Database Connection Error

01
Check Your Database Credentials in wp-config.php

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:

  • Database name
  • Database username
  • Database password
  • Database host

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:

PHP
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:

  • DB_NAME — Must exactly match the name of your database as shown in your hosting panel
  • DB_USER — Must exactly match the database username that has been assigned to that database
  • DB_PASSWORD — Must match the password for that database user (passwords are case-sensitive)
  • DB_HOST — Usually localhost on shared hosting. Some hosts use 127.0.0.1 or a custom hostname like mysql.yourdomain.com

Step 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.

Pro tip: Credentials are case-sensitive. MyDatabase and mydatabase are different. Double-check capitalization carefully.
02
Verify the Database Exists and User Has Access

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:

  1. Log in to cPanel and go to MySQL Databases
  2. Scroll down to the Current Databases section and confirm your database is listed
  3. Scroll further to Current Users and confirm your database user exists
  4. Check the Add User to Database section and confirm the user is linked to the database with All Privileges

If the user is not linked to the database:

  1. Select the user and database from the dropdowns in "Add User to Database"
  2. Click Add
  3. On the next screen, select All Privileges and click Make Changes

Reload your site and test.

03
Check if the Database Server Is Running

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:

  • The error appeared suddenly without any changes on your end
  • The error is intermittent (comes and goes)
  • Multiple websites on the same server are experiencing issues

What to do:

  1. Log in to your hosting control panel and check the server status panel if one is available
  2. Check if your host has posted any service announcements or outage notices
  3. Contact your hosting provider's support team and ask them to confirm whether the MySQL server is running

On a VPS or dedicated server where you have root access, you can check and restart MySQL yourself:

BASH
# Check MySQL status
sudo systemctl status mysql

# Restart MySQL if it's stopped
sudo systemctl restart mysql

# For MariaDB
sudo systemctl restart mariadb
04
Try Different Database Host Values

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:

PHP
define( 'DB_HOST', '127.0.0.1' );
PHP
define( 'DB_HOST', 'localhost:3306' );

If you're on a specific managed host, check their documentation for the correct database host value. For example:

  • WP Engine uses a custom hostname like yoursitename.mysql.wpengine.com
  • Kinsta uses a custom internal hostname
  • GoDaddy shared hosting sometimes requires a specific hostname found in cPanel
Database Repair Concept
05
Repair a Corrupted WordPress Database

If 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:

PHP
define( 'WP_ALLOW_REPAIR', true );

Save the file.

Step 2: Run the repair tool

Open your browser and navigate to:

URL
https://yourdomain.com/wp-admin/maint/repair.php

Replace yourdomain.com with your actual domain.

You'll see a page with two options:

  • Repair Database — Fixes corrupted tables
  • Repair and Optimize Database — Fixes and optimizes all tables for better performance

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:

PHP
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

  1. Log in to your hosting control panel and open phpMyAdmin
  2. Select your WordPress database from the left sidebar
  3. Click Check All to select all tables
  4. In the "With selected" dropdown at the bottom, choose Repair table
  5. Click Go

phpMyAdmin will attempt to repair each table and report the results.

06
Check wp-config.php for Syntax Errors

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):

PHP
define( 'DB_NAME', 'my_database );

Correct:

PHP
define( 'DB_NAME', 'my_database' );

Wrong (extra space in password):

PHP
define( 'DB_PASSWORD', 'mypassword ' );

Correct:

PHP
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.

07
Check Database Connection Limits on Shared Hosting

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:

  • The error is intermittent — it appears and disappears on its own
  • It happens more during busy periods
  • Your server resource usage in cPanel shows high MySQL usage

How to fix it:

  1. Install a database optimization plugin like WP-Optimize or WP-DBManager to clean up unnecessary data and post revisions that may be causing excessive queries
  2. Enable a caching plugin like W3 Total Cache or WP Super Cache to reduce the number of database queries per page load
  3. Contact your hosting provider and ask about increasing your database connection limit, or consider upgrading to a VPS plan that offers greater resources
08
Restore from a Backup

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:

  1. Go to Settings → UpdraftPlus Backups in wp-admin (you may need to access this via a temporary recovery URL)
  2. Find the most recent backup before the error appeared
  3. Click Restore and select the Database component

If you use your hosting provider's backup system:

  1. Log in to cPanel or your hosting panel
  2. Find the Backup or Backup Wizard section
  3. Restore the most recent MySQL database backup

After restoring, update your wp-config.php to confirm all credentials still match, then reload your site.

Always check when your last backup was taken. If it was days or weeks ago, weigh the data loss against the time needed to continue troubleshooting.
09
Scan for Malware

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:

  1. Install and run Wordfence Security (free version available) — it will scan all files including wp-config.php for unauthorized changes
  2. Check wp-config.php manually via FTP and look for any credentials that don't match what's in your hosting panel — a clear sign of tampering
  3. Contact your hosting provider — most managed hosts offer malware scanning and will alert you if malicious activity is detected on your account

If 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.

10
Contact Your Hosting Provider

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:

  • The exact error message you're seeing
  • When the error started
  • What changes (if any) preceded the error
  • The steps you've already tried

Good hosting providers can check:

  • Whether the MySQL server is running and accepting connections
  • Whether your database account is intact and has correct privileges
  • Server-side error logs showing what MySQL is reporting
  • Whether there's a known issue or maintenance affecting your server

Preventing the WordPress Database Connection Error

  • Take regular backups. A reliable backup is your ultimate safety net. Use UpdraftPlus, BlogVault, or your host's built-in backup system to back up your database daily. Store backups in a remote location like Google Drive or Dropbox.
  • Use a managed WordPress host. Managed hosts like Kinsta, WP Engine, and Cloudways actively monitor your database and server health, alerting you to problems before they cause downtime.
  • Protect wp-config.php. Add the following to your .htaccess file to prevent unauthorized access to your database credentials file:
HTACCESS
<files wp-config.php>
order allow,deny
deny from all
</files>
  • Keep a record of your database credentials. Store your database name, username, and password securely (in a password manager). You'll need them if you ever have to rebuild or recover your site.
  • Optimize your database regularly. Use WP-Optimize or a similar plugin to clean up post revisions, spam comments, transient data, and orphaned metadata. A leaner database is less prone to corruption and connection overload.
  • Monitor your site's uptime. Use a free uptime monitor like UptimeRobot to get instant alerts the moment your site goes down — so you can start troubleshooting before your visitors even notice.

Quick Reference: Database Connection Error Fix Summary

Cause Fix
Wrong database credentialsCheck and correct DB_NAME, DB_USER, DB_PASSWORD, DB_HOST in wp-config.php
User not assigned to databaseAdd user to database with All Privileges in cPanel
MySQL server is downContact host or restart MySQL on VPS
Wrong database host valueTry 127.0.0.1 or check host-specific documentation
Corrupted database tablesUse WordPress built-in repair tool or phpMyAdmin
Syntax error in wp-config.phpCheck file for typos, missing quotes, or whitespace before <?php
Connection limit exceededEnable caching, optimize queries, or upgrade hosting plan
Malware attackRun security scan, check credentials, change all passwords
Nothing worksRestore from a clean backup

Frequently Asked Questions

Q: Will fixing the database connection error delete my content?
A: No. The fixes in this guide — correcting credentials, repairing tables, and adjusting server settings — do not delete your database content. Your posts, pages, and settings remain intact. The only exception is restoring from a backup, which reverts the database to a previous state.
Q: How do I find my WordPress database credentials?
A: Log in to your hosting control panel (usually cPanel) and go to MySQL Databases. Your database name and username are listed there. Passwords are not shown after creation — if you've forgotten yours, reset it in cPanel and update wp-config.php with the new one.
Q: Why does the database connection error appear intermittently?
A: Intermittent errors usually point to exceeding the maximum number of simultaneous database connections on your hosting plan, or an unstable MySQL server. Enable caching to reduce database load, and contact your host if the problem persists.
Q: Is "Error Establishing a Database Connection" the same as a hacked site?
A: Not necessarily. Most of the time it's a configuration issue. However, if your credentials in wp-config.php don't match what's in cPanel, and you didn't change them, a hack is possible. Run a malware scan and change all passwords as a precaution.
Q: Can I fix this error without FTP access?
A: For credential issues, yes — if you can access your hosting control panel's file manager, you can edit wp-config.php there without FTP. For everything else, a file manager or FTP client is the most reliable approach.

Final Thoughts

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.
Views: 160