Not a single web developer wants to see their website crash. Imagine the amount of traffic lost during the time your site is inaccessible! It’s even worse for ecommerce owners – one minute of your site crashing could make hundreds of potential customers look the other way. Bummer, right? That’s why you need to look out for the “Error establishing a database connection”.
When dealing with a number of errors your site can experience, it is the one that happens most often to WordPress users. You will recognize it when your site only shows a white space with nothing else but the black error message at the top right corner. If this happens, you need to make sure you have the proper knowledge to get your site back up and running. I’m going to explain you the root cause of this problem and how to easily remove the error from your site.
Finding the Root of Your Problem
You can’t expect to handle this situation well if you don’t know what leads to this issue – especially when there’s a number of factors that could cause the problem. So, identifying the heart of the matter is a must.
First things first, you need to understand that WordPress is built on PHP and MySQL – both work hand in hand to provide you the ability to create a website and publish blog posts with ease.
You can post your content dynamically thanks to PHP – the server-side scripting code. When an audience visits your website, he sends a request to the host server. Then, PHP gathers the needed information from MySQL (the database filing system that stores and manages all of your content) and displays it on your audience’s browser.
Now, the “Error establishing a database connection” is related to WordPress databases. The error message will appear when PHP fails to communicate properly with MySQL database servers.
Reasons Why This Error Might Come Up
Now that you have figured out the root cause, the next thing to do is finding out what conditions may cause the communication problem between PHP and MySQL.
It may either come from your end or something related to the state of your server. Below is a list of reasons why the error might come up:
- Corrupt WordPress files. It can happen if the connection is interrupted while you’re updating your WordPress version. The files won’t save properly and that can cause your core files to be corrupted.
- Corrupt database. This is also why your site might crash – bad setup of permalinks, incorrect image location paths, and malicious attacks are among the many reasons why your database might become corrupted.
- Servers for the database are down. PHP won’t be able to reach your database if the servers are down. It can be caused by server hardware crashes, software malfunctions, DOS (Denial of Service) attacks, etc.
While knowing the causes is essential, identifying which reason is causing the error might prove to be difficult because every case is different.
Rest assured, I will show you how to deal with these problems and get everything back to normal. That being said, it’s possible your site is down simply due to traffic overload – your website is getting more traffic than your hosting service allows. If this is the case, all you have to do is just upgrade your hosting package plan to get more bandwidth.
Fixing the “Error Establishing A Database Connection”
Before we go straight to the solution, there’s another layer of problem identification you must do – to see whether this error happens on the front-end or the back-end of your site.
If you can access your WordPress admin dashboard (/wp-admin) without any error messages, then you move to the next step and try to repair your database using the guide below.
Repairing A Corrupt Database
The first step you need to take is to enable the option to repair the database by inserting a line of code to your wp-config file.
To do this, you need to use either FileZilla to access the file through FTP (File Transfer Protocol) or the file manager in your hPanel, if you’re using Hostinger services.
(Option A) Using FTP
If you choose to use FTP, first you need to have an FTP client like FileZilla installed on your PC.
Then, if this is your first time connecting an FTP client to your hosting provider, you need the necessary credentials (host, username, password, and port). Usually, this information is located in the hosting control panel.
After you successfully access your FTP server, then simply look for the file wp-config.php in the root folder.
Once you have located the file, right-click on it and select View -> Edit. A window asking you which program to use will appear – select the default option.
Using your text editor, look for the line that says “That’s all, stop editing! Happy blogging.” and insert the following line below it
Don’t forget to save the file and take another look at FileZilla. A window will appear telling you that a change has been made – click Yes to send the file back to your hosting account.
(Option B) Using File Manager on hPanel
There’s an alternative method for less experienced website owners, who don’t wish to use FTP. You can also choose to use a file manager to edit your file. We’ll demonstrate it using Hostinger’s hPanel.
First, log in to your hPanel and scroll down until you see the Files section.
Then, click File Manager, select the domain of the site you’re trying to fix, and click Go to File Manager.
Once you got inside the file manager, find the file wp-config.php. Tick the checkbox next to it and click the Edit button that has a pencil icon.
Same as before, insert the line
define('WP_ALLOW_REPAIR', true); after the line that says “That’s all, stop editing! Happy blogging.” Save the file, and you are good to go.
Once you are finished with either of the guides above, the Repair Database option will be enabled. So, all that’s left is to navigate to the repair URL. It will look something like this: http://yoursite.com/wp-admin/maint/repair.php.
If the page is the same one as it appears above, then you did succeded at enabling the repair option for your WordPress site. If not, perhaps you need to go back and make sure you didn’t miss any steps.
The next thing to do is click Repair Database and WordPress will automatically fix any detected common issues in your database.
Important: Once you enable the repair option, the page above is accessible to anyone. So, make sure to remove the line
define('WP_ALLOW_REPAIR', true);after you’re finished repairing.
Repairing Corrupt WordPress Plugins or Themes
The general assumption behind this step is that there is a corrupted plugin or theme that messes up your WordPress site. The question is: which one?
So, to identify the source of the problem, you will have to eliminate the problematic files and see if that fixes the problem.
You can either use FileZilla or the File Manager to get the job done. Since I have already explained how to navigate to your core files using both methods, I’ll explain how to deal with corrupted plugins and themes.
The process is exactly the same as with the file manager on hPanel, so I’ll just use FileZilla for the demonstration.
First, navigate to the wp-content folder and look for the plugins and themes folders.
Then, simply rename the folder of either plugins or themes.
In my case, I changed the folder name to plugins1234567890. This has the same effect as deleting the folder. Once that’s done, check your site again to see if the error still persists.
If your site is up and running again, then you know that there’s a problem with one of the plugins you installed.
Go to the plugin folder you just renamed to see a listing of your installed plugins. Your job now is to check your site again after you apply the same process to each plugin.
Let’s say you have a suspicion that Flash Toolkit is the plugin that’s corrupted because your internet connection was down during the installation.
Rename the plugin folder and check your website once again. If the error is gone, then you’ve found your culprit.
If not, then you’ll have to redo the process for the rest of your plugins one by one. Note that this also applies to themes – if the error is still there, then you’ll have to look for the corrupted files in the themes folder.
Repairing Corrupt WordPress Core Files
If you already tried the steps above and still see the error message, maybe the problem lies in your WordPress core files.
The solution to fixing this problem is by reinstalling WordPress. Even when your site is down, you can do so with the help of FileZilla or the hPanel file manager.
First, you need to download a fresh copy of the WordPress installation files from its official page and unpack it.
Then, navigate to the public_html folder on your FTP client or file manager. This folder contains all the WordPress installation files that you will replace.
Important: If you wish to save your content, make sure to delete wp-content folder from the new WordPress installation package so it wouldn’t replace the old one stored in your hosting server.
In the public_html folder, upload the new files to replace the old ones, and you’re all set!
The “Error establishing a database connection” message is something that no WordPress user wants to see.
Even though there are a handful of approaches to fixing this problem, futureproofing is not something that can be done with this specific issue.
There is no framework or content management system that’s immune to bugs or errors – not even WordPress. The least you can do is prepare yourself by getting familiar with basic knowledge of accessing your core files.
If you always want to be on the safe side, you can create regular backups to avoid losing your data. Having high-quality hosting helps a great deal too – great server performance and maintenance can reduce the chance of errors happening, and a responsive customer support team can lend you a hand when troubleshooting.
Now that you know how to handle the “Error establishing a database connection”, it no longer seems that terrifying, doesn’t? Good luck!