Error Establishing Database Connection in WordPress

How to Fix an Error Establishing a Database Connection in WordPress

Among the most common WordPress errors, the «Error establishing database connection» is one of the most frustrating. This isn’t because it is difficult to fix, but because a myriad of things can cause the error.

This can make identifying the root cause quite difficult. All the time you have to spend looking for the cause is time that your website is not available to the public.

Or in other words, you are not going to be able to make money.

Today, I am going to demonstrate how to identify and fix the «Error establishing database connection» error.

What Causes the «Error Establishing Database Connection» Error?

There are quite a few things that can cause this error to occur and some of them are very simple, which is why the error is so common.

For starters, the most common one is that you have entered the incorrect login information. Obviously, this is the easiest problem to fix, as you need to enter the correct login credentials and the error will resolve itself.

Sometimes this can happen when you rely on your web browser to store your password but didn’t update it after changing credentials.

Now, since you are still reading, I’m going to bet that was not the problem. Thus, the next cause is if the database itself has been corrupted. While this is very unfortunate, it can be quickly fixed by restoring it with a backup.

You can identify if the database has been corrupted if the message on the front and back end of your website matches. If they do not, you have a corrupted database.

This can be caused by a cyber attack or just a faulty plugin.

Another possible cause could be that there is something wrong with the server on your web host’s side. In this case, you need to contact your web host.

Let’s take a look at how you can troubleshoot this error.

How to Fix the «Error Establishing Database Connection» Error

Due to the many potential causes that this error can stem from, you may find it less frustrating to restore your website using a backup than actually trying to troubleshoot. This will get your website back online much quicker.

That said, you may have made the mistake of not keeping an up-to-date backup, which would eliminate that option. If that is the case, remember to set up an automatic backup once you figure out the problem.

Backups are really the fastest way to resolve this error. That is as long as it’s not something installed that is creating the issue, such as a faulty plugin. In that case, you may have inadvertently backed up the root cause of the error.

Method 1: Repair the Website

This method is applicable to websites that believe that the web server has been corrupted. Again, you can tell this is the case if the messages on the front and back end differ from one another.

While this might sound like it’s going to be difficult, it’s actually really easy to pull off because WordPress has a repair mode. The only issue is that it is not enabled by default. Thus, we need to turn it on.

To do this, log into your cPanel and click on the File Manager option.

Click on the File Manager option

Enter the public_html directory and locate the wp-config.php file. Right-click on it and select the Edit option.

Click on the Edit option

Don’t be intimidated by the code here. All you need to do is scroll to the very bottom of this file and paste the following line of code:

define('WP_ALLOW_REPAIR', true);

Save the changes to the wp-config.php file. Now, enter the following URL into your web browser, but replace the «myURL» with your website’s URL:

https://www.myURL.com/wp-admin/maint/repair.php

This will bring you to the repair screen. Click on the «Repair and Optimize Database» button.

Repair and optimize the database

This will allow WordPress to fix any problems found within the database and should resolve the error if corruption was the source.

Method 2: Deactivate All Plugins

Plugins are great tools that add unique features to your website, but they are also the most common cause of errors in WordPress. First, think about if any plugins were recently added or updated.

Those are most likely the culprit, but to be safe, I recommend deactivating all plugins. This will give you a clear answer if a plugin is behind the error or not. While there are multiple ways to go about it, I think the easiest is to use the bulk option.

Go to the plugin area of WordPress and select all plugins.

Select all the plugins

Use the Bulk Actions drop-down to select the Deactivate option. Then click on the «Apply» button.

Deactivate your plugins to fix the "error establishing database connection" error

Check to see if the «Error establishing database connection» is still there. If it is not, then a plugin is to blame. At this point, you want to begin activating each plugin one at a time. After activating one, check to see if the error returns.

This will help you identify which plugin is the problem. Once found, you can check the plugin for any custom code that may cause the issue, delete it, or try rolling it back to a previous version.

If the error is still there after deactivating the plugins, then they are not to blame.

Method 3: Change Your Theme

Just like plugins, themes can also be the culprit. And this is especially true when you are heavily modifying your theme. It’s very possible that an update to the theme did not like the custom code you added, and thus, you now have an inaccessible website.

First, you should determine if it is the theme or not by swapping your active theme.

Switch themes to fix the "error establishing database connection" error

After this is done, check your website to see if the error is gone. If it is, you likely need to check your old theme and look at any custom code that was added. If the error still persists then the theme was not to blame.

Method 4: Check the Login Credentials

Alright, I mentioned this earlier, and if the previous steps have not worked, then this really might be the reason why you are seeing an error. Now, even if you are sure you are entering the correct login information, you might have been the victim of a cyber attack.

Depending on what the hacker’s goal was, they may have changed the login information in an attempt to slow down your response to the attack. That said, there’s a good chance you didn’t even know you were under attack.

Therefore, I recommend verifying the login information. This can be done by viewing the wp-config.php file.

I demonstrated how to find it in the first method, so refer to that if you need help finding it. Once you are in the file, you need to find three specific lines of code that contain the database login information. They look like this:

/** MySQL database username */
define('DB_USER', 'Username');
 
/** MySQL database password */
define('DB_PASSWORD', 'Password');
 
/** MySQL hostname */
define('DB_HOST', 'localhost');

Verify that the information matches what you are using. If not, then someone might have changed it on you.

Call Your Web Host to Fix the Error Establishing Database Connection Error

The steps above should have resolved the error if it was on your end, if not, then the error is probably on your web host’s end. Call them up to be connected with their support team.

They will be able to work with you and resolve the problem. If not, it’s time to switch to a new web host.

Deja un comentario

Tu dirección de correo electrónico no será publicada.

Este sitio usa Akismet para reducir el spam. Aprende cómo se procesan los datos de tus comentarios.

Copy link