You are not alone if you are facing this issue on one of your websites. Every day, many WordPress users face database connection error and put hours in resolving it.
If you want to make sure that the error establishing database connection is resolved instantly, you need to act wisely and make necessary edits in your website’s database files.
This post will help you understand the real reasons behind this error and how to resolve it efficiently.
By the time you finish this guide, you will learn how to fix this error in a hassle-free manner. But before that, let’s try to get some basic information about the error establishing the database connection and why it occurs.
1 Why Does This Error Occur?
There are various reasons, such as wrong database login credentials, unresponsive database server, corrupt database, etc. because of which WordPress fails to establish a proper database connection. The most common reason is the unresponsiveness of web server that can be dealt with by contacting your web host.
Just in case, if the problem remains unsolved, here is a brief guide that can be referred to fix this error. Follow this step by step tutorial to correct database connection error in a hassle-free manner.
2 Fixing Database Connection Error In WordPress
Most of the WordPress errors are self-explanatory and give users ample hints to locate the infected file or the line of code. However, the error establishing database connection is entirely different and gives you no information regarding the conflict or error in WP files.
It’s up to you to use a trial & error method to locate the infected file or code and fix the issue. The more time you take in finding the infected file, the longer this error will persist and affect the overall user behavior.
So, act quickly and fix this error without any further delay.
Important Note: Before you move further and make any change in your WordPress database, create a backup of your WP files to avoid any problem if things don’t turn out as per plans.
2.1 Locating Infected File
A critical troubleshooting tip that may help you fix the problem is finding out if the problem is on the front end or the backend of your site. The front end of the website is its home page or other pages that are visible to everyone:
You can try to login to your site and see the response. If the website looks great in general but shows the ‘error establishing database connection ‘whenever you try to log in, it means there could be something wrong with the admin panel files which needs to be repaired immediately.
To make specific changes in admin panel files, you can log in to your website via FTP. Use any FTP client as per your choice; we are considering FileZilla for demonstration purpose:
Once you have logged into FTP, go to public_html file and look for the wp-config.php file:
After locating wp-config.php, right click on it and download the file on your system to make precise edits:
The downloaded file would look like this:
Since the site was showing ‘error establishing database connection’ every time you tried to access ‘wp-admin’ page, you need to make changes in your site’s database accordingly.
What you can do is scroll down to the bottom of the wp-config.php file and add a simple line of code “define(‘WP_ALLOW_REPAIR’, true);“ which will look like this:
By adding this line of code, you’re defining you want to repair the database of the site.
Once the code is added, save the file, upload it to your web server and go back to the website:
Now that you have edited the code to make changes in the database of your site, you can go to your website and access ‘yourwebsite.com/wp-admin/maint/repair.php‘ page.
Based on your requirements and criticalness of the error, you can choose either ‘Repair Database’ or ‘Repair and Optimize Database’ option. Click on any of them, repair your database and see whether the site has started functioning smoothly or not.
This step will most likely resolve the database connection error and bring your site back to normal.
Please delete the line of code you added in the config.php file if your site has started working properly.
Sometimes, the error persists even after editing the code and making changes in the database files. It can be frustrating as you don’t know exactly where to look for the right solution. If you ever face this problem, follow the below-stated steps.
Whenever the database connection error doesn’t get resolved by repairing database, you can again login to FTP, access public_html and click on the wp-config.php file.
The wp-config.php file controls a host of elements, such as database username, password, hostname, etc.
Your website shows the error establishing database connection if any of these items is wrong. So, make sure all of them contain the right information.
Often, while installing WordPress on your server, these elements are downloaded automatically and cause no problem. However, if they are not in the correct order, you might face an error.
At times, you may have to put your IP address in place of the local host or database hostname. If this information is incorrect, you need to contact your web hosting provider and request for the right information to get the website live once again. The process won’t take more than a few minutes if executed correctly.
Let’s take an example to understand how these elements can cause the database error. In the below-mentioned screenshot, we have removed password from the database:
If we save this file, refresh it and go back to the website, we’ll see the database connection error:
So, even a tiny element like the wrong password can cause database error and put your site at risk.
Now, let’s go back to wp-config.php, add the correct password, save it, and refresh the website. As soon as you do it, the site will go live and start functioning smoothly.
There are multiple ways through which the error establishing database connection can be fixed; however, using wp-config.php file via FTP and contacting your web host for support are some of the common ways to get rid of it.
Often, the web hosting company resolves the database connection problem by itself or provides you necessary database file information to troubleshoot the error. In some cases, it can also ask you to reinstall the WordPress to resolve the database connection error.
So, don’t freak out when your site shows error establishing database connection next time. Instead, follow this step by step guide, look for the conflicts in your database files and make necessary changes. The process won’t take much time and save your site from going offline.
3 How To Fix The Error Establishing Database Connection In WordPress? – Video Tutorial
You can also check this video and follow the steps mentioned in it to resolve the error establishing database connection-
Did you face any trouble or get stuck at any stage while following this guide? Please share your doubts, suggestions, and feedback with us in the comments section.