Fix the Error Establishing a Database Connection in WordPress

Establishing a Database Connection is a very common, fatal error in WordPress 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, which can be caused by many different reasons, making the troubleshooting process a bit of a challenge.

In this article, we are going to review the most common causes of this error and show you how to fix it.

How to Fix the Error Establishing a Database Connection in WordPress article image

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.

Error establishing a database connection notification image

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.


How to Fix Error Establishing a Database Connection WordPress

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

bluehost database section image

And you should see something similar to that:

current mysql users image

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 host 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.

wordpress repair database image

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.

sql database image

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.

Conclusion

In conclusion, fixing the “Error Establishing a Database Connection” in WordPress involves a systematic approach to identify and resolve common causes such as incorrect database credentials, corrupted WordPress files, or server-related issues.

By checking the wp-config.php file, repairing the database, ensuring the MySQL server is running, and consulting your hosting provider when necessary, you can often restore your site quickly. Regular backups and adequate site maintenance can help prevent future errors and ensure your WordPress website runs smoothly. Following these steps will minimize downtime and keep your site accessible to visitors.

Researched and written by:
HowToHosting Editors
HowToHosting.guide provides expertise and insight into the process of creating blogs and websites, finding the right hosting provider, and everything that comes in-between. Read more...

1 Comment

  1. Devid

    I am searching on google how to fix database connection error problem and I find your post. Thanks in advance! hopefully it will work!

    Reply

Leave a Comment

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

This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.
I Agree
At HowToHosting.Guide, we offer transparent web hosting reviews, ensuring independence from external influences. Our evaluations are unbiased as we apply strict and consistent standards to all reviews.
While we may earn affiliate commissions from some of the companies featured, these commissions do not compromise the integrity of our reviews or influence our rankings.
The affiliate earnings contribute to covering account acquisition, testing expenses, maintenance, and development of our website and internal systems.
Trust howtohosting.guide for reliable hosting insights and sincerity.