How To Fix The “Error Establishing a Database Connection” 2023
Error Establishing A Database Connection error is one of the most annoying problems that wordpress users often encounter, which can occur for many reasons.
The Error Establishing a Database Connection error may appear even if you have not made any changes to your website.
I just encountered this error on my own site yesterday and it took me a long time to find and fix the problem. While searching for the problem, I realized that there is no detailed article written about it.
In this article, I will try to explain in detail what causes you to get an Error Establishing a Database Connection error and how to solve this problem.
Why Are We Getting Error Establishing a Database Connection?
The reason we get an Error Establishing a Database Connection error is because your site files cannot connect with the database.
We know exactly the main cause of the error, but what makes the error a headache is that the connection problem between your wordpress site files and the database can be caused by many reasons.
We can show the situations that cause the “Error Establishing a Database Connection” error, such as your WordPress database username or password or database name is incorrect or changed, you cannot get a response from your database server, or your database has crashed.
Step: 1 Does Error Establishing a Database Connection also Occur in /wp-admin/?
When you get an Error Establishing a Database Connection error, the first thing to do is to check whether this error also occurs in your site’s admin panel. So, is the error message shown for the front and back (admin panel) of your site the same?
Do both sides of your site show the Error Establishing a Database Connection error or do you get a different error in the admin panel?
If the Error Establishing a Database Connection Error occurs in your admin panel (dashboard), that is, if you are getting this error for your entire site, skip this part and go to the second step below.
If in the admin panel “One or more database tables are unavailable. If you’re getting a different error like “The database may need to be repaired”, all you need to do is repair your wordpress site’s database.
You can repair the database by adding the code below to your wp-config.php file;
define('WP_ALLOW_REPAIR', true);
After adding the code, go to http://www.yoursite.com/wp-admin/maint/repair.php and you will see a screen like the picture below.
In this section, you can repair your database by clicking one of the repair database or repair and optimize database buttons.
Important!!!: With this process, that is, due to the code we added to the wp-config.php file above, we actually made our address http://www.yoursite.com/wp-admin/maint/repair.php visible to everyone, so anyone can enter this url and repair your site’s database. It can also be done externally.
So after completing this process, you need to enter wp-config.php again and delete the code we added.
If you are still getting the same error after repairing your database, then you should continue reading my article.
Step: 2 Checking Wp-Config File
One of the most important files among WordPress files is the wp-config.php file. This file connects your database and site files to each other.
If you have changed your database password or username and you have not specified this change in your wp-config.php file, this will cause you to not receive an Error Establishing a Database Connection error.
If you edit your new database username and password in the wp-config.php file, the problem will disappear.
First of all, you need to go to your wp-config-php file and check that all the information is correct.
define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');
Important!!!: The db_host part may not always be localhost. This is usually the case, but some hosting companies may use different prefixes. You can confirm this by asking your host if DB_HOST is “localhost”.
But as I said, it is localhost in most hosting companies, so I recommend that you consider this as the last possibility that will cause you to get an Error Establishing a Database Connection Error.
If everything you checked is correct, or if you still get Error Establishing a Database Connection after correcting the wrong, this indicates that the error is on MySQL.
Step: 3 Checking MySQL Server
Another factor that causes you to get an Error Establishing a Database Connection Error may be that your site is receiving too much traffic.
This causes your server, that is, your host, to not be able to handle the traffic you receive, that is, you get an Error Establishing a Database Connection Error, especially if you are using shared hosting (public hosting).
The best thing you can do in this situation is to contact your host and ask if your MySQL server is handling the traffic.
If you want to do this yourself without contacting your host, check your other websites on the same server, if you are getting the same error on those sites, the exact reason you get Error Establishing a Database Connection is from your MySQL server.
1- If you do not have another site on the same server, go to your cPanel and connect to your database via phpmyadmin. If you can connect, you also need to check the permissions of users to access your database.
To do this, create a file named testconnection.php on your site and paste the code below into this file.
<?php
$link = mysql_connect(‘localhost’, ‘database_name’, ‘database_password);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);
?>
Important!!!: Be sure to replace the password part and username with your own user information.
After adding the code, go to the URL you created with the name testconnectiong.php in your browser (yoursite.com/testconnection.php)
If you get a “Connected successfully” message when you go to the address, that is, if you connect successfully, this indicates that your visitors have sufficient permissions to connect to the database of your wordpress site.
If you get an error here, first go back to your wp-config.php file and review everything again and perform this process again. I’m sure there is something you missed.
2– If you go to your cPanel and try to connect to your database via phpmyadmin, you cannot connect and you get an error like the one below;
Access denied for user ‘foo’@’%’ (using password: YES)
It means there is an error on your servers. This doesn’t just mean that your servers are down, it may also be a sign that there is a problem with user permissions.
When I got this error and tried to access the phpmyadmin panel via cPanel, it gave me an error like;
#1215 – Access denied for user ‘taa’%’@’ (using password: YES).
When I contacted Turhost, they said that the user’s permission was somehow reset. They couldn’t find how to reset themselves, but when they changed my user permissions, my site was back to its original state.
As a result, if you continue to get Error Establishing a Database Connection error while trying to access the phpmyadmin panel via cPanel or when you perform the testconnection.php test (with the correct database information), you should contact your host.