Before going to the solution, there are some points need to be clear that why this error comes. Let me explain briefly. There are some reason for this error. This error came with my website, after that I had gone through many topics on internet which is related to this, but I could not find solution at the end. I chatted live with my hosting provider and inform them for my problem. They had solved my problem. It doesn’t mean that you can find your solutions from different blog or websites. First I explain you what type of problem I was facing?
How I am receiving error “error establishing a database connection”?
I have gone through different blogs and websites for support and resolve this issue, almost all website or blogs showing the same reasons of this issue. But I give reference of one website from where I got enough information wpbeginner. Here I read the reasons of this issue and solution. But unfortunately I could not get solution from here for my reason of this issue. Because my reason was different. When I write my URL in browser with www I get this error and when I write my URL without www it works perfect. I am writing this experience just because the error is same but reason is different.
What are the other reasons of this error?
There are two main points I got from wpbeginner, In this article Syed Bulkhi has explained very well and in detail. you can see this post If your problem is resides in this category what Syed Bulkhi has explained in his post. I referring two points only from wpbeginner.
1 -Why do you get this error?
Well in short, you are getting this error because WordPress is unable to establish a database connection. Now the reason why WordPress is unable to establish a database connection can vary. It could be that your database login credentials are wrong or have been changed. It could be that your database server is unresponsive. It could be that your database has been corrupted. In our experience, majority of the times this error happens because of some sort of server error however there could be other factors as well. Lets take a look at how to go about troubleshooting this problem.
2- Does the problem occur for /wp-admin/ as well?
First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. If you are getting a different error on the wp-admin for instance something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.
The above two points are enough clear that what is the reason of the problem and your problem is occurring on both front end and back end. For more detail you may visit: wpbeginner.
How I resolved “Error Establishing a Database Connection“?
If your problem resides same as I had facing then you may follow few simple steps, The error ” error establishing a database connection” is seem like that, the problem is with some database connection, like your DB user name or DB password is incorrect or some error in wp-config file. It can be the problem with databse but if your problem is, what I mention above that your website works without www and your website not works with www then this is the problem of redirection. You just go to your cpanel and redirect your www. folder to non www. folder. It will work. Normally this issue comes in godaddy server, as I researched on internet. I am using godaddy server I have resolve this issue with these simple redirection method.
Steps to Redirection in cPanel in Godaddy Server
1- Go to your godaddy cPnel and locate redirect option.
2- Select 302 temporary redirect from first box.
3- Select your URL from where you want to redirect, such as www.yoursite.com. Do not write after slash, leave blank.
4- Type your URL where you want to redirect, such as http://yoursite.com
5- Select “Redirect with or without www.” from next toggle button’s options. click on Add button.
Done! hope this will work for you.