How to Fix the Error Establishing a Database Connection in WordPress

As a WordPress site owner, I know how worrying it can be to see the "Error establishing a database connection" message when trying to access your site. With over 41% of the web powered by WordPress, database issues can bring your site grinding to a halt, impacting both your business and your readers.

Not to worry – in my 15 years as a web developer, I‘ve helped hundreds of site owners get things up and running again. This comprehensive guide will walk you through all the potential causes and fixes for the database connection error, using my expertise to help get your site back online quickly.

Why Does the Database Connection Error Happen?

To understand why you‘re getting this error, you first need to know how WordPress and databases work together…

WordPress uses MySQL databases to store all your site‘s content, settings, and users. The WordPress software connects to this database each time someone visits your site, querying the database to load things like your posts and comments.

Your wp-config.php file contains the credentials needed to log in and access the database:

  • DB_NAME – The name of the database
  • DB_USER – The database username
  • DB_PASSWORD – The database user‘s password
  • DB_HOST – The database server hostname, usually ‘localhost‘

If this connection information is incorrect, or if the database server is down, WordPress won‘t be able to query the database. This leads to the "Error establishing a database connection" message.

There are a few key reasons this error can show up:

1. Incorrect Database Credentials

This is the most common culprit. Double check that the credentials in wp-config.php match the info for your database.

2. Database Server Outage

Problems with the database server itself can prevent connections. This can happen if the server is overloaded or crashes.

3. Corrupted Database

If the database files themselves get corrupted, WordPress may not be able to access the tables and data as expected.

4. Outdated Site URL

The stored ‘siteurl‘ value can become out-of-sync, leading to issues.

Let‘s go through each scenario more in-depth, with steps to troubleshoot and fix the problem…

Step-by-Step Guide to Resolving the Error

1. Verify your Database Credentials

First, login to your web hosting control panel, and confirm you have the correct database name, username, password, and host info.

In cPanel, you can find this under "MySQL Databases".

cPanel MySQL Databases

Double check that the wp-config.php constants match:

define( ‘DB_NAME‘, ‘your_database_name‘ );
define( ‘DB_USER‘, ‘your_db_user‘ ); 
define( ‘DB_PASSWORD‘, ‘your_password‘ );
define( ‘DB_HOST‘, ‘localhost‘ );

If anything is incorrect, update wp-config.php and try reloading your site.

2. Check for Database Server Issues

If the credentials are correct, the issue may be at the server level. Contact your host to see if they are experiencing MySQL outages or slowdowns.

You can also try connecting to the database manually via PHPMyAdmin to test if the server is responsive.

For managed WordPress hosts, database server problems are rare, but can happen if they are over capacity. Budget shared hosts are more prone to outages under high traffic.

3. Repair Corrupted Database Tables

In some cases, the database itself can become corrupted. WordPress may fail to connect and show errors like:

Error establishing a database connection
Database repair required
One or more database tables are unavailable

To allow WordPress to attempt repairs, add this to wp-config.php:

define( ‘WP_ALLOW_REPAIR‘, true );

Then visit /wp-admin/maint/repair.php to start the repair process.

Corrupted databases can happen for sites that have been hacked. Running regular backups gives you a clean copy you can restore from.

4. Update the ‘Site URL‘ Value

Sometimes the stored ‘siteurl‘ value in the wp_options table can become outdated or incorrect.

This can prevent WordPress from being able to connect properly during requests.

You can manually update it to the correct URL via phpMyAdmin:

UPDATE wp_options 
SET option_value = ‘http://your-correct-site-url‘
WHERE option_name = ‘siteurl‘;

5. Allocate More MySQL Resources

If your database server is underpowered or overloaded, connection issues can occur as MySQL becomes unresponsive.

Upgrading to a managed WordPress hosting plan that offers ample MySQL resources can help. You can also ask your host to allocate more RAM and CPU cores to MySQL.

6. Reboot Web Server

For self-managed VPS or dedicated servers, rebooting the machine can clear up temporary glitches causing database connectivity problems.

This restarts both the web server and database server, essentially giving them a fresh start.

7. Contact Your Web Host

If you still can‘t resolve the issue after trying all above steps, reach out to your web host‘s technical support team.

They have additional tools at their disposal to investigate and fix any server-side config issues that may be affecting the database connection.

I hope this guide gives you a good overview of the various factors that can cause the database connection error, plus actionable troubleshooting tips. Don‘t hesitate to reach out if you need any help getting your site back online.

Written by Jason Striegel

C/C++, Java, Python, Linux developer for 18 years, A-Tech enthusiast love to share some useful tech hacks.