Error establishing a database connection… when everything looks good!

You have your WordPress installed. Everything is functional. One fine morning, your site displays this error.

In a usual situation, you should check if the wp-config.php file is correctly filled in. Then, you test to access your database, everything seems normal. Finally, you quickly create a small script that tries to connect to your database with the same parameters as your WordPress installation… and it works.

The cause

As said before, usually this error comes from the wp-config file which has a configuration error. It should therefore be checked first. Then, you should check that it is functional (by trying to access it) and contact your host if it is not the case, then if it is active that there is no hacking of your database (by looking at its size, by observing the tables, by comparing it to a backup…).

If you have validated all these steps, there remains one cause that you can possibly identify by comparing your database with a recent backup: a data corruption in your database

The error processed in this article can be triggered when the wp-options table is affected by a problem.

The solution

The wp-options table contains general settings for your website, parameters for your plugins, themes… and some temporary data.
Among these parameters, there is the URL of your site, and that’s what we’re going to be interested in.

  • Connecting to your database
  • If necessary, temporarily, you can upload a phpMyAdmin via (S)FTP to your site https://www.phpmyadmin.net/downloads/.
  • Go to the wp-options table
  • See if you can identify if the siteurl parameter is present and filled with the URL of your site.
  • If not, execute the following query:
    INSERT INTO `wp_options` VALUES (1.0, 'siteurl', 'https://example.com/', 'yes')
    where https://example.com is to be replaced by the URL of your site

If your site is working again, you will first need to make a full backup of your site (especially the database), but you will need to ask yourself how this problem could have occurred. It may be necessary to perform an audit of your website in order to identify any weaknesses or vulnerabilities that may have been exploited.

Sometimes there may be several errors in your database. In addition, you can add the following line to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

then access the following URL: https://www.exemple.com/wp-admin/maint/repair.php where https://exemple.com is to be replaced by the URL of your site.
This will start the optimization and correction of your database.

Leave a Comment

Your email address will not be published.

You may also like