Error Establishing a Database Connection is attributable to various reasons. This might be extremely annoying particularly if it occurred by itself. It generally takes 20-30 minutes to diagnose and fix the issue.
In this post, I’ll show you how to fix the error establishing a database connection in WordPress. This guide is for those that want to learn how to fix WordPress database connection errors.
So why do you have database connection error?
You’re getting this specific error simply because WordPress cannot establish a database connection. The reason why WordPress is not able to establish a database connection is numerous. Perhaps your database login details at wp-config.php are incorrect or have been modified.
Maybe your database server is unresponsive. Perhaps your database has become corrupted. To the best of our knowledge, most of the time this error occurs due to server errors. Here are ways to troubleshoot this issue.
WordPress Database Connection Errors due to the wrong configuration at wp-config.php
Sometimes, WordPress database connection error message is caused by issues in the wp-config file. The wp-config file is where your database details are stored, and these settings are used by WordPress to connect to your MySQL database.
This file can be found in the public_html root directory in your server.
If you did your WordPress installation manually or if you’ve edited this file previously, then you should be familiar with the wp-config.php file. The settings in your wp-config.php might have changed.
Make sure they are correct. If not, please modify wp-config.php.
define( 'DB_NAME', 'database_name_here' );
define( 'DB_USER', 'username_here' );
define( 'DB_PASSWORD', 'password_here' );
define( 'DB_HOST', 'localhost' );
If the issue is resolved, please read my article titled How to Clean up and Optimize a WordPress Database.
If the issue hasn’t been resolved, then add the following code to wp-config.php to repair your WordPress database
Once you have added the code above to your wp-config.php file, go to http://www.yourdomain.com/wp-admin/maint/repair.php. Click on Repair Database.
You should remove the code once the database connection error message has been fixed. In case you still have the database connection issue, the problem is very likely to be on the server-side. Please contact your hosting provider. If you keep getting those errors, you should consider switching to a more reliable hosting company.
How to repair WordPress databases manually
You can also manually repair the WordPress database. We strongly recommend to backup your WordPress database manually before attempting to repair it.
In order to get your database details from cPanel, you should:
- Log in to the cPanel of your web host
- Scroll down and locate the database section
- Click on MySQL Databases
- Scroll down and locate the current databases section. Here you can view your database name and username.
- Once you find the database, click on repair.
Another way you can repair your database is to select repair from the menu.
A Few Last Words
Are you still having WordPress errors? A lot of WordPress hosting providers will be happy to help you fix database errors if you ask for their help.
You might also contact me, I’ll be glad to resolve the issue.