The “Error Establishing a Database Connection” is one of the most frustrating errors for any WordPress user. This error happens when your website doesn’t have access to the database. Resolving this error is necessary because it can affect your sales and analytics. Once you get a hang of it, this issue can be easily detected and fixed.
We have a researched and come up with a complete list of solutions to help you fix this error.
Why does this error occur?
When WordPress is unable to establish connection due to varied reasons, you will get this error. The reason can be anything from incorrect database login credentials to unresponsive database server to a corrupted database.
Mostly, any kind of server error causes error establishing a database connection.
Troubleshooting the error
Make sure both front-end and back-end of your site are getting the same error. If you are getting different error like database tables aren’t available, then repair your database.
For this, add this line in your wp-config.php file.
To see this, go to the settings by visiting this page:http://www.yoursite.com/wp-admin/maint/repair.php
The user doesn’t have to be logged-in to access because to repair a corrupted database the users can’t login. Once you have repaired the database, remove this line from your wp-config.php.
If this didn’t solve your problem, then you can find another solution for your trouble.
Check the WP-Config file
The most important in file in WordPress installation is the wp-config.php. Here you will specify all the details for WordPress to connect to your database. Once you change the database user password, you’ll need to change the file too.
To check everything in the wp-config.php file is the same:
DB_Host isn’t always the localhost, this will be different depending on the host.
To fix your problem, replace the localhost with the IP.
This problem occurs when you are running WordPress on a local server.
If everything is correct in the file and you still have the error, then the issue is on the server end.
Check the Web Host
This error occurs when the traffic on your website increases. It just means that the host server is unable to handle the load, making the site slow.
For this, you have to ask your hosting provider whether your MySQL is responsive.
To test MySQL server, you have to:
Test other sites on the server.
If they have same error, then your MySQL server has an issue.
If your sites aren’t on this hosting account, access phpMyAdmin through cPanel and connect to your database. Once connected, you will create a new file testing_connection.php and paste this code:
$link = mysqli_connect('localhost', 'username', 'password', 'database-name');
die('Could not connect: ' . mysqli_error());
echo 'Connected successfully';
Make sure you replace the username as well as the password. You’ll get to know your user has sufficient permission when the script gets connected.
Double-check for any typos in your wp-config file. If you can’t connect to the database then the issue is with your server and can be either due to MySQL server is down or your users don’t have permission.
If nothing else works, try uploading the latest version WordPress to fix your error. This error can be really common and frustrating.