Error Establishing a Database Connection, is one of the most common WordPress errors and adds to one of the most scary errors to ever get in the world of CMS’s. It is almost like a bad website curse initiated by many known and unknown reasons.
While looking around scouring for a solution, no one article adequately gives a perfect cure for this error, today we shall attempt to put all these solutions on one article and hopefully one of them will help you get out of this mud and get your website up once again.
So here you go, lets fix the Error Establishing a Database Connection
Usually your website would use the database which utilizes the MySQL language and the server-side language called PHP. Therefore whenever your website URL/domain name is entered into the browser, WordPress will use PHP to request and retrieve all the information pertaining to the page from the database and sends it or shows it on the visitor’s browser.
Here are several reasons why WordPress does not establish a database connection:
- Database login credentials are wrong or have been changed.
- Unresponsive database server, meaning the DB server could be down
- Server inundated with many requests due to a sudden or continuous heavy traffic spike.
- Corrupted database due to the installation of a poorly coded plugin.
- Corrupt files in your WordPress installation sometimes due to hackers installing scripts.
- Did you migrate to a new hosting provider?
Step 1: Back Up Back Up Back Up
We always advise that before you make any attempts to fix the error establishing a database connection in WordPress, please make a backup! The following are your possible back up options:
- Use the CPanel wizard to do your back up
- Download all your web files from FTP (SFTP) via your hosting account.
- Use the Export function to export your MySQL database.
- Alternatively, you can use UpdraftPlus, VaultPress or WP Time Capsule to backup your data.
Step 2: Check and confirm Your Database Login Credentials
After doing your back up, the very first thing is to check and ensure your database login credentials are correct. Remember this lists among the most common among the reasons why the error establishing a database connection signal comes up. Did you migrate to a new hosting provider?
To find the connection details for your WordPress site, check the wp-config.php file which is generally located at the root of your WordPress site. Therefore connect to your site via SFTP and browse to the root of your site of use the CPanel, click on “File Manager,” browse to the root of your site, and right click to edit the file.
It contains four important pieces of information that all need to be correct in order for the connection to successfully take place.
Step 3. Repair a Corrupt Database
As stated above, one of the reasons for the Error Establishing a Database Connection could be a corrupted DB due to the common fact that several hundreds of tables are constantly added/removed by new plugins and themes. Therefore should you try to login to your WordPress site’s dashboard and are receiving the following error, it could means your database is corrupt: “One or more database tables are unavailable. The database may need to be repaired.”
This is the error you will see at the back end, however at the front end, the visitors will see the error establishing a database connection message.
WordPress actually has a database repair mode which you can initiate. Simply add the following to the bottom of your wp-config.php file.
Ensure you remove or comment the above repair code instruction after you are done with repairing database from the wp-config.php file, otherwise you could open a window for anyone to run the repair.
To run repair using the CPanel you can also run a repair from within the MySQL databases screen.
Step 4. Issues With Your Database Server
If you have tried the above and nothing has changed or improved, it is then highly recommended that you check with your hosting provider as it could be an issue with your database server. Such problems could include too many concurrent connections to your database at once, thereby generating the error.
Your hosts and many others might have limits or ceilings on their servers on how many connections are allowed at once. Utilizing a caching plugin can help minimize the database interactions on your site.
Step 5: Restore The Default WordPress Files
As we wrote above, sometimes the core WordPress files can get corrupted for a number of reasons such as corrupt plugins or themes or hacking attempts on your site. You might also have done accidental unprecedented and unexpected file modifications.
Therefore you it is advisable to restore the default, native core files of WordPress. The easiest way to restore them is to just download a clean version of WordPress from WordPress.org and copy it over the one you have right now thereby replacing them at once.
Always download the newest package to your desktop. Unpack. Browse through the unpacked archive and delete the “wp-content” folder.
Next, connect to your site via FTP, go to the main WordPress root, upload your new WordPress files. Overwrite all the previous files. When the upload is completed, go back to your site and refresh it to see if the error clears.
Step 6. Restore Latest Backup
After trying the solutions above this could be your last but not least. If you did back ups in the past regularly you can always resort to your most recent working backup if needed. This, might just be you faster way to resolve things, this is however if you are not bothered or worried about losing any data between when your last backup was taken. Many hosts have their own backup restore process. Remember that you might need to restore both your database and your files.
This is a last resort kind of fix for the Error Establishing a Database Connection, if this doesn’t help, you will have no other choice than coming back to your web host support line, tell them everything that you’ve done and ask for help.
How to prevent or avoid this Error in the future?
Sure enough there isn’t a surefire, 100% fail-proof way to protect your site against any error establishing a database connection, however these below are the two most helpfl ways:
- Get a quality web host. WHen hunting for a home for your website, ensure that you work only with hosts that have optimized their platforms to work with WordPress. That way, you can always count on knowledgeable support and fast help. Not to mention that you will also avoid many common WordPress problems.
- Always BACK UP!!. Ypu can use the Cpanel automated back up solution or resort to plugins such as the simple backup plugins like UpdraftPlus to ensure you always have a working WordPress backup that you can use to restore quickly in case everything else fails.
Before you go: Please subscribe to our website for the latest tips, ideas and recommendations to make your WordPress site wonderful.