How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

You might have a WordPress website and need to fix the “Error Establishing a Database Connection“. It offers a ton of benefits that make it worthwhile. However, there comes a time when you encounter the error establishing a database connection. 

Fix the Error Establishing a Database Connection

This fatal error makes the website inaccessible for users. It is also one of the most common WordPress errors

There are various things that impact the WordPress database connection. It can be tough for beginners to solve the problem. This post aims to shed light on how you can fix the issue with minimal effort. 

As long as you follow the instructions mentioned here, you should be able to get your website working again.


What Is the Error Establishing a Database Connection?

Before we cover how to fix the error by establishing a database connection, it is important to know what the error is. 

Every bit of information of your WordPress website including the login information, plugin settings, Meta description, page data, and post data is stored in the MySQL database.

As for the data that does not get stored, it includes media content like themes and images. Whenever someone pays a visit to your site, PHP would execute the code and queries from the database to display everything for the visitor.   

On the other hand, if it does not work properly, one would be left with a message stating that there is an error establishing a database connection. Thus, the entire page would be blank as no data would be retrieved since the connection is not established.  

When this happens, it breaks the front end of the website and also prevents one from accessing their WordPress dashboard. But, users would not see the error immediately. This is due to the fact that the website would serve data from the cache.  


Causes of the Error Establishing a Database Connection in WordPress

Generally, the error is caused by a corrupt database, incorrect database information, and an irresponsive database server. The database makes it easy to retrieve, organize, and store data into the software. 

WordPress is a content management system (CMS). It utilizes the database for storing content and other website data. Then, it connects with the database every time a visitor views the website. The following information is required by WordPress to connect to the database.

  • Database Server
  • Database Password
  • Database Username
  • Database Name

Wp-config.php is the WordPress configuration file where the information is stored. Now, if any of the items are incorrect, it would not be possible for WordPress to connect to the database server. Hence, the error establishing a database connection in WordPress would pop up.

Besides incorrect credentials, the error would also occur when the database files become corrupt or the database server is down

The fact is that there are many moving parts with plugins and themes. Users delete and install these constantly which results in the database becoming corrupted.  

Corrupt files are also a consequence of getting hacked. As for issues with the database server, there are multiple things that can go wrong such as the data experiencing an overload of traffic. 

It is quite common if you have a shared hosting service as it would fail to cater to a surge in users. 


How to Fix the Error Establishing a Database Connection in Each Scenario?

1. Check the WordPress Database Credentials

The first thing that you need to do to fix the database connection error in WordPress is to check the WordPress database credentials. 

Chances are that incorrect database credentials are to blame for the error. If you have moved the WordPress website to a new host, it might be to blame. 

The wp-config.php file is responsible for storing the WordPress database credentials. It contains important settings such as the database information. If it is your first time editing the wp-config.php, you might want to go through this guide to get an idea about what to expect.  

Review the lines below. 

// MySQL settings - You can get this info from your web host //
/ The name of the database for WordPress /
Define ('DB_NAME', 'database name here') ;
/ MySQL database username /
Define ('DB_USER', 'username here') ;
/ MySQL database password /
Define ('DB_PASSWORD', 'password here') ;
/ MySQL hostname /
Define ('DB_HOST', 'local host') ; 

Go through the information to determine if the database host, password, username, and database name are correct. 

Head over to the WordPress hosting account dashboard to confirm the information. You must log in to your hosting account to proceed. Next, click on “MySQL database” from the database section. 

After you click on the database, you will be directed to the database management page of the hosting dashboard. It will showcase your username and database name. Make sure to look below to find the links for changing user passwords and the database users.  

After you confirm the username, password, and database name, you just need to head over to the wp-config.php file to change the information as needed. Then, you have to visit the website again to ensure that the database connection error is gone. 

However, if the error is still there, you need to keep reading to take the right troubleshooting step.


2. Check the Database Host Information

Next, you need to check the database host information. 

If you have gone over the username, password, and database name, you might want to check the database host information to ensure that it is correct. It is common for the majority of WordPress hosting services to use a local host as their database host.

There is still a possibility that the hosting company might use a separate server for hosting the databases. 

In such a case, it is unlikely for the database host information to be referred to as a local host. Therefore, all you need to do is contact the company to find out what the database host information is. 

