How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

When you start using WordPress, you’re bound to encounter a number of errors.It’s inevitable; it can be your fault, at times systems break, someone fails at their job, natural calamities strike – anything could cause an error.

Why do you get this error?

error
you are getting this error because WordPress is unable to establish a database connection.

  • It could be that your database login credentials are wrong or have been changed.
  • If database server is unresponsive.
  •  Your database might be corrupted.

In our experience, majority of the times this error happens because of some sort of server error however there could be other factors as well.

Troubleshooting “Error Establishing Database Connection.”

Does the problem occur for /wp-admin/ as well?

First thing you should do is to make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin). If the error message is the same on both pages “Error establishing a database connection”, then proceed onto the next step. If you are getting a different error on the wp-admin for instance. The database may need to be repaired.

repair database

 The user does not need to be logged in to access this functionality when this define is set. This is because its main intent is to repair a corrupted database, Users can often not login when the database is corrupt. So once you are done repairing and optimizing your database, make sure to remove this from your wp-config.php.

Checking the WP-Config file

WP-Config.php is probably the single most important file in your entire WordPress installation.  If you changed your root password or user password, then you will need to change this file as well.Check is if everything in your wp-config.php file is the same.
wp php

wp-config.php file is where you specify the details for WordPress to connect your database.

Remember your DB_Host value might not always be localhost. Depending on the host, it will be different For popular hosts like HostGator,BlueHost,Site5,It is localhost.

1.define(‘DB_NAME’, ‘database-name’);
2.define(‘DB_USER’, ‘database-username’);
3.define(‘DB_PASSWORD’, ‘database-password’);
4.define(‘DB_HOST’, ‘localhost’);

Check your Web Host (MySQL Server)

Error establishing database connection when your site gets swarmed with a lot of traffic. Basically, your host server just cannot handle the load (specially when you are on shared hosting). Your site will get really slow and for some users even output the error. So the best thing you should do is get on the phone or live chat with your hosting provider and ask them if your MySQL server is responsive.

For those users who want to test if MySQL server is running yourself, you can do a few things. Test other sites on the same server to see if they are having the issue and they are also getting the same error, then most definitely there is something wrong with your MySQL server.

sql
If you do not have any other site on this same hosting account simply go to your cPanel and try to access phpMyAdmin and connect the database.

you can connect, then we need to verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code in it:

<?php
$link = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>

Make sure to replace the username and password. If the connected successfully, then it means that your user has sufficient permission, and there is something else that is wrong. Go back to your wp-config file to make sure that everything there is correct

So if you get the access denied error in either connecting to your phpMyAdmin or through testconnection.php results, then you should contact your host right away to get them to fix it.

Solutions that Worked for Others

 

It is important to note, that these may not work for you. Use at your own risk and make sure that you have sufficient backups if anything goes wrong.

Client was getting the error that database needs to be repaired. Even after repairing the database, the error did not go away.then tried various things and at the end, the issue was the site url. Apparently that was changed which caused the error to persist.that had ran the SQL query by going to phpMyAdmin:

UPDATE wp_options SET option_value=’YOUR_SITE_URL‘ WHERE option_name=’siteurl’

Make sure to replace YOUR_SITE_URL with the actual url example: 

http://http://digitalkalyan.com

Conclusion

 

At the end of the day, you should not sweat when you encounter this error. As we’ve outline here today, it’s really simple to resolve, and should you run into any problem, your web host is always at your beck and call.

We hope this article helped you learn how to fix the “Error Establishing a Database Connection” error in WordPress.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.