How to fix the connection error with the WordPress database?  Find out in this article.

If you've been surfing the web for long enough, you must have seen this error a few times. " Error establishing a connection to the database"; it's a erreur quite frustrating especially for novice users. 

In this tutorial, we'll show you how to fix this error on WordPress by compiling a list of solutions.

Note: Before making any changes to the database, make sure that you have necessary backups.

how to fix the connection error with the WordPress database

But before, if you have never installed WordPress discover How to Install a WordPress Blog in 7 Steps et How to search, install and activate a WordPress theme on your blog 

Then back to why we are here. 

What is the cause of this error?

Well, in short you are getting this error because WordPress is unable to establish connection with the database. Now the reason WordPress is unable to establish a connection with the database can vary.

So much to see... 5 WordPress plugins to better manage errors 404

The login details for your database may be wrong or have changed. It could also be that your database server is not responding, or your database may have been corrupted.

In our research, we have found that the majority of times this error occurs due to a server error, but there could be other factors.

Let's take a look at ways to solve this problem.

Does the problem happen as well for / wp-admin /?

The first thing to do is to make sure that the same error occurs on both the public interface or "Front-end" of the website, and on the dashboard or "Back-end" (wp-admin). If the error message "Error establishing a connection with the database" is the same on both pages, so go to the next step.

Discover How to duplicate a WordPress database with phpMyAdmin

If you get a different error on the wp-admin, For example something like “One or more sections of the database are unavailable. The database may need to be repaired ”, then you need to repair your database.

You can do this by adding the following line in your file wp-config.php. Add just before the line « That's all, stop editing! Happy blogging ».

define ('WP_ALLOW_REPAIR', true);

Once you've done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

repair of the database

Remember, the user does not need to be logged in to access this feature when it is set. Indeed, its main objective is to repair a corrupted database; moreover, users often cannot connect when the database is corrupted.

So once you're done repairs et optimizing your database, be sure to remove it from your file wp-config.php.

If this repair does not resolve the problem, or you are having difficulty completing the repair, then continue reading this article; you might find another more effective solution.

Verify the wp-config.php file

wp-config.php is probably the most important file in the WordPress installation. This is where you specify WordPress details in order to connect to the database. If you have changed your root password, or the database user password, then you will need to change them in this file as well.

See also to go further How to manage WordPress files and folders

The first thing you should always check is if nothing has changed in your file wp-config.php.

define ('DB_NAME', 'database-name'); define ('DB_USER', 'database-username'); define ('DB_PASSWORD', 'database-password'); define ('DB_HOST', 'localhost');

Remember that the value of DB_HOST might not always be localhost. Depending on the host, it will be different. For popular web hosts like HostGatorBlueHost, Site5 is localhost

Browse your directory with these FTP clients to make file transfers to your hosting

Some people suggested fixing the problem by replacing localhost with the IP address. It is common to see this kind of problem when WordPress is running on a local server. For example on MAMP, the value DB_HOST when changed to the IP address seems to work.

Go further by discovering How to install WordPress locally on a PC / Windows with XAMPP

Define ('DB_HOST', '127.0.0.1: 8889');

The IP address will vary depending on the web host services.

If everything in the file wp-config.php is correct (be sure to check for typos), then it's fair to say that the problem is at the server level.

Consult your web host (MySQL server)

Sometimes the error establishing a connection with the database occurs when your website is overloaded with an amount of traffic. Basically your server just can't handle the load (especially if you are on a shared hosting). 

Dedicated Server, VPS, Shared or Cloud? What is the best WordPress web hosting solution

Your website will then become very slow, and for some users, this error will occur. So the best thing to do is contact your web hosting provider by phone or livechat, and ask them if your MySQL server is in Response mode.

For users who want to check if the MySQL server is running, there are a few things you can do. Try other websites hosted on the same server to see if they have the same problem. If the same error occurs, then it is clear that there is a problem with the MySQL server of your web host.

How do I allow users to edit certain pages?  Find out in this article

If you don't know of another website hosted on this same server, just go to your control panel and try to access the phpMyAdmin in order to connect to the database. If you can log in, then check if your database user account has sufficient permissions.

Create a new file called testconnection.php and paste the following code inside:

Make sure to replace the username and password. If the connection is successful, it means that your user has sufficient permission, and something is wrong. Go back to your file wp-config to make sure everything is correct (re-analyze for typos).

Read also our article on 6 WordPress plugins to share your articles on social networks

If you can not connect to the database by going to phpMyAdmin then you know the problem is with the server. It doesn't necessarily mean that your MySQL server is down. It could just mean that your user does not have sufficient permissions.

It is therefore important to contact your web host in this case, so that they can correct it.

Solutions that have worked for others

It is important to note that these solutions do not work for you. Using them therefore constitutes a risk. That's why you have to make sure you have backups in case something goes wrong..

Deepak Mittal states that for his client he tried to repair the database. Even after this repair, the error did not go away. He then tried different things, and in the end he found that the problem was with the website URL. Apparently, the latter would have changed, causing the error. He made a request SQL on phpMyAdmin:

UPDATE wp_options SET option_value = 'YOUR_SITE_URL' WHERE option_name = 'siteurl'

Be sure to replace "YOUR_SITE_URL" with the current URL.

This seemed to solve her problem and those of others who commented on her post.

Sachinum claimed he was able to connect to the database with testconnection.php, so it replaced the user in wp-config.php by user root. Since then WordPress has started to work well.

Then he put the settings back as they were and everything was working fine. He couldn't understand what had caused the problem. He concluded that it was due to a typo.

See also How to merge two WordPress databases

All in all, this is a really frustrating mistake. What did you try that seemed to work? We would be happy to share it with the public, so that others don't waste a lot of time finding the same solution as you.

Discover also some premium WordPress plugins  

You can use other WordPress plugins to give a modern appearance and to optimize the handling of your blog or website.

We offer you here some premium WordPress plugins that will help you do that.

1. Contact form 7 Aweber Add-on

Ce WordPress Plugin premium combines the simplicity of WordPress Plugin Contact Form 7 and add to your website a system of creation of Contact form. It integrates the automatic response module of AWeber and can send e-mails directly to your list AWeber. 

Cf7 Aweber Add on

Its main features are: statistics of AWeber in your WordPress dashboard, 8 attractive and cute templates to create your own subscription form, and this in no time, verification of the entire broadcast campaign of your account AWeber in one click, ease of installation, compatibility with the latest versions of jQuery and WordPress, complete documentation and 24/24 support.

Download | Demo | Web hosting

2. Total Recipe Generator

Total Recipe Generator is a premium recipe plugin available on WordPress. It gives you the opportunity to present your recipes to users in a way that will definitely help you stand out from the crowd.Total recipe generator wordpress recipe maker with schema and nutrition facts elementor addon plugin wordpress

Ce WordPress Plugin will allow you to easily transform your website into a website dedicated to cooking recipes. You will be able to:

filter the recipes listed according to the quantity of nutritional elements (sodium, carbohydrates, proteins, etc.), filter the recipes listed according to the preparation time, in a hierarchical and semantic order, the cooking time, the difficulty, type of recipe and all using drop-down values ​​etc…

Download | Demo | Web hosting

3. AccessPress Social Icons Pro

AccessPress Social Icons Pro allows you to create various social icons and link your social profiles from your WordPress website. It is very attractive because you can create, personalize and design beautiful icons for your social media profiles. Accesspress social icons pro wordpress plugin

This premium WordPress plugin offers you 16 beautiful sets of icons that you can choose from. In addition to that, it gives you the ability to create your own set of icons, customize them, set sizes, add multiple effects, etc.

Download | Demo | Web hosting

Recommended Resources

Find out about other recommended resources to help you build and manage your website.

Conclusion

Here is ! That's it for this tutorial, we hope this tutorial helps you to fix WordPress database connection error. If you liked it, feel free to share it with your friends on your favorite social networks

However, you can also consult our resources, if you need more elements to carry out your projects of creation of Internet sites, by consulting our guide on WordPress blog creation.

But, in the meantime, let us know your comments and suggestions in the dedicated section.

...