Are you tired of the “Error Establishing a Database Connection in WordPress”? It is one of the fatal errors that makes your website inaccessible for everyone. 

If this problem occurs it’s a bit troublesome for beginners to troubleshoot the problem. If you do anything wrong, your whole database can be lost and your website as well. So, what’s the solution?

Well, in this post I have come with the solution. I will show you how to fix error Establishing a Database Connection in WordPress. All you have to do is stick to the post till the very end. Then you will be able to solve this problem on your own. 

Let’s begin then. 

What Can Cause An Error Establishing A Database Connection In WordPress?

This wide popular problem can happen due to incorrect data in your settings, irresponsive server, corrupt database, or any other reason. 

A database is a place or software platform that stores data and allows to retrieve data in time of need. 

WordPress uses the database to store its data and all of the content that the user saves. It connects to database every time a user visits your website. 

WordPress uses following data for connecting to the database:

  1. Database name
  2. Database username
  3. Database password
  4. Database server

All these informations are stored in your WordPress configuration file named wp-config.php. If any of these informations are incorrect WordPress will fail to connect to your database server and the error message ‘Error establishing a database connection’ will be shown on your display. 

How To Fix The Error Establishing A Database Connection In WordPress

The first thing you should do is 

1. Check Your WordPress Database Credentials

Incorrect WordPress database credential is the most common reason for database connection error. All your database credentials are store in the wp-config.php file. In this file, all the important configuration is saved including database information. 

When you see the error open the wp-config.php file and look for the following lines

<!-- wp:paragraph -->
<p>// ** MySQL settings - You can get this info from your web host ** //</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>/** The name of the database for WordPress */</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>define( 'DB_NAME', 'database_name_here' );</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>/** MySQL database username */</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>define( 'DB_USER', 'username_here' );</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>/** MySQL database password */</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>define( 'DB_PASSWORD', 'password_here' );</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>/** MySQL hostname */</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>define( 'DB_HOST', 'localhost' );</p>
<!-- /wp:paragraph -->

Then make sure the database name, username, password, and database host is correct. You can confirm this from your hosting dashboard. 

Go to the hosting dashboard and click on MySQL Databases. It will take you to the database management page, from there you will find out the database name and the username. 

error establishing a database connection

Below that find out your database user and links to change their password. 

When you have confirmed the database name, username, and password you can change the required information in the wp-config.php file if needed. After that check, your website and you will see the error is solved. 

2. Check For Your Database Host Information

If you are sure that your database name, username, and password are correct then you may review the database host information. 

Many hosting companies use localhost as database hosts. However, some of the companies may use separate servers to host databases. Therefore you need to contact your hosting company and confirm your database host information. 

3. Repair Your WordPress Database

If you are getting a different error like, ‘one or more database tables are unavailable’, then your database may need to be repaired. 

You can do it by adding a simple line in your wp-config.php file. Open the file and add the below line. That’s all. 

define('WP_ALLOW_REPAIR', true);

After you add the line you will be able to see the settings page by visiting this link.:

http://www.yoursite.com/wp-admin/maint/repair.php

Once you are done repairing, remove this code and save the wp-config.php file. 

4. Check If The Database Server Is Down

If everything seems legit then your database server (MySQL server) may be down. This can happen due to heavy traffic on your server, especially when you are using shared hosting. 

For this reason, your site will get slow and some users may get errors. So the best thing is, you give a phone call or knock at the support of your hosting provider and ask if your MySQL server is responsive. Then they will fix it for you. 

Last Words

The most common reason for the problem “Error establishing a Database Connection in WordPress” is mentioned above. There might be other reasons too, but those are very rare. First try to apply the above solutions, if the problem is not fixed then call a developer to fix the problem. 

If you liked the post please share it with your friends and don’t forget to leave behind your thoughts in the comments box below. 

Leave a Reply

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