Once the company informs you about the database host information, you just need to update the database host information to get rid of the error. But, if this step does not bear fruit, you do not need to lose hope as there are other steps that you can also take to resolve the problem.


3. Repair WordPress Database

If the above steps do not solve anything, there is a possibility that your database is corrupt and in need of repairs. This does happen occasionally as hundreds of tables are constantly removed and added by new themes and plugins. 

When you log in to your WordPress website dashboard and see an error, your database is most likely corrupt. The error will read as “One or more database tables are not available. The database might require repair.” 

The error would only appear on the backend, whereas, you would see the same error establishing a database connection on your frontend. The fact is that you can get it fixed easily. In fact, you can fix the error within minutes direct from your dashboard.  

Moreover, it is worth noting that WordPress offers a database repair mode that can be initiated to resolve the problem. You just need to add the following towards the end of the wp-config.php file. The process is straightforward.


WordPress Repair Mode

Define ('WP_ALLOW_REPAIR', true);

Browse to “https://yourdomain.com/wp-admin/maint/repair.php” on the WordPress website to start repair. Then, you would have the option to optimize or repair the database. 

As you are troubleshooting the issue on the website, it is recommended that you opt for the repair database option since it is a lot faster.


WordPress Repair Database

Once you have run the database repair, you need to make sure that you get rid of the line of code that you had included in the wp-config.php file. 

If you fail to do so, anyone would be able to run the repairs. It is also possible to run the repair from the MySQL database screen if running the cPanel.  


CPanel Repair Database

In addition to the above, you can also run the repair from phpMyAdmin directly. To do so, you need to log into your phpMyAdmin account. 

Next, you must click on the database and choose all of the tables. Then, you just have to proceed by clicking on “Repair Table” from the dropdown menu. 


Repair Tables in phpMyAdmin 

Finally, you also have the option to run the repair with WP-CLI using the command mentioned below.

wp DB repair

It is a good idea to head over to WordPress developer resources to learn more about the usage. It should not take a lot of time to optimize the database. You just need a bit of patience. Learn to optimize WordPress for performance with the help of the resources. 

Keep in mind that the user is not required to be logged in when accessing the repair page. However, as suggested, you must not forget to remove the code. 


4. Check to See If the Database Server Is Down

Now, if the abovementioned steps do not fix the error establishing a database connection in WordPress, your database server might be down. 

Whether you have a digital marketing website or any other type of website, you need to determine if the database server is operating or not. 

Typically, the MySQL server could be down due to a surge in traffic on the server. The host server might be struggling to handle the load which would result in your website failing to load. This is especially common if you opt for shared hosting. 

Moreover, you might have noticed that your website is very slow before the error appeared. Some users might have seen the error before others due to cache. To resolve the issue, you need to contact the hosting provider by using live chat or giving them a phone call.

When you speak with the customer support representative, you should simply ask them whether the MySQL server is responsive or not. 

In case you have multiple websites using the same server, you will need to check those websites as well to determine if the MySQL server is actually down.

As for those of you that do not have any other website with the same hosting account, it is best to head over to the hosting dashboard. You just have to access phpMyAdmin to connect to the database. 

If you manage to connect to the database, you would come to know if the database user has permission.

To check the dataset, you will need to create a new file known as testconnection.php and copy the code mentioned below.

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

 The password and username should be replaced to ensure access. Now, you must proceed with uploading the file onto the website. If the script manages to successfully connect, it simply means that the user has permission. It also means that there is some other problem. 

It is important that you go through the wp-config.php file to determine if everything is correct. This is possible by scanning the file for typos.


5. Ask for Help

Finally, if the abovementioned steps do not offer relief, it is best that you ask for help. Start by contacting the web hosting company for support. 

A reputable WordPress hosting company should help troubleshoot the problem for you. They can either fix the issue altogether or point you in the right direction.

If the company does not offer support, you always have the option to hire a WordPress developer to help you out. Codeable is a great platform where you can find a developer who has been highly-vetted for a reasonable rate. Besides, you can trust the developer. 


Conclusion

Once you have gone over this post, you will come to know how to fix the error by establishing a database connection in WordPress website right keyword . Each step should be tried in the order mentioned above to ensure that you resolve the problem in the shortest amount of time possible. 

Leave a Reply

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