How to Fix the Error Establishing a Database Connection in WordPress?
Using WordPress can become frustrating if you get the obstruction in your working process. Most of the errors are quickly resolved but what irritates to the optimum level is when as a user you see a hindrance in form of ‘Error Establishing a Database Connection’. This is the error which you could encounter on your site for many reasons. Before talking about the solutions, let us simply define what this error means. In clean and shortest meaning, it means that your database is unable to establish a connection with your site and this is why no contents of your site are displayed.
After a lot of brainstorming and in-depth research, we have found that there are four common and most frequently occurring causes that establish this error on your site. These reasons are Wrong login credentials, Corrupted database, corrupt files and troubles with your database server. We took a deep dive in all of these four issues and have a crystal clear understanding of removing them as well. In this write-up, we will take each of these issues one after the other and discuss them and with their solutions in the most effective manner to help our readers to resolve the problems. Read on!
1) Wrong/Incorrect Login Credentials In Your Database
This is one of the most frequent reasons why the error occurs in establishing your database connection. It has come to notice that when you have opted for new host provider, this error then occurs in 90% of the cases. The details of connection in your WordPress site are kept in the wp-config.php file. You can find this at the root of your site. They are named below:
- Database name
- MySQL database username
- MySQL database password
- Server issues
The easiest and simplest way to access your wp-config.php file is by using cPanel. Click on ‘File Manager’ and browse to the root of your site, then right click on edit to edit the file.
2) Managing Corrupt Database
The common hint to know whether the problem is existing due to the corrupt database is when one of the ends of your file works and the other does not. For solving this, you have to download your wp-config.php file and then add the line-
define (‘WP_ALLO W_REPAIR’, true);
After doing this you will be able to access a repair page on your site by going to http://yoursite.com/wp-admin/maint/repair.php but don’t forget to replace ’yoursite.com’ with the actual name of your site. Just as you do this, your screen will show you two options namely- Repair Database and Repair+ Optimize Database. You can choose either option and get your database repaired. The recommendation here would be to choose ‘Repair Database’ as the other options take a bit longer time. Either option will get your job done. After you successfully do this, go to the wp-admin.php and remove the earlier mentioned line of code from the file.
3) Managing And Repairing Corrupt Files
If your files are corrupt then this could be a mighty reason that you are getting a message of ‘error establishing database connection’. The reason why your files are corrupt can be many. Some of them may be a hacker getting access to your files, transferring files through FTP or any other. The good news is that the corrupt files can be fixed. The way to do this is to do an entire replacement of WordPress core version of your site. This does not mean that you are changing or replacing content of your site.
Stuff like plugins, themes, media etc would be even touched while you carry this process. This is very simple to do. Go and download a new copy of WordPress from the site WordPress.org. Just as you do this, unzip this file in your computer system and delete a wp-content folder and wp-config-sample .php.file. Upload all the other files through SFTP along with choosing the option of overwriting on your existing file. As soon as you do this, your corrupted files will be displaced and you will be having clean files. You can go and clear your browser cache after this process. This method will take your corrupt files away.
4) Database Server Issues
There is surely a problem in your hosting provider’s service if the above-mentioned methods did not resolve your problem. When many connections connect to your database, the problem appears and the error occurs in establishing a connection with the database. This happens because hosts always put limits on their connections and when the surge occurs, the problem arises. The overcrowding then becomes the reason for your site to establish a connection to your database. The relaxing thing is that this issue can be fixed. For troubleshooting this trouble, one can make use of caching plugin. You can use best-caching plugins like W3 Total Cache, WP Super Cache to recommend a few.
Important– We highly recommend having a strong back-up for your site before you apply any of the above-mentioned methods to fix your problem. If you do not have much knowledge about troubleshooting or if you are encountering this problem for the first time, try to hire a professional helper in this case. Altering the names and languages of the website is a tricky process and you can fall into a deep ditch if you do not do them correctly.
If unfortunately your error does not get resolved even after applying all the aforementioned methods for resolving an issue, then we would recommend you to talk to your host provider to look into the matter. They are professional people and they know how to carry the works in a good manner. Do not make the rookie mistake of trying any type of guesswork with the settings of your WordPress site. You may get into deep and complicated problems as sometimes the errors become very complicated to find if you do not remember where you have made the changes in your WordPress setting panel.
Maggie Sawyer is a Sr. Web Developer at MarkupHQ Ltd., a leading high quality of markup conversion service provider company with a global reach. She is an expert of WordPress customization services and able to PSD to WordPress conversion services with guaranteed 100% client satisfaction. She enjoys writing useful tutorials specially related to the world of WordPress.