Fixing the Error Establishing a Database Connection in WordPress

WordPress is one of the most popular Content Management System (CMS). It runs smoothly and causes very few problems. However, if you are using WordPress for a while, the chances are that you might encounter the dreaded error, Error Establishing a Database Connection.

It is one of those WordPress errors that cause panic in your heart. And why not, your WordPress database contains everything that runs your site. It has your content, images, user information, settings, Meta descriptions, and more. They are all contained in your database.

When the plain white screen flashes the ominous phrase stating, Error establishing a database connection, it means it’s the time for quick remedial action on your part.

It’s an unnerving error.

Your website remains down. You lose traffic, and that means lost customers and ultimately lost a business opportunity.

So, what exactly is this error?

What causes this error and more importantly how to fix this error and get your website back to normal?

Here, I will go on to describe what the error message means. What causes this error and show you the ways how to troubleshoot and fix the problem.

Error Establishing a Database Connection in WordPress

What is the meaning of Error Establishing a Database Connection?

To understand this error, you first have to know how WordPress works to display your website.

PHP and MySQL are the two languages that are used to write the WordPress software. MySQL is the database that is used to store all the information that is on your WordPress site. PHP is used to store and retrieve information from that database.

PHP code accesses your MySQL database and retrieves information needed for creating the page. So, there’s a PHP query to access information on your WordPress web page. For example, there’s a query for accessing the post title, another one for getting the author name, one for retrieving the publication date, and so on.

Now the error message: “Error Establishing a Database Connection.” It means that for one reason or another the PHP code was not able to connect to your MySQL database. So the information needed to build that page was not retrieved. That’s the reason; the error message is displayed on an otherwise blank page. A connection with your WordPress database is not established, and so no information is displayed on your website.

What causes the Error – “Error Establishing a Database Connection?”

Now that you know the meaning of the error message let’s get to know the reasons behind its occurrence. In fact, there may be several reasons why your website is not connecting to the database.
They can be divided into three categories.

Firstly, your website might be having trouble connecting to your database because your login credentials for your database are wrong.

Secondly, your database might be corrupted. There are a lot of things that could cause your WordPress database to be corrupted. For example, it could result because of uploading of a bad Plugin.

Thirdly, it might happen that the server which is hosting your database is down. It could be due to an error on the part of your web hosting company, or there could be a sudden traffic spike which your server was not able to handle.

With all these possibilities causing the error, we now move on to fix the problem.

How to Fix Error Establishing a Database Connection Problem

Here, I list different methods for fixing the error, Error establishing a Database connection”. You will get all the solutions at one place.

Method 1: Does the Error occur for wp-admin?

If you are getting the error message when you are accessing wp-admin, then it is signs of database corruption.

So, you have to repair your WordPress database.

For this, you have to access your wp-config.php file. The file has your WordPress installation settings and configuration. The wp-config file is located in your root WordPress file directory. You need to log into the cPanel, select File Manager, and then access the folder where you installed WordPress.

cPanel File Manager

Once the wp-config file is open, you need to add this line of code to the bottom:

define( ‘WP_ALLOW_REPAIR’, true );

The line will enable you to repair your database by navigating to www.yourwebsite.com/wp-admin/maint/repair.php (you have to replace “yourwebsite.com” with your site URL.

You will get two options.

1. Repair Database
2. Repair and Optimize Database

You can choose any one of them. However, optimizing takes more time than just repair.

Repair WordPress Database

Once you have done the repair, don’t forget to remove the line of code that you added to wp-config. It will not allow anyone to access your database.

Don’t close the wp-config file unless the error is resolved and the website is working again.

If the error is still not resolved, continue to the next step to troubleshoot the problem.

Method 2: Check the WP-Config file

In this step, you have to look at the database settings in your wp-config file. So, go ahead and open the wp-config.php file for editing.

At the top of the file, you would find the credentials for your database.

It looks something like this:

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define( ‘DB_NAME’, ‘database_name_here’ );

/** MySQL database username */
define( ‘DB_USER’, ‘username_here’ );
/** MySQL database password */
define( ‘DB_PASSWORD’, ‘password_here’ );
/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost’ );

So, the WordPress needs these pieces of information to access your database.

They are:

• The name of the database (“DB_NAME”)
• The login username (“DB_USER”)
• The login password (“DB_PASSWORD”)
• The database host (“DB_HOST”)

The WordPress will not be able to connect to the database in case any of these values is not correct.

You should use PHPMyAdmin for accessing and editing the database. It can be located in the cPanel dashboard.

PHPMyAdmin will display a list of databases on your server. Click on the one that matches the name in your wp-config file. It will show tables in the database. Locate the table named, “wp_options” and click on the browse button next to it. You should see your website name, URL, and other general settings here.

If the database name is not correct, fix it in wp-config.

After this, you have to check the username and password.

This can be done by creating a simple .php file to test whether you can connect to the database with the credentials in your wp-config file.

Or

Create a new user and password and update the wp-config file with the new info.

Once you have corrected your database name, username, and password, you just have to correct DB_HOST. In most cases, this value is “localhost.”

Method 3: Fix Corrupted Files

If troubleshooting the database doesn’t resolve the error, then you should try to fix any corrupted files in WordPress. But, before we go ahead, you should backup all your data either manually or by using a plugin. We need new and fresh WordPress files so that the problem can be resolved.

For this, you need to download a fresh copy of WordPress CMS.

Then delete the whole wp-contents folder and the wp-config file. Finally, paste the remaining files into the WordPress root folder. You can use an FTP client. It will replace the corrupted files that are causing the error.

If the error persists, you should contact your web hosting service provider as it is possible that the error occurs at the server end. You should tell them about the measures you took to resolve the problem.

Concluding

The Error Establishing a Database Connection is one of the most annoying errors that you can get with your WordPress site. However, as we have seen, it can be fixed. I have illustrated three easy methods to fix it. If you follow these steps, you can have your site up and running in no time.

I would appreciate if you can share with us which method worked for you. Do let us know your experience in the comments section below!

WPX Hosting

WPX Offers the Fastest Managed WordPress Hosting.

Get Now

SEMRush

SEMRush is all-in-one SEO Software.

Get Now

Adsterra

Adsterra is Ad Network for Publishers with a 100% Fill Rate.

Get Now

Rank Math

Rank Math is a powerful WordPress SEO Plugin.

Get Now
Scroll to Top