Why am I Getting Error Establishing Database Connection on my WordPress Site?

Error Establishing Database Connection

Last updated - April 28, 2022

When you have a WordPress site, continuous uptime is an essential aspect for user experience and search engine ranking. So it is important to understand some of the common errors you might be facing from time to time and how to troubleshoot them. One such error that you may often encounter is ‘Error Establishing Database Connection’. In this article, we will discuss some of the reasons for your WordPress site getting this error. We will also discuss a few different ways to help you troubleshoot this issue.

What is Error Establishing Database Connection?

Error Establishing Database Connection is a common error you will see on WordPress sites and can be managed effectively. The data of your WordPress site such as posts, pages, meta information, plugin settings, etc are stored in a MySQL database. When a user tries to access your site, PHP commands are passed on to the database to retrieve specific information. When there is a break in this communication channel, that is PHP commands are not able to communicate with MySQL, you will get this error.

Why am I Getting Error Establishing Database Connection?

Now, you might be getting this error for a number of reasons. Let’s quickly explore some of the reasons for getting ‘error establishing database connection’ on your WordPress site.

Wrong login credentials

The login credentials could have changed for your site. It can happen when you change your hosting environment or when you make certain updates on your themes and plugins. And, due to this, the access to database is prevented. You need to verify the login credentials to ensure that it is correct.

Database is corrupted

There are chances that your database has become corrupted for a variety of reasons. It could be due to a breach by hacking, plugin or theme issue, missing table, etc.

Problems with core WordPress files

Sometimes a WordPress update can lead to problems in the core WordPress files. Plugin or theme updates can also create such problems.

Issues with database server

Your database can be on a server different than that of your web server. In an event where there is an issue with this database server, there will be an error establishing database connection. If you are using shared hosting, this could be a possibility for getting the error.

Traffic spikes

Sometimes unexpected traffic can strain the database and it can go into an unresponsive mode.

How to fix error establishing a database connection?

As this is a common error often seen on several WordPress site, we will discuss some of the expert suggested ways to resolve it quickly.

Restart the database

One of the first things you can try when you encounter this error would be to restart the services. You can restart the database first and then restart Apache. The Unix commands used on Ubuntu can be used for this purpose. Please note the commands will vary depending on the database you are using on your WordPress server. This is a useful strategy to try out, as it can get the database up and running if it was down for some reason.

Now, let us see a few examples of commands you can use.

You can restart the MySQL service on your Ubuntu server using the following command:

sudo service mysql restart

In the above example, mysql is the name of your MySQL service. If it is named differently, you need to change the command accordingly. Alternatively, you can try the following code as well: 

sudo /etc/init.d/mysql restart

To restart Apache2, use the following code:

sudo service apache2 restart

To restart Nginx, here is an example command:

sudo service nginx restart

Confirm if you are using the correct database login credentials

There is a possibility that the login credentials of your database can change when ou change the hosting service or make certain changes to files. When you are facing with this error, one of the first things you can verify is that the database login credentials are correct.  You need to verify four aspects here:

  • MySQL Database server name
  • Database name
  • MySQL Database password
  • MySQL host name

You need to ensure that the values on the “wp-config.php” file is same as that on your server. For this, you can use SFTP or cPanel. If that is too much for you, you can always get the help from your hosting service’s support team.

Getting in touch with your hosting service provider

As discussed above, this problem could be due to a problem with your server. To have a clear understanding on this, you will have to get in touch with your hosting service provider. Basically you have to understand if the database server is working fine, and there has no problems related to traffic spikes and such. Most of the quality hosting providers offer quality support through live chat, email or phone. It is important that you get their attention quickly to ensure there is no problem with the server.

Screenshot of Kinsta, WordPress Shared hosting servce
Kinsta offers great hosting options for websites of all sizes.

Here is a list of quality web hosting services you can choose from for your WordPress site.

The major advantage of getting in touch with your hosting service provider is that you will know if there is any problem related to the server or traffic. If there is any problem from their end, you can rely on them to get it fixed as soon as possible. Now, if they check and confirm that they could not detect any problems with the database server, and everything is working as they should be, then you need to look for other solutions.

To reduce the issues related to database server, you can use a caching plugin, which will help in reducing database interactions.

Repair the corrupt database

You will see the “error establishing database connection” on your site’s frontend. To see if the database is corrupted, you need to go to the backend and check. When you try to access the site from the backend, you will get a message similar to “one or more database tables are unavailable. The database may need to be repaired.” If this is the case, you can run a code to repair the corrupted database.

You need to add the following code to your “wpconfig.php” file, and run the repair mode:

define('WP_ALLOW_REPAIR', true);

By running this code, you can repair the corrupted database. Once the repair is done, you will have to remove the code from the “wpconfig.php” file.

Fix the corrupt WordPress files

Now, if your database is not corrupted, there is a chance that your WordPress files are corrupted. This could be due to a hacking attempt, errors in file transfer, or problems with your hosting service. In this scenario, you will have to fix the corrupt files to solve the error.

One of the expert suggestions is to replace the WordPress core file without making any changes to themes or plugins. You need to download a fresh version of the WordPress zip file from WordPress.org. Extract the zip file and delete the wp-content folder, and wp-config-sample.php file. Upload the remaining files to your site using SFTP. This process will replace the corrupt files. You will be able to resolve error establishing database connection in this manner if the problem was with corrupt WordPress files.

Backup restoration

One of the last resorts to resolve error establishing database connection would be to restore a backup. You can try this option if none of the other strategies discussed above have worked. Make sure you restore backups of both your WordPress files and database. For restoration, you can go to your backup files and point to a timeline where you want to go back to.

Here is an article to help you backup and restore your WordPress files and database.

How to stop error establishing database connection from recurring?

By paying attention to certain aspects of site management, you will be able to prevent this error from repeating. One of the main aspect to note is to opt for a quality hosting service. You need to estimate your traffic scenario while choosing a hosting plan. Also, make sure that the hosting service that you choose offers ample options for scaling, and reliable support. Having a good hosting service will definitely help in reducing the occurrence of error establishing database connection.

Use Load Balancer

When your server capacity is limited, increase in traffic can really stress out your site. The WordPress instance serving the site won’t be able to handle the number of users. The result will be your website going extremely slow or being completely down. You can avoid this scenario by using a load balancer with multiple WordPress instances. This will help you balance the load on the site and more effectively maintain your site by reducing delays and downtime.

Representational image of using load balancer

Another aspect you need to be mindful about is investing in a good backup solution for your site. This will help to periodically save your WordPress files and database. In the event of a problem with your database or files, you can restore your site to an earlier version of itself this way.

Further reading


Please enter your comment!
Please enter your name here