How to fix the error connecting to a database in WordPress

One of the most common and dreaded errors that WordPress users encounter is the “Error establishing a database connection

It means that Your website can no longer communicate with your WordPress databasecausing your entire site to go down.

There is no reason to ignore this and you must fix it as soon as possible since it could affect your sales, traffic and reputation of your business.

In this article, I will discuss some of the most common causes of this error and the easiest ways to fix it to speak.

Let’s get started!

What is the error establishing a database connection?

Your WordPress site stores all data including post data, page data, meta information, plugin settings, login information, etc. in your MySQL database.

As soon as someone visits your website, PHP executes the code on the page and queries the data in the database. The information is then displayed in the browser from the database.

If for some reason this doesn’t work properly, you get the shown below Message “Error establishing a database connection “.

Because the connection isn’t working properly, it can’t get data to render the page, so the entire page is blank.

The front end of your website will then become inoperable and you will not be able to access the WordPress dashboard. Problematic, isn’t it?!

What can cause this connection error?

Wrong login data

WordPress must use a specific login and password to access the database. If they change for any reason, WordPress can’t retrieve anything from the database.

Corrupt WordPress files

Among other reasons, WordPress files can get corrupted due to a failed update. This can include everything from plugins to themes and even the WordPress core software update.

Corrupt database

It could be caused by a faulty plugin or a hacker messing with the database internally.

Problems with your database server

Many things can go wrong on the web host side, e.g. B. that the database is overloaded or not responding due to too many simultaneous connections.

With a shared host, this problem can be quite common as the same resources are used by a number of users on the same server. I recommend monitoring your website uptime and performance to see how often this is happening.

How to fix the error establishing a database connection

Before you start troubleshooting, we always advise you to to create a backup.

Many of the recommendations below involve manipulation of the data in your database, so you must avoid making the situation worse.

Even if you think you’re tech savvy, it’s a good idea to Always make a backup before attempting to fix anything on your WordPress site.

4 steps to quickly fix the error connecting to a database in WordPress

  1. The most common reason for database authentication issues is incorrect credentials – so check them!
  2. Repair an unreadable database with the built-in WordPress repair file: define(‘WP_ALLOW_REPAIR’, true);
  3. Troubleshoot corrupted files;
  4. Contact your web hosting provider if you have problems with your database server

1. Make sure you are logged in with the correct credentials

To fix the connection database error, the very first thing you need to do is make sure that the database credentials are correct.

This is the most likely cause of the “Error establishing a database connection” message. Especially after changing hosting provider.

On WordPress the connection details are stored in a file called wp-config.phpwhich is located in the WordPress root directory.

There are four important pieces of information needed in wp-congif.php for the connection to be successful.

database name

// ** MySQL settings ** //
/** The name of the database for WordPress */
define('DB_NAME', 'xxxxxx');

MySQL database username

/** MySQL database username */
define('DB_USER', 'xxxxxx');

MySQL database password

/** MySQL database password */
define('DB_PASSWORD', 'xxxxxxxxx');

MySQL hostname (server)

/** MySQL hostname */
define('DB_HOST', 'localhost');

For each item, you must verify that the values ​​are correct and match your server settings.

Starting with the database name

You can usually find the database information in your hosting admin panel or in PhpMyAdmin.

If you have difficulties to get the access data for the database, you can contact the support of your hosting provider.

Database username and password

If your database name is already correct but you still get this error message, you need to verify both your username and password.

To do this, you need to create a new PHP file in the WordPress root directory and enter the following code. You can name the credentials_test.php but it doesn’t really matter.

<?php
$test = mysqli_connect('localhost', 'db_user', 'db_password');
if (!$test) {
die('MySQL Error: ' . mysqli_error());
}
echo 'Credentials are good! Well done.';
mysqli_close($testConnection);

Be sure to replace db_user and db_password with your own credentials. Save and upload the file.

Then navigate to the file on your WordPress site: https://mysite.com/credentials_test.php.

Whenever you have aMySQL errorreceive:Access Denied” (Access denied), you know that your username or password is incorrect and you must Reset your credentials and update your wp-config.php file accordingly.

Be sure to delete this credentials_test.php file afterwards.

2. Repairing an unreadable database using the built-in WordPress repair file

You may also need to clean up your database if it has become corrupt.

This can happen from time to time as hundreds of tables are constantly being added and removed by new themes and plugins.

If you receive the following error message when trying to access your WordPress dashboard, your database is corrupt: “One or more database tables are unavailable”.

If that’s the case, WordPress has a database repair modethat you can use. Just add this piece of code at the end of the wp-config.php file.

define('WP_ALLOW_REPAIR', true);

Then go to this URL: https://yoursite.com/wp-admin/maint/repair.php

You then have the choice of repairing the database or repairing and optimizing the database.

To speed up your website recovery, we recommend the Restore database option ” because it is faster.

Remove the line of code from your wp-config.php file after you the mentioned above Database repair performed to have.

If you’re still having connection issues with your WordPress website, continue to the next troubleshooting step.

3. Troubleshoot the corrupted files

Corruption of your files is another possible cause of the Failed to connect to database message.

Whether it’s caused by a misconfigured FTP server, a hacker gaining access to your site, or a host problem – You can fix it quickly!

In short, you will replace the core of WordPress on your website.

This will not affect your plugins, themes or media, only the WordPress installation itself.

To do this, you need to download the latest version of WordPress from WordPress.org.

Unzip the file on your computer after downloading it.

You then need to delete both the wp-content folder and the wp-configuration-sample.php file.

Next, overwrite the existing files with the new ones by uploading the rest to your website via SFTP. Replacing the problematic files ensures that you get a fresh copy that is not corrupt and preserves all your media and content.

Clear your web browser’s cache, refresh the page and go to your WordPress site to see if the error still persists.

If so, read on!

4. Finally, contact your hosting provider

If none of the above solutions work for you, you should speak to your hosting company as they are a Problem with your database server could act.

This problem can actually occur when there are too many concurrent connections to your database. The reason for this is that many hosts limit the number of simultaneous connections they allow.

Caching plugins like WP rocket can help minimize your site’s database interactions.

If the problem is related to your database server, I advise you to upgrade your current hosting plan or go straight to Kinstaswitch .