Here are a few common reasons why this particular error occurs on a WordPress website:

Invalid database login credentials in WP config

The most common cause of this error is incorrect database login credentials. There are no similar login credentials for WordPress site and database login. Login to the database requires separate credentials, which are too hard to guess. If any username or password change occurs, WordPress will not be able to retrieve any information from the database.

Corrupted database

What to do if the database is corrupted? Yes, this may be the cause of the error. Everyone has old version plugins and themes that are no longer used. But no one takes the time to deactivate or remove them. This creates loopholes that allow hackers to easily break into the site.

Another possibility could be a failed WordPress base update that includes missing plugins, themes, or even a glitch in the WordPress software update.

See also:

How to Repair a Corrupted WordPress Database: 2 Methods.

Hacked files in WordPress installation

It is possible that there are certain corrupted files in your WordPress installation that create a database related error. Such files may be loopholes or backdoors placed by hackers to regain access to your site.

Database server issues

There may be issues on the server that need to be resolved quickly. For example:

The database is overloaded due to traffic.

Too many concurrent connection requests. While this is good for your site, it can create problems.

This type of issue usually occurs due to shared hosting where multiple users are hosted on the same server.

WordPress database tables have been corrupted

Sometimes tables get corrupted for various reasons and this is one of the common problems that causes an error when establishing a database connection.

How to fix an error when establishing a database connection?

Before proceeding with the elimination of this error, we recommend that our readers make a backup copy of the site along with the database. Make it a habit whenever you are trying to fix any mistake on your WordPress website.

There are several plugins that automatically backup your site, so you don’t have to do it manually every time you update your site or make any changes to it. You can use WordPress backup plugins like UpdraftPlus or Duplicator to backup both files and database.

Now let’s start troubleshooting.

Check database login credentials

The main reason for this message to appear is the wrong credentials used to log into the database. This happens to most people who move to a new host. Make sure you are using the correct username and password.

The connection details for a WordPress site are stored in the wp-config.php file.

Screenshot of wp-config file

The file contains four important pieces of information that must be correct for a successful connection.

Database name (“DB_NAME”)

Login Username (“DB_USER”)

Login password (“DB_PASSWORD”)

Host(“DB_HOST”)

If any of these values ​​are incorrect, WordPress will not be able to connect to the database.

You can easily find the wp-config.php file in cPanel. Just click “File Manager”, go to the root of your site and right click to edit the file. Here you need to check all input values ​​with those that are on the server to make sure they are correct.

How to Fix Database Connection Error in WordPress

DB_Name: The first thing to check is the name of the database. You can check it through cPanel control panel. Login to phpMyAdmin and check your database name.

When you click on your database name, a screen appears with a list of table names. Click the Browse button next to the table named wp-options and see if you find your site’s name and URL along with other settings.

If yes, and the name matches your wp-config entry, you have the correct entry. If the URL is correct but your wp-config file is incorrect, change the name in it to match the correct database.

Examination

Then check the username and password. This can be checked in two ways:

Create a PHP test file to check if the credentials from wp-config connect to the database. Paste the following code:

one

2

3

four

5

6

7

eight

<?php

$test Connection = mysql_connect(‘localhost’, ‘root’, ‘password’);

if (!$testConnection) {

die(‘Error: ‘ . mysql_error());

}

echo ‘Database connection working!’;

mysql_close($testConnection);

?>

Add the credentials to a new text file and save it.

Now enter the file URL into your browser and try; you will get a “Connected Successfully” or “Unable to Connect” message.

If you didn’t find the username and password in the table, create a new one in wp-options. Don’t forget to make changes to the wp-config.

If you don’t know how to add a new user, follow these steps:

Go to cPanel and click “MySQL Databases”.

Scroll down to MySQL Users. from there, add a new user.

Select the username and password you just created, and click Add.

Now find “Add user to database”. Select the username and password you just created and click Add.

Also update the wp-config file with the new credentials.

Check if the error is resolved.