Problem: How To Fix Error Establishing A Database Connection In WP

Problem: How To Fix Error Establishing A Database Connection In WordPress

Error Establishing A Database Connection is the common error you will see once in a while. It is caused because of numerous reasons. We will explore all the possibilities in this article.

In simple terminology, this error may be caused because of the problem with the database connection. Following are the possibilities, later in this article you will learn how to fix these issues.

1. Database Crashed/Unresponsive
2. One or more database tables are Missing/Unavailable/Corrupt
3. Database Login/Permissions Error

1. Database Crashed/Unresponsive

Error Establishing A Database Connection is caused if Database Crashed/Unresponsive. If any of these happens you will get this error.

How you will know one of these caused the issue?

Check the other websites on that server or hosting account. If those websites are working correctly, then your Database is not Crashed/Unresponsive. Proceed to next step.

Why Your Database Crashed/Unresponsive?

The common reason is if you are on VPS server and you have limited resources, your website is using resources more than that. Like getting a hike in the traffic, the database won’t be able to handle that much traffic and eventually, it will crash and become unresponsive.

Solution to Database Crashed/Unresponsive

Restart your database server if you have access to the server or ask your service provider.

After that, you are good to go. If you don’t want to face this problem in future upgrade your server with good resources like increasing ram and processor or moving your website to good hosting provider (I recommend you to go with BlueHost, if you are on shared hosting)

If you are getting this problem quite often, then there is a problem with server optimization settings. If you are using cPanel on your server, raise a support ticket on their support website.

My Experience: As of this writing, I got this same problem, I’m moving few large client accounts from my dedicated cPanel/WHM server to different cPanel/WHM server. After successfully moving few accounts, I’m getting “Error Establishing A Database Connection In My WordPress Website”. I checked other website’s on this server, other website’s also showing the same message. So I know the database is Crashed/Unresponsive. Just rebooted my server, after a couple of minutes everything is fine and my website is back.

This can happen for any reason even if you are in shared hosting like your hosting provider is doing some critical operations on the server or moving some client accounts. It can be any reason, check the other websites on that server. If other websites are not working, raise a support ticket with your hosting provider. The better option is, move to a good hosting provider (I recommend BlueHost).

2. One or more database tables are Missing/Unavailable/Corrupt

You can check this error is causing with “One or more database tables are Missing/Unavailable/Corrupt” by accessing front end of the WordPress website, you should see this error “Error Establishing A Database Connection”.

If you access WordPress Admin area, you should see this message “One or more database tables are unavailable. The database may need to be repaired”.

If you see this message in the backend, that means few tables are corrupt or missing. Proceed with these instructions to fix this error.

Add this line in the wp-config.php file after ‘That’s all, stop editing! Happy blogging’

After adding the above line access this URL on your website

You should see a message like this.

You can go with any option like “Repair Database” or “Repair and Optimize Database”. While performing Repair Database, it checks for all the core database tables and if any of the tables are missing those tables are restored with default schema.

And one thing to remember for performing this task, you don’t need WordPress admin login credentials because it’s maintenance task. If you add this above and forget it, anyone can access this page. After completing this task, remove the “WP_ALLOW_REPAIR” from wp-config.php file.

After performing this above maintenance task, you should see your website. If you still not able to get the website back, Proceed to next step.

3. Database Login/Permissions Error

You will get this Error “Establishing A Database Connection”, even if you have a problem with database login credentials or permission problem. With this problem, you will see the same error in both frontend of the WordPress website and backend of the WordPress website.

You can fix this problem by checking the login credentials in wp-config.php file. If you see any problem in the credentials update those values and check your website, you should be able to access WordPress frontend website and backend website.

If you still have the problem, update database host with IP address of the server instead of localhost. In some cases, this can solve the problem.

If your credentials are correct and still getting the error, then proceed to next step that is checking the connection with a new PHP file.

If the above solution has not worked for you create a new file in your WordPress root directory or anywhere in your server, you should be able to access this file from a web browser.

Create a file with the name of dbtest.php and use your login credentials. Use your correct login credentials, that is same as the above.

Read more about connecting to database here.

If you see an error message like “#1045 – Access denied for user ‘myuser’@’%’ (using password: YES)”, then there is some problem with login credentials on your server. Check with your hosting/server provider.

If you are able to see “Database Connection Successful” message, check the login credentials carefully on wp-config.php file. There might be some typo mistakes or some sort of errors.

What if Everything Failes, Can’t figure out issue

What if you followed all the steps and can’t figure out the issue.

You followed step 1 and you came to know that you Database is not Crashed/Unresponsive.

You followed step 2 and you came to know that this is not your problem “One or more database tables are Missing/Unavailable/Corrupt”.

You followed step 3 and you came to know that there is no problem with Database Login Credentials or Permissions.

If you get no for all the above 3 steps, Then the issue is there might be some changes/mistakes in the database, do a complete restore of your website. That is the only option you have.

Just restore your website from one of your old backups. That’s the only final step you can do to get your website back online.

Before restore, be sure to take the backup of your current files and database. Otherwise, you will lose all the updates you made at that time.

So, that’s why taking backups of your website regularly is very important. Check out the Free WordPress Backup Plugins.

Always have the latest backup of your website in hand. This will save you from a lot of headaches.

Conclusion To Fix Error Establishing A Database Connection In WordPress

I hope this article helped you to solve the issue, let me know through the comment form. How you solved your problem.

If you have any problem, let me know through the comment form below.

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: