Main Website

How to fix ‘Error establishing database connection’ on your WordPress website?

Website errors are a common part of website maintenance. A CMS system like WordPress is a sum of many moving parts. Website owners can run into errors for making seemingly innocuous changes, such as a plugin update.

One of the common errors that website owners face is the ‘error establishing database connection’ message. The cause behind the error is explained in the message itself. When a website faces an issue while connecting to the database, your WordPress website will show this message.

There are many reasons behind an interruption arising in your database connection. To solve this issue, you would naturally need to find the cause and fix it.

Such a website error can be frustrating because of the downtime it causes. Good uptimes also ensure that website-based apps built with powerful mobile app creators like AppMySite remain functional and active.

In this article, we discuss how you can troubleshoot and fix this database connection error. It is advised that you take a backup of your website before trying out the methods discussed below.

#1: Clear your website cache

In some cases, the database connection error comes up if there are too many requests being made to the database. This happens when you’re using a heavy plugin like a page builder. Such a plugin can cause your database to shut down for a few seconds.

Read: How many plugins is too many? Here’s how plugins can slow your website speed

If this is indeed the case, the solution is simple. Simply clear your browser cache and try loading the website after a few minutes. The database should be up and running within a few minutes.

If the website is back, your issue is resolved. In case you have a caching plugin active on your site, make sure you purge all the cache. This is important because your plugin may have cached the error page while your website was down. Purging the cache would make sure new visitors don’t see the cached error screen.

To avoid this error, you should consider using a reliable hosting partner.

Read: How to choose a website hosting partner for your WordPress website?

#2: Check your wp-config file

What happens when you load a website?

When a URL is loaded in a browser, a request is sent to the server to load the information needed. The information is loaded successfully only if the server can access the database. If the credentials used by the server to access the database are incorrect, the website won’t load.

More importantly, your website will show a database connection error.

This is one of the most common reasons behind a connection failure.

To check if this is the case, you would need to see your wp-config file. Log in to your website’s FTP panel. You will find the wp-config file in the WordPress installation folder. When you open the file with notepad, you will see the following information at the beginning:

/** The name of the database for WordPress */

define( ‘DB_NAME’, ‘database_name’ );

/** MySQL database username */

define( ‘DB_USER’, ‘username’ );

/** MySQL database password */

define( ‘DB_PASSWORD’, ‘password’ );

/** MySQL hostname */

define( ‘DB_HOST’, ‘localhost’ );

Inside the quotes, you will find your database name, username, password, and SQL hostname. If this information is incorrect, your website will show a connection error.

To check if this information is correct, login to your hosting panel and check your database credentials. If the credentials match, there’s no problem.

If the credentials don’t match, enter the correct ones. Copy the right credentials from your hosting panel to the wp-config file. You can then upload the file back to the FTP server and try loading the site again. Your website should start working.

It is advised that you make a copy of the wp-config file before making any changes.

#3: Check your database

Another reason behind a connection error can be that your SQL database is down. This happens when there’s too much load on your server due to heavy traffic or excessive use of the website’s resources.

How can you check if your database is down? The easy way of doing this is to ask your hosting service provider. If you’re using a self-managed hosting service, your host may not offer support in this case.

In such a case, you would need to access your website server through an SSH terminal. Putty is an example of the same.

Download Putty on your system and launch it. You will be asked to enter the hostname. Enter your website URL and hit connect. On a black terminal window, you’ll be asked to enter your server username and password.

Once you do this, enter the following command:

sudo netstat -plt

After entering the command, you should see a tabular response. In the local address column, there should be a mention of SQL.

If SQL is not mentioned at all in this column, your database is down. To start it manually, enter the following command:

sudo systemctl start mysql

This command should restart the database. Your website should start loading after you’ve entered this command.

Run the netstat command again to see if SQL shows up in the local address column. This time, it will.

#4: Repair your WordPress database

Repair your WordPress database

WordPress has an in-built database repair mechanism that can be triggered when needed. If the methods covered above don’t work, you can trigger this mechanism and start a database repair.

How does this work?

Open your wp-config. php file with Notepad as discussed earlier. Just before the line ‘That’s all, stop editing! Happy blogging’, add the following code:

define(‘WP_ALLOW_REPAIR’, true);

Next, go to the following URL. Remember to replace it with your own website domain:

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

This URL will open the WordPress Database Repair page. You can select the Repair and Optimize Database option. Once this step is complete, you will be able to access your website again.

Remember to remove the line of code you added to the wp-config file. This is to ensure that other users are not able to access the repair database URL later.

This troubleshooting method is also helpful when you’re investigating errors like the WordPress White Screen of Death.

#5: Check file and directory permissions

Incorrect file permissions may also cause database connection errors. Every file on your server has specific file permissions. On WordPress, there are three levels of permissions for any given file or directory: read, write and execute.

Website hosts generally have their own set of permissions. To set the correct permissions, it is probably a good idea to talk to your website host.

You can do this yourself as well. Learn the file permissions that WordPress recommends and make the changes needed on your WordPress website.

#6: Talk to your website host

If none of the above methods work, you should talk to your website’s hosting partner. If your site is hosted on a managed WordPress host, you should get a quick response.

Managed hosts provide maintenance services for your WordPress website. In case you face downtime, dedicated WordPress hosts can help get your website running in no time. This is part of the services a managed website host offers.

It is also possible that the database is down because of some issues with your host. In this case, there’s little you can do besides waiting for the servers to be functional again. It is extremely rare for website hosts to run into major reliability issues, but it can happen.

If all else fails, talk to your hosting partner. It’s the easiest way to get to the bottom of the issue.

In conclusion

Error establishing database connection is a common WordPress error that can lead to severe downtime. Several complex reasons can cause this error to occur.

In this article, we cover the diagnostic steps you can take to troubleshoot database connection errors. Follow the steps discussed to get your website up and running. The best way to avoid these errors is to manage your site resources well. AppMySite users can benefit from high uptime as it means the app they create on the app maker platform works seamlessly.

Related Articles