People who have worked on WordPress for a long time have almost certainly encountered the dreaded error known as the "error establishing a database connection".
This problem often sends website owners into a frenzy of panic because you will likely find the WordPress admin not working or the front end of your website won't be accessible.
STOP don't panic, take a deep breath and read this post.
The error establishing a database connection can rear its head often without warning. At times, you can be the person at fault. At other times, the systems might have broken, someone might have failed at his/her job or it can be a result of natural calamities – the causes are countless.
It can even include things that are seemingly good such as having an increase in website traffic which could break the website leaving the error and the associated bad feelings.
Like any other software, the WordPress platform will at times throw errors but none of them is as stressful and baffling as the "error establishing a database connection" error. The main reason behind that is the error does not provide details relating to what is wrong and it can result from one of many causes.
Jump to possible solutions
To help you understand the EEDC error better, we have to explain the two fundamental components of WordPress. The WordPress platform is made using PHP files, such as sidebar.php, index.php, and others, and MySQL an SQL database.
PHP is usually in charge of every function on WordPress websites such as retrieving posts, posting comments and posting pages. MySQL is usually in charge of data storage.
A quick example, after typing your websites URL in an address bar and hitting the "Enter" key, the PHP will kick in to fetch and display your homepage’s content from the MySQL database. WordPress stores videos, images, and any such media in the wp-content folder and PHP retrieves it from there. The MySQL database holds the vital information PHP requires to make the website.
If PHP is unable to access the MySQL database for any reason, you will receive the grievous error establishing a database connection on the screen. That is a message from PHP telling you that it could not find the data it required to put your WordPress website together.
The error can result from various issues such as:
The reason behind that is WordPress requires specific passwords or log in to access your database. If they change, WordPress will not fetch anything from your database as it won't be able to log in and connect.
A corrupted database can cause add-ons, bad plugins, data-transfer, and themes interruptions, which will leave the database corrupted completely or short of tables. That might be a result of a rogue WordPress plugin messing up with the database internally. It might be due to a hacker's attack, a failing theme, and some related issues.
An Unresponsive database server could make your database dead (unresponsive, caput or recovering from traffic upsurges). This is often an issue with the database server itself and you may want to contact your host to check.
Even though high traffic is among the best problems you can have, it can be a big problem. Fundamentally, high traffic might make the database crash and become unresponsive. A good example, if one of your most recent posts has gone viral, everyone will try to access your website. As a result, the server might not be able to handle so many visits at once.
The steps to fixing the error establishing a database connection are not dangerous, but you should always have a full backup. If you are already using one of the available backup tools like VaultPress or use WordPress maintenance services. Then the likelihood is that you already have a backup. If you don't, however, you will need to take one before you do anything.
WordPress offers numerous plugins that you can use to back up your data, but after you are locked out of your WordPress site, they will not be usable. If you already run regular backups then you may well already have a working backup from the day or week before.
After the error appears, you will have to go for a hosting management software or cPanel to complete the backup. If you do not have an idea on how to do that, start by talking to your host, who should provide you with the best solutions.
Remember to back up the database in addition to the files on the server because it is the database, which contains your website’s critical information.
If you do not have chat support or direct line with the host, telling whether the database server is malfunctioning or overloaded on their end is hard because the end symptoms can be indistinguishable from your end.
So let us look at each of the causes of EEDC in details and the steps you should take to fix the issue.
Incorrect database credentials are the leading cause of EEDC error. If the error arises when setting up a new WordPress installation, the incorrect database credentials are the main culprit. To connect to the database, WordPress will require four different types of information – the database host, database username, database name, and the database password.
If you don't have this information readily available, you should ask your host for it. You will need to open a file on your website to check and enter these details. The file called wp-config.php is located in the main directory where all your site files live. You can gain access to this through an FTP program or by using your Cpanel file system.
You will then want to look at the lines of code as pictured below and check that all the information is correct. If it is not then correct it and save the file. Just copy and paste the data directly to ensure that it is exactly how it should be. Some passwords will have periods or dashes near the end, don't forget to include them. Just one digit wrong will cause the error.
If you have switched hosts recently, remember to double-check the details of your database host. For most hosts it will be "localhost" but double check just in case.
If you are completing a new installation of WordPress, you will be presented with the fields to enter the information as you proceed through the standardised WordPress setup routine.
In most cases, you will be able to move backward and fill out the information correctly. If that is not possible, you will have to download, edit and re-upload your configuration file situated in your WordPress root directory as outlined above.
Change/check all the values within the second set of parentheses so that you can correct the values by modifying the connection details. After that, your website should be running. If you have already checked all the details and the same error exists, it is time to move on to the next cause of this error.
Some users report that re-uploading the fundamental WordPress files fixes the error. The reason why that works is seemingly a mystery, but in most cases, it has to do with resetting the file permissions or replacing the corrupt core files.
You should be careful to avoid overwriting the config.php file because it contains vital setup information or wp-content directory that holds the plugins, themes, and media. Back up the data before you replace or modify anything.
To get that done, you will have to download another version of WordPress from the WordPress.org and then use your computer to unzip the package. Open the unzipped folder and delete the wp-content folder and config.php file to ensure that you do not replace those on the live server accidentally.
After deleting that, open the FTP client or your Cpanel file manager and start uploading the other files to the WordPress root folder. If no one has made any dodgy modifications to the WordPress core files, overwriting every file on the live server safely should be possible.
Be careful to avoid overwriting the wp-config.php file or your wp-content folder accidentally.
If the front end of your website is working properly but the backend is not (or the vice versa) then the most probable cause is a corrupted database. The screen is also likely to display an error message informing you that some of the database tables are absent. Fortunately, WordPress has a tool that you can use to repair the corrupted database.
Start by downloading the wp-config.php file, which resides in the root directory of the WordPress install.
Open up the file in a text editor (not word) and add the below code to the bottom of the file.
After re-uploading it, you will be able to access the special database repair page. You just need to open "https://yoursite.com/wp-admin/maint/repair.php" (remember to substitute the "yoursite.com" with your actual domain).
A screen displaying "Repair+Optimize Database" and "Repair Database" options will appear. Choosing either one of the two options will be fine, but for this scenario and specific issue we would suggest just using "Repair Database".
After doing that, go back to the wp-config.php and delete the code line from your file. Note that the repair page is accessible and insecure. If it exists in the config file, it is accessible to the public so it's important to remove it once you have used it.
Did this fix your connection error? If not don't worry we have a couple more options to try.
It is hard to differentiate an overloaded server from a malfunctioning one if you do not have access to the advanced tools or without talking to the hosting company. If you think that one of the two issues is the cause of the error establishing a database connection error and the host service provider has chat/email support or a functioning telephone line, you should contact them to find out which it might be.
For shared servers – where people share the server resources with the many other users – telling whether the error is due to a malfunction or overload is hard. It might be that your site is fine but another site on the server has had a sudden spike in traffic which in turn can affect others on the same server.
This problem often resolves itself within a short time. And if the database is physically malfunctioning, the web host will replace it because their systems will tell them to do that.
If the database fails due to an overload, it will reboot automatically and start running within a few minutes. If that has been happening for a long time, alert the hosts so that they can investigate the problem in more detail. If you are relying on a shared server and the traffic has been increasing rapidly, you are likely to experience many database connection issues because you will be overloading the server.
Even though the errors resolve themselves, they are likely to happen again if the traffic surges continue. It might be time to start thinking about upgrading your hosting. It's great you are getting more traffic but if that traffic keeps hitting error pages they won't stay around for long.
Even though it is not a direct solution to the database connection error issue, managed WordPress hosting will save you many headaches. Particularly if you don't understand or don't want to deal with these kinds of issues.
WordPress-specific managed website hosting services are designed for WordPress alone and your website will, therefore, run quickly and more efficiently. The server software, hardware, and the customer representatives will be geared towards WordPress. They will likely run daily backups of your site and happily troubleshoot and fix errors like this for you.
There is no 100% fail-proof method of protecting a WordPress website against the error establishing a database connection.
Go for quality website hosting and backup your website data daily if possible. Before choosing a website hosting service, ensure that they have optimised their platforms to work with WordPress.After choosing the website hosting service and installed WordPress, ensure you only use plugins and themes from reputable companies. This will minimise errors arising from badly coded themes or plugins.