Error establishing database connection may happen due to two different reasons. I will walk you through the process to fix the issue on a priority basis.
1. Error establishing database connection for corrupted database
If you had a working website in the past and if you see the ‘Error establishing database connection’ very recently or if you see the error after activating a plugin, then your database has been corrupted.
Now you have to repair the database. Don’t be afraid, it’s very easy. And it will not break anything on your site if anything goes wrong.
Follow the steps with me to fix the error.
Open the “wp-config.php” file and paste the following code at the very bottom.
This wp-config.php file is located at the root of your WordPress installation. To be more specific, it’s the same directory as wp-admin, wp-content, wp-includes.
Anyways, after you pasted the code, save the wp-config.php file and go to the following URL: http://your_site.com/wp-admin/maint/repair.php
Don’t forget to replace the “your_site.com” with your actual domain.
After you go to the URL, you’ll see two options for repairing the database just like the screenshot below.
Click the first option “Repair Database” and WordPress will repair the database & show you a long list of text. That means the database has been repaired successfully and you fixed the error just a moment from now.
Well, don’t forget to remove the line that you added to the “wp-config.php.” Otherwise, anyone can perform the same action that you did a moment before.
2. Error establishing database connection for wrong credentials
If the first method (repairing database) did not work for you then follow this one. Error establishing database connection also happens for wrong login credentials that you have written in the “wp-config.php.” Follow the steps below to fix the database connection.
Open the “wp-config.php” file again and look for the following 4 lines:
The above information needs to be correct in order to make a connection with the MySQL database.
In this step, together we will go through the process and find or manage the 4 values (DB_NAME, DB_USER, DB_PASSWORD, DB_HOST). Be patient and walk with me.
Login to “phpMyAdmin” from your hosting panel.
A. FIND THE DB_NAME
After you click the “phpMyAdmin”, it will redirect to a page where you will see the list of the databases (if you have multiple websites).
Remember/copy the “DB_NAME” that you see on your wp-config.php file. And search for it. In my case, the “DB_NAME” is “cal_WPDMS” and in your case, it must be different.
So search for the “DB_NAME” that you see on your wp-config.php file. And if you found the name on the list then your “DB_NAME” is correct. But if you did not find it then you have to correct it.
B. FIND THE DB_USER
On your hosting, under the “Databases”, you will find another option called “MySQL Databases” (see screenshot below) and click on this option.
On the next page, scroll a bit and you will see a section (Current Users) that contains the list of current database users.
Same as past, check if any username matches the “DB_USER” (wp-config.php). But you don’t find any matching username, you can create a new one and assign the new user to the database and update your wp-config.php file.
C. FIND DB_PASSWORD
Luckily, there is no way to find the “DB_PASSWORD” because all the passwords are encrypted by MD5 before storing them in the database.
So, if you do not have the password then you have to create a new DB user and assign it to the database.
D. FIND DB_HOST
The “DB_HOST” is generally “localhost” for most of the hosting companies, but it may be different for a few hosting such as DreamHost, GoDaddy, etc. You can google for your hostname or ask a customer support agent.
Now you have all the 4 credentials that are required to connect the database. Update your wp-config.php with new credentials and hit save. And it should solve the “Error establishing database connection.”
If you tried the above two methods and if none of these have solved the Error establishing database connection then it occurred from your hosting company. In this case, you have to ask them to fix the issue.