A very common error in WordPress Error Establishing a Database Connection. Do not panic! This error is also usually very easy to solve following these steps.
Why Does This Error “WordPress Error Establishing a Database Connection”?
I know that you want to get to the point and solve your error directly and as soon as possible, but first you have to understand why it happens so you can solve it correctly.
Any installation of WordPress consists of a set of files (divided into different folders) and a database with several tables. For WordPress to work properly, it has to be all connected to each other and thanks to the wp-config.php file.
This file is located in the root directory of your hosting account and contains the necessary data for WordPress to connect to the database, which will be created in another folder (you do not need to touch it a priori)
The error, therefore, occurs when the access data you have in that file is not what is needed to access the database.
As a general rule, you should have typed this data at the beginning of the whole. When you installed WordPress for the first time. However, it is possible that, for some reason, they have been reconfigured. So you will have to review them manually to make sure they are correct.
So, Let’s go to solving WordPress Error Establishing a Database Connection Problem.
Find the wp-config.php File to Fixing WordPress Error Establishing a Database Connection
This file has different information about the configuration of WordPress, however, you should go to the section where it talks about “MySQL settings” since that is where the fault lies.
To access this file you can do it from the Control Panel of your hosting provider, inside the File Manager, and edit it with the “Code Editor”, for example.
There you have to locate 4 fundamental parameters, which are what I explain below (be careful not to delete the quotes or any punctuation at the time of modifying data!)
Name of the MySQL database
define (‘DB_NAME’, ‘xxxxxxxxxxxxx’);
User name of the database
define (‘DB_USER’, ‘xxxxxxxxxxxxx’);
Counterclaim access to the database
(Do not confuse with the password to access WordPress)
define (‘DB_PASSWORD’, ‘xxxxxxxxxxxx’);
Address of the server where the database is located
define (‘DB_HOST’, ‘localhost’);
In the four options, you have to check that what I have written in red letters is correct. That is, it corresponds with the configuration of your database.
The normal thing is that you no longer remember the parameters that you put … or that if you chose an automatic installation of WordPress you would not even type them, but that it was the CPanel itself.
Do not worry, there is a way to check it.
Check MySQL Data
Now you must return to your CPanel (or the control panel used by your hosting provider) and look for the icon that says “MySQL Databases”. Access that icon
The normal thing is that you only have one created, so you should follow these steps:
- Go down to where it says “Current Databases”.
- Search where you name the database
- Check that this name is the same as you have in the wp-config.php file in the ‘DB_NAME’ section
Save the file wp-config.php (only if the data was wrong and you have modified it) - Reload your WordPress page
If the error has not been solved, we have to continue on.
- Keep going down to where you put current users.
- Check that the data with your config-php file and make sure it is what is written next to ‘DB_USER’
- Save the file wp-config.php (only if the data was wrong and you have modified it)
- Reload your WordPress page
If the fault was not there either, we have to continue testing. Let’s go for the password. The disadvantage in this case is that we can not find out what you had, but it is possible to modify it with a new one.
- Click on “change password”, next to current users,
- Choose a new password
- Write the same in your wp-config file, next to ‘DB_PASSWORD’
- Save the file wp-config.php (only if the data was wrong and you have modified it)
- Reload your WordPress page
Has it been solved? We are running out of possibilities … We still need to check if the prefix of the tables included in the database is correct.
Check data in PhpMyAdmin
This check is very simple, we are only going to check a data. For this you need:
- Go back to your CPanel and choose the icon “phpMyAdmin“
- In the new screen, display your database in the list on the left
- You will see a lot of tables, and all of them, you will see that they start with xxxx_
- Go to your wp-config file and look for $ table_prefix = ‘xxxx_’; (Change it if it’s different)
- Save the file wp-config.php (only if the data was wrong and you have modified it)
- Reload your WordPress page
If even after making all these checks has not solved the error, you can only think that your hosting provider has changed the location of the server where your database is hosted (although it is not usual)
Check the location of the database
In this case, you have to contact your hosting provider, yes or yes, to ask the address where your database is hosted and put the IP or address that you provide within your file wp-config.php
Specifically, you have to search define (‘DB_HOST’, ‘localhost‘); and write the address where it says ‘localhost’ (this word means that your database is on the same server as your WordPress).
What has gone? I’m sure that in the end, it was more the shock you’ve taken than the time it took you to solve it. If you want to contribute some idea to help other users, I encourage you to leave your comment below.