If you have a website with WordPress CMS, then you may have faced the Database error problem. Error Establishing a Database Connection can occur for many reasons, it may be the hosting error or it may be an error for accidental coding change or bad plugin. And if you are a beginner, this could be awfully frustrating especially when it happened on its own without you changing anything. So, here we will share you a perfect solution for the Error Establishing a Database Connection in WordPress.

Error Establishing a Database Connection

During the database connection error, your site will be inaccessible, so it’s a very bad thing and you just need to fix it as soon as possible. And keep in mind that before you make any database changes, make sure you have sufficient backups.

Why do you get this error?

Normally this error happens because WordPress is unable to establish a database connection. Now the reason why WordPress is unable to establish a database connection can differ. It could be that your database login credentials are wrong or have been changed. Or it could be that your database server is unresponsive. It could be that your database has been corrupted. In our experience, the bulk of the times this error happens because of some sort of server error however there could be other factors as well. So, let’s take a look at how to go about troubleshooting this problem.

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

The first thing you should do is to make sure that you are receiving 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 receiving a different error on the wp-admin, for example, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following line in your wp-config.php file. Add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

define(‘WP_ALLOW_REPAIR’, true);

Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

wp repair db

Keep in mind that, 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 log in 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.

If this repair did not fix the problem, or you are having trouble running the repair then proceed reading this article as you might find another solution to work.

Checking the WP-Config file

WP-Config.php is apparently the single most important file in your entire WordPress installation. This is where you define the details for WordPress to connect your database. If you changed your root password or the database user password, then you will need to modify this file as well. The first thing you should always check is if everything in your wp-config.php file is the same.

define(‘DB_NAME’, ‘database-name’);
define(‘DB_USER’, ‘database-username’);
define(‘DB_PASSWORD’, ‘database-password’);
define(‘DB_HOST’, ‘localhost’);

Some communities suggested that they fixed their problem by replacing localhost with the IP. It is obvious to see this sort of issue when running WordPress on a local server environment. For example on MAMP, the DB_Host value when changed to the IP may seem to work.

define(‘DB_HOST’, ‘’);

IP’s will vary for online web hosting services.

If everything in this file is correct (make sure you check for typos), then it is fair to say that there is something corrupt on the server end.

Check your Web Host (MySQL Server)

Usually, you will notice this Error establishing database connection when your site gets swarmed with a lot of traffic. Basically, your host server just cannot handle the load (especially 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 need 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. If they are also getting the same error, then most definitely there is something wrong with your MySQL server. Also, 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. If 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:

$link = mysqli_connect(‘localhost’, ‘username’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysqli_error());
echo ‘Connected successfully’;

Make sure to substitute the username and password. If the script connected successfully, then it means that your user has enough permission, and there is something else that is wrong. Go back to your wp-config file to make sure that everything is right (re-scan for typos).

If you cannot connect to the database by going to phpMyAdmin, then you understand it is something with your server. It does not necessarily mean that your MySQL server is down. It could indicate that your user does not have sufficient permission.

You can also try – 5 Best WordPress LMS Plugins Compared (Pros and Cons)

#1045 – Access denied for user ‘foo’@’%’ (using password: YES)

If somehow your user’s permissions were reset, then you need to contact your hosting providers because if you are using shared hosting the permission can only change by the providers. But if you using VPS or dedicated then you can change it on your own.

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.

Few more other solutions for Error Establishing a Database Connection

It is necessary 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.

Sometimes this error can occur due to wrong URL setup. Obviously, that was changed which caused the error to persist. He 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: https://i1null.com. The wp_options will be different if you have changed the default WordPress database prefix.

This seemed to fix the issue for him and few others that commented on his post as well.

And if you are able to connect the database with testconnection.php, then you need to change the wp-config.php user to the root user. WordPress started to work perfectly fine. You need to revert the settings back to the database-user, and it continued to work. He could not figure out what was wrong, but concluded that it was a typo.