Web development

How To Fix WordPress Error Establishing A Database Connection

wordpress error establishing a database connection

How to Fix the Error Establishing a Database Connection in WordPress If you have been surfing the web for a while, at least you have seen this error several times. Failure to establish a database connection is one of those curses that could be caused for many reasons. As a WordPress beginner, this could be terribly frustrating, especially when it happened alone without you changing anything. We encountered this problem yesterday on our own site. It took a little over 20 minutes to detect and solve the problem. While doing the research to find possible causes, we realized that there was not a good article that covered everything. In this article, we will show you how to fix the error when establishing a database connection in WordPress by compiling a list of solutions, all in one place.

Note: Before making changes to the database, make sure you have sufficient backup copies.

Why do you get this error?

In summary, you are receiving this error because WordPress can not establish a database connection. Now the reason why WordPress can not establish a database connection can vary. It may be that the login credentials of your database are wrong or have been modified. It could be that your database server does not respond. It could be that your database has been damaged. In our experience, most of the time this error occurs due to some type of server error, however, there could also be other factors. Let’s see how to solve this problem.

Does the problem occur for / wp-admin / too?

The first thing to do is to make sure you get the same error both on the front-end of the site and on the back-end of the site (wp-admin). If the error message is the same on both pages “Failed to establish a database connection”, proceed to the next step. If you get a different error in the wp manager, for example, something like “One or more tables in the database are not available.” The database may have to be repaired, “then you must 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

wordpress error establishing a database connection

Remember, the user does not need to log in to access this functionality when this definition is established. This is because your main intention is to repair a damaged database, users often can not log in when the database is corrupted. Once you have finished repairing and optimizing your database, be sure to remove this from your wp-config.php.

If this repair did not solve the problem or if you have problems to execute the repair, continue reading this article as you can find another solution to work with.

Checking the WP configuration file:

WP-Config.php is probably the most important file in all your WordPress installation. This is where you specify the details for WordPress to connect your database. If you changed your root user password or the database user password, then you will need to change 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');

Remember that your DB_Host value may not always be localhost. Depending on the host, it will be different. For popular hosts like HostGator, BlueHost, SiteGround, it’s localhost. You can find other host values here.

Some people suggested that they corrected their problem by replacing localhost with the IP. It is common to see this type of problem when running WordPress in a local server environment. For example, in MAMP, the DB_Host value when changing to the IP may seem to work.

define('DB_HOST', '');

IP will vary for online web hosting services.

If everything in this file is correct (be sure to check the spelling errors), then it is fair to say that there is something wrong at the end of the server.

Check your web server (MySQL server)

You will often notice this error when establishing a database connection when your site is filled with a lot of traffic. Basically, your host server simply can not handle the load (especially when it’s in a shared hosting). Your site will be very slow and for some users will even generate the error. So, the best thing to do is to get in touch by phone or live with your hosting provider and ask if your MySQL server is responsive.

For those users who want to test if the MySQL server is running yourself, you can do some things. Try other sites on the same server to see if they have the problem. If you also receive the same error, there is definitely something wrong with your MySQL server. If you do not have any other site in this same hosting account, simply go to your cPanel and try to access phpMyAdmin and connect the database. If you can connect, then we must verify if your database user has sufficient permission. Create a new file called testconnection.php and paste the following code into it:

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

Be sure to replace the username and password. If the script successfully connected, 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 everything is correct (re-analyze the typographical errors).

If you can not connect to the database by accessing phpMyAdmin, then you will know that it is something with your server. It does not necessarily mean that your MySQL server is down. It could mean that your user does not have enough permission.

In our case, our MySQL server was running. All the other sites on the servers worked fine except WPBeginner. When we try to go to our phpMyAdmin, we end up receiving the error:

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

We spoke on the phone with HostGator and their support quickly found the problem. Somehow, the permissions of our users were restored. I’m not sure how that happened, but apparently that was the reason. They returned and restored the permits and we were able to recover the site.

Therefore, if you get the access denied error when connecting to your phpMyAdmin or through the results of testconnection.php, you should contact your host immediately to have it fixed.

Solutions that worked for others:

It is important to keep in mind that these may not work for you. Use at your own risk and make sure you have enough backup copies if something goes wrong.

Deepak Mittal said that his client was receiving the error that the database needs to be repaired. Even after repairing the database, the error did not disappear. He tried several things and in the end, the problem was the URL of the site. Apparently that was changed which caused the error to persist. Executed the SQL query by going to phpMyAdmin:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Be sure to replace YOUR_SITE_URL with the real url example: https://www.wpbeginner.com. The wp_options will be different if you have changed the default prefix of the WordPress database.

This seemed to solve the problem for him and for some others who also commented on its publication.

Sachinum suggested that he could connect the database with testconnection.php, so he changed the wp-config.php user to the root user. WordPress started to work perfectly well. Then it reverted the configuration to the user of the database, and it continued working. He could not figure out what the problem was, but he came to the conclusion that it was a typographical error.

Cutewonders suggested that they remove the active_plugins content in the wp_options table and edit the content of recent_edited. Basically, that seemed to solve the problem. Please, your complete answer here.

We read in numerous sources that users simply loaded a new copy of WordPress and corrected the error.

This is a really frustrating mistake. What have you tried that seemed to work for you? We will be happy to expand this resource, so that others do not have to waste so much time looking for a solution.

Review Date
Reviewed Item
How To Fix WordPress Error
Author Rating