How to Fix the Error Establishing a Database Connection in WordPress

Establishing a Database Connection is a very common error in WordPress. That is a fatal error that prevents your website from running and makes it inaccessible to all of your visitors. This error appears, when WordPress is not able to make the connection to your database and what is bad about it is that this can be caused by many different reasons, that make the troubleshooting process a bit of a challenge.
However, in this article, we are going to review the most common causes of this error and show you how to fix it.


Also, Read Best Fixes for “ECONNREFUSED – Connection refused by server” Error in FileZilla


Causes Of Error Establishing a Database Connection in WordPress

The error “Error establishing a database connection” can be caused by incorrect information in your WordPress, broken database, or problems regarding your database server.

The “database” is where all of the information regarding your website is stored. As a CMS (content management system), WordPress stores all of its content into a database, and each time a visitor wants to visit your website the WordPress makes a connection to your database, so it can retrieve and serve the proper information. WordPress needs to be configured correctly to work as expected, so you should use the proper database name, username, password, and server. This information is stored in a file that can be found in your WordPress directory. The file is called “wp-config.php” and it stores that exact information.


Error Establishing a Database Connection WordPress Fix

One of the most common reasons that you will see the error establishing a database connection error in WordPress is when something in your wp-config.php file is wrong. This happens most often when you transfer your website to a new host or if you just misconfigured something.
Open the wp-config.php file, and make sure that everything is set correctly and there are no mistakes. The file should look like this:

// ** MySQL settings – You can get this info from your web host ** //
/** Database name for WordPress */
define( ‘DB_NAME’, ‘database_name_here‘ );
/** Database username */
define( ‘DB_USER’, ‘username_here‘ );
/** Database password */
define( ‘DB_PASSWORD’, ‘password_here‘ );
/** MySQL hostname */
define( ‘DB_HOST’, ‘localhost‘ );
If you are not 100% sure about your hosting details, go to your hosting account, click on Databases, then MySQL Databases

And you should see something similar to that:

Check for some spelling mistakes or any errors regarding your username, password, or database name and make sure that you are using the database that has the WordPress installed on.
If this does not fix your issue, you should proceed to the next step:


Database and Host Information Check

If you are 100% sure that your username, password, and database name are set correctly, then you should make sure that your ost information regarding the database is correct.

Most of the hosting companies use the “localhost” as their database host, but some hosting companies that offer managed WordPress hosting usually use separate servers, to host the databases, and in that case, it should not be set to “localhost“. The best way to ensure the right information is to contact your hosting company and ask them to confirm your information.


WordPress Database Repair

If you are getting a different error, that can look something like “database tables are unavailable“, you might want to try to repair your database. To do that, open the wp-config.php file with any text editor and add the following line of code:
define(‘WP_ALLOW_REPAIR’, true);
Once you do that, go to yoursite.com/wp-admin/maint/repair.php and follow the repair guide. After you complete the process, remove the line from your wp-config file and try to open your website. It should be running smoothly now.


The Server Might Be Down

If you still experience the error, it might be caused by a database crash, that can occur because of high traffic on the server or hosting-related problems.
This is known to happen mostly on shared hosting servers, where one or more of the websites that you share your server is gettings lots of traffic or is infected or spammed. In this situation, the best thing that you can do is to get in touch with your hosting provider and ask them for further help.


Other Solutions

Another solution that you should try if the problem persists is to update the URL address of your website, straight from your database.

To do that, go to your database, click on the SQL menu and type the following code:
UPDATE wp_options SET option_value=’Your website URL’ WHERE option_name=’OLD URL‘. After this change users of local servers, VPS, or dedicated servers should perform a restart on their servers and try again.
Yet if any of these solutions does not work for you, the best thing that you can do is to contact your hosting provider for further investigation and help.


Also, Read How To Install WordPress on Nginx CentOS 7


Leave a Comment

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

Time limit is exhausted. Please reload CAPTCHA.