Clicky

Tutorials

How to Fix Error Establishing a Database Connection in WordPress

You must’ve come across the ‘Error Establishing a Database Connection’ few times. This error can be caused due to many reasons. It is a frustrating error, especially for WordPress beginners.

In this article, you’ll learn to fix the Error Establishing a Database Connection issue by going through a compilation of solutions provided by us. Make sure to have sufficient backups before you make changes to the database.

Why do you get Error Establishing a Database Connection in WordPress?

As the name suggests, you’re getting this error because WordPress is unable to establish a database connection. The reason why WordPress is unable to establish a connection could vary. The database credentials might’ve have changed, or might be wrong. Your database might have been corrupted. Or, it could simply be unresponsive.

Let us see how to troubleshoot this problem.

Database repair method to fix Error Establishing a Database Connection

Before going ahead, you’ll have to make sure that you’re getting the same error on front-end and back-end of the site (wp-admin).

If the error is similar on both the ends, you may go ahead read how to troubleshoot it. If the error is different on both the ends, it means that the database needs to be repaired.

You can repair the database by adding the following line in your wp-config.php file.

define('WP_ALLOW_REPAIR', true);

Add this just before ‘That’s all, stop editing! Happy blogging’ line in wp-config.php.

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

Error Establishing a Database Connection in WordPress

When this define is set, the user does not need to be logged in to access this functionality. This is so because the main intent of this functionality is to repair a corrupted database. The user cannot login when the database is corrupt. Hence, once you’re done optimizing and repairing your database, make sure that you remove this from your wp-config.php.

If the above method does not fix your problem, or you’re having trouble to run this functionality then read further to find out other solution that might work.

Try checking the wp-config file to fix Error Establishing a Database Connection in WordPress

As a WordPress user/developer, you should know that WP-Config.php is the single most important file in the entire WordPress installation. This file is where you specify details for WordPress to connect your database. First and foremost thing you should always check is whether everything in your wp-config.php file is the same (in case you changed your root password or the database user password, then you’ll need to change the file as well).

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

Your DB_Host value might differ, it might not always be localhost. It differs with the host. The value is similar to localhost for popular hosts like BlueHost, HostGator, Site5.

For some, the problem might get fixed by replacing the localhost with the IP. It is a common issue when you WordPress is running on a local server environment. This issue is common when you’re running your WordPress on the local server.

Checking you Web Host (MySQL Server) to fix Database connection error in WordPress

The Error establishing database connection is often noticed when your site gets a lot of traffic. The host server is unable to handle so much load, especially when you’re on shared hosting. Your site will get very slow, and some users might get an error. The best thing to do in this case is to get in contact with your hosting provider and ask them if your MySQL server is responsive.

If you want to test whether your MySQL server is running, you can do the following things. Try testing other sites on the same server to see if even they have an issue. If they encounter the same issue, then most definitely, the issue is with your MySQL server.

What if you don’t have any other site to test on the same hosting account?

Well, don’t worry, simply go to your cPanel and try to access phpMyAdmin and connect to the database. If you’re able to connect, we’ll have to verify whether your database user has sufficient permission. To do so, create a ner 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);
?>

Replace the username and password in the above code.

If the connection is successful, then it means that your user has sufficient permission, and the problem is something else. Go back and check your wp-config file to make sure that everything there is correct. Scan for typos.

If you get an access denied error while either connecting to your phpMyAdmin or through restconnection.php results, then you should contact your host. Because sometimes, your user’s permission could get reset due to some unknown reason, and your host will help you restore the permissions, and you’ll be able to get your site back live.

Some other solutions to fix database connection error in WordPress

There are some more solutions to fix database connection error in WordPress which have worked for some people. These may not work for you and use these solutions at your own risk, make sure you have sufficient backups if anything goes wrong.

  • Sometimes, you might get an error saying that the database needs to be repaired. But, even after repairing the database, the error doesn’t go away. The issue, in this case, might be the URL. The URL might change causing the error to persist. You can check whether you have the same problem by running the SQL query in 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.

  • If you’re unable to connect the database with testconnection.php, try changing the wp-config.php user to root user. Your WordPress might start working fine. Revert the settings back to the database user. The issue, in this case, might be a typo.
  • You can also try removing the content of active_plugins in wp_options table and edit the contents of recently_edited. You might fix the problem.

We hope that at least one of the above solutions might have worked for you to fix Error Establishing a Database Connection in WordPress. If you know any other solution, please share with us in the comment section.

 

Share this Story
Load More Related Articles
Load More By Shardul Deshmukh
Load More In Tutorials

Leave a Reply

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

Join Over 50,000+ Subscribers

Interested In
  •   
  •           
  •                      

Search