Every website that is currently active has to go through this problem which is the “Error Establishing a Database Connection” because of the complication that a website goes through with the management of the database and so.
This is a very common error and there is nothing to be worried about.
What is “Error Establishing a Database Connection” Error?
This error usually takes place when the webserver is unable to connect with the database. Now there can be a lot of reasons for the webserver to not reach the database.
They can also vary a lot. The most common problem is that wrong or invalid settings such as wrong SSL settings.
There are multiple changes that this problem can be occurred due to the hosting too, so it’s really important to use a proper and reliable hosting provider.
Basic Understanding of the Error:
WordPress CMS (The CMS that you are currently using) uses two technologies and has to utilize those. They are
PHP is the core language in which the complete WordPress framework is developed in so PHP is really essential for the proper functioning of WordPress Website or Blog.
MySQL is basically a database application that most of the websites and blogs use. All the data that you provide are pretty much stored in the MySQL database. I hope you get how important that is.
So essentially WordPress uses PHP and MySQL both interconnecting to power a WordPress Website and Blog.
Causes of “Error Establishing a Database Connection” Error
#1: Corrupted WordPress Configuration Files:
The core WordPress configuration file can get corrupted sometimes due to some of the technical updates or wrong code updation.
Let’s say you change some of the code in the WordPress configuration file and they are wrong then the WordPress won’t be able to connect to the database and the problem cannot be verified.
#2: Too Much Real-Time Traffic:
I know how can too much traffic can be a problem, but yeah there is certain capacity that the database can handle.
Let’s say you have the 5GB bandwidth and the website get 1,000+ Real-time suddenly due to one of your post went viral on the Social Media.
5GB bandwidth which not optimized problem can be able to handle the traffic so it panics and goes down blaming the problem on the database because the logs can be transferred to the database.
#3: Corrupted Database:
This is the major problem that happens when the logs or the plugins or the theme developer messes up the database log then to database get corrupted to protect itself and stop any other external attacks.
#4: Incorrect Login Credentials:
This happens to a lot of people. Let’s say you have a security plugin and you have maximum login attempts and when you exceed that the database would be really affected and the developer has to change the maximum restricted to more.
Methods to Fix “Error Establishing a Database Connection” Error:
- By Contacting the Web Host Provider
- Check if the Database is not corrupted
- Check the Database Login Credentials
- Restore the Default WordPress Files
Method 1 – By Contacting Your Web Hosting Provider
This is a major problem that occurs if you don’t really have a good and reliable hosting service.
This method can be used if your using a shared hosting where your website will be hosted with multiple sites and all those sites would have interconnected database connection.
So let’s say if your web hosting provider got an external attack such as DDoS, it messes up the databases that are hosting on those servers.
So it is really important that you contact your Web Hosting Provider either using the Customer Support Email ID or Live Chat or Contact Number or Raise a ticket.
Explain to them the situation and ask them if the problem is from their end and ask them to fix it as soon as possible.
Method 2 – Check if the Database is not Corrupted
WordPress Database is complex stuff to understand and with this complexity, it can cause a lot of problems such as Error establishing a database connection.
- Firstly, Go To your WordPress Dashboard or Backend. (Ignore these steps if you get the error “Error establishing a database connection”).
- Closely read the error that your website is displayed. With the new update in WordPress, they are keen on informing people about the error and its explanation. See if you can find good advice and try and implement that.
Another possibility is that you’ll see a message starting with “One or more database tables are unavailable […]” This means that it’s you who needs to “repair” the database.
This can be solved easily. All you need is access to FTP files from the website.
Connect to your website through FTP(File Transfer Protocol). You can download FileZilla.
Go to the Main WordPress Root File and download the “wp-config.php” file to your computer. Edit the file and add one line of code at the end (right before “That’s all, stop editing”)
Now upload the file back to your web server using FTP and overwrite it with the previous file.
Finally, Goto example.com/wp-admin/maint/repair.php
When you go to the above URL you will get something like the image below.
Click “Repair Database” or Repair and Optimize Database” and let it repair. Once the repair finishes go back to your site and see if the error is still there.
If your website is working then fine stop right here or try the next method.
Method 3 – Check your Database Login Credentials
WordPress uses a required and specific username and password to connect to the database. Usually, these access credentials don’t change but due to some issues or connectivity issue with the plugin or theme can cause this issue.
This issue can be fixed with wp-config.php file. These are the lines that we are talking about
They hold all the details of your Database and if changed it will cause problems connection the website to the database.
Copy those four lines of code and contact your web hosting provider and give them these 4 lines of code and ask them if it’s correct and if not ask them to fix it.
Most probably they will fix it (Depends on the Quality of your Web hosting provider).
Once they fix it they will revert back to you with the correct lines of Login Credentials. Now all you need to do is paste the code in wp-config.php and upload the file back to the server and replace the file with the older version via FTP
Method 4 – Restore The Default WordPress Files
As mentioned about the core files of WordPress can also be a cause for this problem.
Usually, it is the WordPress files that get corrupted due to multiple reasons such as the external attack on your website or the theme that your using or the plugins.
Whatever it is you need to restore the default WordPress Files.
The easiest method to do this is to Download a Fresh WordPress Package from WordPress.org and replace the files with your current ones.
- Go to WordPress.org and download the Updated New WordPress Package.
- Unzip the file.
- Open the browser and find the file “wp-content” folder.
- Delete “wp-content” folder.
- Connect your website via FTP.
- Goto Main WordPress Root directory.
- Upload the New WordPress Files and Overwrite it with the previous ones.
Usually, this method would have cleared the problem and you are ready to go.
How To Avoid This Error In the Future?
There is no 100% foolproof method to not have this problem in the feature. But I can give you people some tips or cautionary in the future to not get this problem.
- #1: Buy Quality Web Hosting Provider. 90% of your work in protecting your website comes to this. Everything depends on how your web hosting provider is. Make sure you buy a reliable and secure Web hosting service.
- #2: Avoid using unsecured themes and plugins. Don’t download the Paid version of theme and plugins for free from the third-party websites because these themes and plugins could be inserted with malicious code and that would really screw up your website. Always stay original to not have these problems in the future.
- #3: Always take backups. There are multiple automated backup plugins for WordPress, use those and make sure you always have backups of your website for any future use. Nobody can predict the future so it’s better to take care of it.
I hope that these four methods that are mentioned above work. If you have any more problems, please don’t hesitate to comment below and please share this article with your friends.
- I Bought A Domain Name Now What? 7 Amazing Things To Do (with Bonus Tips) - April 30, 2020
- A Beginner’s Guide to Website Speed Optimization ⚡ Fix Your Slow Site - April 16, 2020
- Top 13+ Best Free WooCommerce Themes for Your Store in 2020 - March 16, 2020