If you are using your website in WordPress then you may have come across an error called “Error establishing a database connection”. It may look like a serious problem as you don’t get any other elements except for just texts. This error is also relevant to something called “White Screen Death” (WSOD).
What the screen tells you along with the error statement, is that you no longer have access to the database of WordPress. As a result of this, the entire website that you own goes down with it.
Keep in mind that to avoid any kind of impact on sales or any traffic, you must resolve the issue as quickly as possible. If you have this issue and wondering what to do then follow the contents below to find out more about this!
What is Database Connection Error?
Before you can jump into solutions, it is important that you first understand why this error happens.
WordPress solely works on two important aspects known as PHP language and MySQL. While PHP is the language the WordPress is built on, MySQL stores all the information on the website in the form of a database.
So whenever this error comes up, WordPress becomes unable to get any information from the database. This means, it cannot get access through the commands of PHP. So naturally, WordPress does not know what it needs to do, hence this error comes up on the screen.
Causes Behind Database Connection Error!
Here’s a list of reasons that could possibly trigger this error:
- Login Credentials are incorrect.
- WordPress files are damaged/corrupted.
- The Database is damaged/corrupted.
- The server of the database is down.
- High Spike in traffic.
Now that you know the trivial reasons, you can proceed to follow the section below to get the issue fixed immediately!
How to Fix Database Connection Error?
In this section, there are some easy solutions that you can apply to fix Error establishing a database connection.
1. Connect to Web Host Provider
If you come to find that your website is down when it was working well minutes ago, then it may be that the fault is at the end of Web Host.
In that case, it is advisable that you get in touch with the web host. You can also consider doing a live chat with the web host telling them briefly about the issue. In any case, you’d be able to know what the issue is and also let them fix it for you!
2. Check the Theme Files/Plugins
Due to some misfortunate incidents, it is possible that your WordPress files might be damaged or corrupted. This could happen due to many minor reasons such as setting up a theme or a plugin and then the website collapsed. Or due to editing files from WordPress.
The first thing you need to do is to make a connection to the host through FTP. In order to do that, all you need are the login credentials. However, if you do not remember then you can go through the early email messages from the host.
After you’ve connected, go to the root folder of WordPress which is named as “public_html”. Under that, you will see some sub-folders that are named as “Wp-admin” or “wp-content”.
Go to “wp-content”, select and rename the folder called “plugins” as something different than the original. Then, open the WordPress site and check if you are facing the same problem or not.
If you come to find that the error is still there, then go back to the root folder and keep renaming the folder names into different names than the original. Keep repeating this until you see any changes in the error.
3. Verify If the Database is Corrupted or Not
The database of WordPress is complex. Hence, there are trivial aspects of the database, that need to work unitedly. Failing that would result in everything going out of its place.
First, go to the Dashboard and look out for the error. If you see the following statement, “Error establishing a database connection” then you can skip the upcoming step.
However, if you come to find something else, then read what WordPress is telling you to do. If you see a statement like this “One or more database tables are unavailable” then you need to fix the database.
Establish a connection to your site, using FTP. Then using the root of WordPress, download the content called “wp-config.php”. Open this file in “Notepad” and then add a command line just before the statement “That’s all, stop editing”.
Here’s what you need to add: “define(‘WP_ALLOW_REPAIR’, true);”
Now you need to upload this edited file on your website through FTP. This will overwrite the old configuration with the newly edited one.
Now when you open the WordPress website, you will have a repairing box that will appear on the screen. From two options, “Repair Database” and “Repair and Optimize Database”, choose either of them to repair the database automatically.
Now check if you’re facing the same problem again or not. In any case, you’d need to reset the old configuration by removing the extra line that you added in the “wp-config.php” file.
4. Verify Database Credentials
As you already know that WordPress uses very precise and specific credentials to make a connection to the database. Usually, it is unlikely for these credentials to change on their own.
However, due to external factors such as the host environment, credentials can change. As a result, this error would show up. In order to fix this, do the following:
Copy these lines specifically from the folder called “wp-config.php”:
Now, get in touch with the web host support and then paste the copied lines into the live chat system. The support representative is supposed to check the details and let you know what to do or what to modify.
Most likely you will be given new credentials. In that case, copy the new credentials and simply paste them in the folder called “wp-config.php”. You need to paste the credentials exactly where you first copied the four-lines that start with “define”.
After you’ve made the changes, using FTP, upload the newly edited file. Now visit the WordPress website and check if the error still persists or not.
5. Restore WordPress Files to Default
Often times, it is likely that the core files of WordPress may get corrupted. This could happen due to unauthorized attempts of accessing by hackers, faulty plugins or sudden modifications by mistake.
This can be fixed if you simply restore the old WordPress files. To do that, simply get the lean version of WordPress from the website and then copy the downloaded file. Go to the “root” folder and upload the new files of WordPress.
After the process is done, visit the website and check if this fixed the error or not!
However, if you still face the issue after attempting the solutions, you can always get in touch with the web host provider. Tell them about the problem in detail and get it fixed.