This WordPress tutorial explains how to solve the error when establishing a connection to the database shown in the screenshot below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-1.png" data-lazy- height="224" src="data:image/svg xml,” width=”549″>

After reading this tutorial, you will learn how to check your database name, username, and password and change them within the WordPress configuration file and your server settings.

All instructions in this WordPress tutorial include screenshots, making it easy for all users to follow the steps described.

What “Error establishing a database connection” means:

As the error explicitly says, the problem is your WordPress can’t connect to the database, defined in the wp-config.php file within your website root directory.

This file (wp-content.php) contains the database name, username, password, and server. If some information does not match with the information defined on the server, the connection to the database will fail.

This tutorial explains how to make sure the credentials in the wp-config.php file are the same you have defined in your server.

Checking your database credentials in WordPress:

To begin, login to your Cpanel account; on the main screen, scroll down and access the File Manager.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-2.png" data-lazy- height="531" src="data:image/svg xml,” width=”941″>

Once in the File Manager, move to your website root directory and find the file named wp-config.php. Once found, right-click it and press the Edit option, as shown in the screenshot below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-3.png" data-lazy- height="384" src="data:image/svg xml,” width=”751″>

If asked to confirm encoding as shown in the following image, press the Edit button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-4.png" data-lazy- height="423" src="data:image/svg xml,” width=”617″>

In the file wp-config.php you will see content similar to the following (except for venteany_wp588, venteany_wp588, and [email protected], which are my own credentials, here you will see yours. We need to make sure the same credentials are defined in your database. Save this file or the relevant content to compare later.

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

define( ‘DB_NAME’, ‘venteany_wp588’ );

/** MySQL database username */

define( ‘DB_USER’, ‘venteany_wp588’ );

/** MySQL database password */

define( ‘DB_PASSWORD’, [email protected] );

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-5.png" data-lazy- height="388" src="data:image/svg xml,” width=”576″>

Go back to your Cpanel main screen, find MySQL® Databases, and press it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-6.png" data-lazy- height="519" src="data:image/svg xml,” width=”929″>

Depending on the number of sites or databases you created, you may see one database or a list of them. Find the proper database, which you learned when reading the wp-config.php file. The proper line within the file is

( ‘DB_NAME’, ‘venteany_wp588’ );

In your case, “venteany_wp588” will be replaced with your database name.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-7.png" data-lazy- height="550" src="data:image/svg xml,” width=”1160″>

Scroll down until you find your database and press it.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-8.png" data-lazy- height="385" src="data:image/svg xml,” width=”763″>

There you can see the defined username; make sure it’s the same as defined in your wp-config.php file (Line define( ‘DB_USER’, ‘venteany_wp588’ ); where venteany_wp588 is replaced by your actual DB username).

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-9.png" data-lazy- height="394" src="data:image/svg xml,” width=”713″>

If the user is correct, you can go back to the previous screen by scrolling down and pressing the Back to Manage Databases link. If the user isn’t the same as defined in the wp-config.php file, we found the problem. In this case, also press Back to Manage Databases.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-10.png" data-lazy- height="74" src="data:image/svg xml,” width=”617″>

If the user isn’t the same as defined in your wp-config.php, you have two options: Replace the user-defined in the wp-config.php file with the one shown in the previous step, or create a new DB user with the sanem usernames defined in the wp-config.php file.

If the database name and the user are the same in the server (previous step) and as in the WordPress configuration file, then the problem may be the password. On the main databases screen, scroll down until finding the section named Current Users<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-11.png" data-lazy- height="501" src="data:image/svg xml,” width=”967″>.

You may see a long list of users depending on the number of users and databases, scroll down until you find the relevant user.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-11-1.png" data-lazy- height="501" src="data:image/svg xml,” width=”967″>

When you find the user, press the Change Password button to define a new password.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-12.png" data-lazy- height="426" src="data:image/svg xml,” width=”811″>

Now you need to define the password. The password must be the same in this step as in the wp-config.php file. You can copy-paste here the password found in that configuration file, or you can set a new one, in whose case you will need to set the same password in the wp-config.php file (Line define( ‘DB_PASSWORD’, ‘[email protected]cxxnBP1’ where –@S1cxxnBP1 must be replaced with your actual password). Once you type the password, press the Change Password button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-13.png" data-lazy- height="387" src="data:image/svg xml,” width=”611″>

If the username does not match the one defined in the wp-config.php file, you can replace the username defined in the wp-config.php file and replace the existing one with the three steps shown above. To do it, apply the following steps:

  • Open the Cpanel File Manager.
  • Find the wp-config.php file and edit it.
  • Replace the username defined in the line define( ‘DB_USER’, ‘venteany_wp588’ ); (In this case venteany_wp588 must be replaced).
  • Save changes.

Another option is to create the user and password defined in the wp-config.php within your database, as explained below.

To create a new DB user and password according to the one shown in your wp-config.php file, on the Databases Manager page, scroll down until finding the “Add New User” section. Type the username password (the password must also match the one defined in the wp-config.php) and press the Create User button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-14.png" data-lazy- height="492" src="data:image/svg xml,” width=”628″>

Once the user is created, you can press the Go Back button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-15.png" data-lazy- height="317" src="data:image/svg xml,” width=”766″>

You need to associate the user you just created with the proper database. On the Databases Manager screen, scroll down until finding Add User to Database. Select the user you just created and the proper database and press the Add button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-16.png" data-lazy- height="337" src="data:image/svg xml,” width=”677″>

Grant the user all privileges by selecting ALL PRIVILEGES as shown below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-17.png" data-lazy- height="529" src="data:image/svg xml,” width=”868″>

Then scroll down and press the Make Changes button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-18.png" data-lazy- height="450" src="data:image/svg xml,” width=”819″>

Now your WordPress site must work, with the same database name, username, and password defined both in the wp-config.php file and your server.

In case it doesn’t work, go back to the Cpanel File Manager and make sure the username and password you created match the data in the configuration file.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-19.png" data-lazy- height="384" src="data:image/svg xml,” width=”751″>

In case it doesn’t match, type it again and save changes.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-20.png" data-lazy- height="395" src="data:image/svg xml,” width=”857″>

Fixing MySQL Databases errors from Cpanel:

In some cases, databases may get broken, and the easy steps shown below may solve the problem.

On the Databases Manager, scroll until you find Modify Databases. Select your database and press the Check Database button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-21.png" data-lazy- height="535" src="data:image/svg xml,” width=”896″>

Wait until the check ends and scroll down to press the Go Back button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-22.png" data-lazy- height="549" src="data:image/svg xml,” width=”614″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-23.png" data-lazy- height="432" src="data:image/svg xml,” width=”937″>

Then, again in the Modify Databases section, select your database in the Repair database section and press the Repair Database button.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/connection-WordPress-24.png" data-lazy- height="537" src="data:image/svg xml,” width=”859″>

These steps might solve your database connection error if the previous didn’t.

Conclusion:

The WordPress error message “Error establishing a database connection” is a commonly known error. As explained previously, any user with server credentials can fix it in a few steps.

This tutorial focused on the main reasons for this problem: Database name, username, or password incompatibility. If you are an advanced MySQL user, you can also apply the changes explained above from the terminal. If you are using Plesk or any other hosting manager, the steps are the same, with a different but also intuitive user interface.

Thank you for reading this article explaining how to solve the database connection error in WordPress. I hope it was useful. Keep following Linux Hint for additional high-quality tutorials.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/linuxinstitute_icono-150×150.png" height="112" src="data:image/svg xml,” width=”112″>

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.