If you are a WordPress website owner or a developer, you must have sometimes faced “Error establishing a database connection” Error. It’s obvious that you will be concerned but you should not worry too much as there is a solution to that. This problem can be fixed easily as this is a common WordPress error. First let’s understand this problem i.e. what this error is all about? This error is self-explanatory i.e. it’s an error in establishing a database connection. Actually WordPress uses PHP programming language and MySQL database. WordPress uses PHP commands to connect to the MySQL database, gets the information which is needed and then displays it on the screen. The error can come at this time.
This error occurs when, due to some reasons like issues with updation of WordPress Theme or WordPress plugin, WordPress cannot access the information in the database via PHP commands. When this happens, WordPress gives the error “Error establishing a database connection.”
Some of the most common reasons why this error occurs are: -
Wrong Login Credentials
WordPress needs the correct login and password to access the database. If they have been changed then WordPress will not be able to fetch anything from the database.
Corrupted WordPress files
Sometimes due to failed update or due to other reason, the core WordPress files get corrupted. This can happen due to failed update of themes, plugins or the main WordPress software.
This can happen when an unstable or unsafe plugin messes with the database, due to a hacker’s attack or when a theme fails.
The Database server is Down
Sometimes, the web server and the database server are installed on different systems and sometimes the database server may be down for some reason.
Sometimes due to high traffic, your database might become unresponsive.
Right Ways to Fix “error establishing a database connection”: -
Now you know what this error is all about. So now let’s discuss the right ways to fix this problem.
Contact Your Website Hosting Provider
If your website was working just few minutes ago and now it is not working, then there is a good chance that the problem might be at your website hosting’s end. Hence contact your hosting provider through live chat or phone call and inform them about the error. In most cases, they will tell you the cause of the error and will fix it for you. They will tell you if your database is alright and whether there is a spike in traffic or whether there is some hacking attack on the server. If the problem is not on their end, then proceed to the next step.
Check if Your Theme Files or Plugin Have Been Corrupted
There are some instances when your WordPress files get corrupted. This may happen either when you have updated a theme or a plugin which resulted in the problem. If you have not updated anything then skip this step. Otherwise, first connect to your host via FTP using a tool like FileZilla. Login with the username and password you know. After that connect to the server and navigate to the WordPress root folder. It’s generally called public_html or www. If you are there, then you should see the sub-folders like wp-content and wp-admin. Now go into the wp-content and rename the “plugins” folder. Go back to your site and see if the error is still there. If not, then now you know the error was due to some plugin. Now to find out the exact plugin which is causing the problem, go back to FTP, rename the folder back to “plugins” and change the folder names of the individual plugins, one by one and keep checking your website when the error comes and when it goes away. Once you find out the specific plugin which is causing the error, delete it or search on internet for the solution. If the error is still there even after checking all of the plugins, do the same process with the “themes” folder. If this too doesn’t work, then the error is caused by something else.
Check if Your Database Has Been Corrupted
The WordPress database is a complex thing and sometimes something wrong happens inside it and it throws the error “error establishing a database connection.” First of all, check your WordPress dashboard – yoursite.com/wp-admin
If you still see the same error, then skip this step else see whether WordPress is suggesting something for you to do. WordPress has the ability to auto-resolve some of the problems and errors. If you see the message “Can’t Select the database” then follow the steps WordPress is suggesting and your problem may get resolved. If you see a message “One or more database tables are unavailable..”. Then you can repair the database by clicking on the Repair button which WordPress is showing. Once the automatic repair finishes, go back to your site and see if the error is still there. If error vanishes then you have done. Otherwise move on to the next step.
Check Your Database Connection Credentials
We have told you that WordPress uses the correct username and password to connect to the database. Generally, these access credentials remain same and you just have to remember them and use them as it is in the file wp-config.php.
But sometimes due to some of your actions like when you make changes to your site files, the hosting environment or anything else, these access credentials might change and it starts throwing “error establishing a database connection” error.
As the username and password to connect to the database are stored in the file wp-config.php hence you can copy these details from this file and send to your hosting provider and ask them whether these details are correct or not. The company technical support professional will check these details and will come back to you with some feedback. They will provide you with the correct details if something has changed. You just need to login via FTP, download the file wp-config.php, do the required changes into the file and upload it back via FTP. Now check your site to see whether the error has been resolved or not. If not, then move on to the next last step.
Restore the Default WordPress Files
In some cases, the core WordPress files can get corrupted due to many reasons for example if there is a hacking attempt on your website or when you have modified some files by chance, or due to the installation of a bad plugin. Whatever reason it might be, you can still restore the default, native WordPress files. The best way to do that is to just download a clean version of WordPress from WordPress.org and copy it over the one you have right now. For this go to the WordPress official website https://wordpress.org/download/ and download the latest package to your desktop. Now connect to your website via FTP, go to the main WordPress root, upload your new WordPress files and overwrite all the previous files. After this step, check your website again and see the result. This will resolve almost all of your problems.
If you have followed all of the above steps and process carefully then you will surely resolve and fix the error “error establishing a database connection” which you might have been facing with your WordPress website. If in some rare case, the error still comes then you have no choice than contacting your website hosting provider and telling them everything what you have done, what worked and what didn’t. Now it is the hosting provider who can do something to resolve this issue. If you wanted to avoid this error from coming in future, then you should take some steps. First thing is that always go for quality hosting. Choose the one which has optimized their platforms to work with WordPress. Second most important thing to do is to have backups. Install a simple backup plugin which works automatically and you will always have a working WordPress backup which you can restore anytime when everything else fails. I am sure the above steps will help you in resolving and preventing the error “error establishing a database connection” with your WordPress website.