Let me tell you a few things before showing you how to fix error establishing a database connection in WordPress?
Errors are unpredictable. It can happen any time, but without reason, it wouldn’t happen.
Sometimes the error may cause from your side or any other source. So the main focus of the website owner should be at fixing the fault as soon as possible.
Error Establishing a Database Connection is the most common error the WordPress website owners face. Some guys get panic when they see this error and start to look for the solution.
You shouldn’t be among the panicking guys; instead, you should take a cup of coffee and start looking for the “cause and solution“.
You might ask, Why are you saying “cause and solution”?
The answer is straightforward; you might face this error in future also. So it is best practice to look for the root cause of the problem. So whenever you meet the issue in future, you should be able to solve it by own.
Understanding “Error Establishing a Database Connection”
The sentence “Error Establishing a Database Error” is self-explanatory.
The error is related to the database. WordPress is most compatible with MySQL because this content management system is built primarily with two technologies.
- PHP: PHP(Hyper-Text Pre-processor) is the programming language used by WordPress.
- MySQL: MySQL is used to store the website data like your posts, settings, users, etc.
So the question arises “Why WordPress throws this Error? “.
Well, there are few reasons which are listed below.
- Incorrect Login Credentials: If you entered the wrong database login credentials, then the error will be thrown.
- Corrupted Database: If your database is corrupted, this might be the cause.
- Server Memory: This may be another cause of this error.
- Corrupted WordPress Files: If the core WordPress files are corrupted, this might be the cause then.
Looking for the Cause of Error Establishing a Database Connection
My personal way to look for the cause of the error is to set debugging to “ON”.
Debugging is the easiest way to look for the root cause of almost any error.
Until now, I have faced this issue many times; that is why I am listing this method here.
Where To Set Debugging to “ON”?
It isn’t a big deal. But before you start, please keep a fresh backup of the website.
Another thing I want to mention here is that you should know “Little programming”. If you don’t have any programming back, then jump over the “Fix section” directly.
Step #1: Go to your core WordPress files. There you will find a
Step #2: Now starting editing it. Don’t write anything inside the file yet. By editing I mean look for an option in your web host panel to edit the file.
Step #3: Inside the file look for this line
define("WP_DEBUG", false);. Set the value “false” to “true” just like this
define ("WP_DEBUG", true);. Now hit Save and visit your site. You will see different error message this time but with a point source of this error.
As mentioned earlier, If you have a programming background, then do this step otherwise not.
How To Fix Error Establishing a Database Connection In WordPress?
As said earlier, you should first look at the cause of the error then you would be able to present a proper solution.
Repairing the Database
If your database is corrupted then there is no way to solve this except repairing.
Another point you should keep in mind is that if your website’s front-end and back-end are throwing the error, then your database is healthy and you just need to check the login credentials.
But if it is opposite to above, then your database requires repair.
Repairing a database in WordPress is pretty straightforward. You have to follow these steps carefully.
Let’s start repairing the database.
Step #1: Go to root files of your website. Inside the files look for the
wp-config.php and start editing it.
Step #2: Inside the file look for this comment
/* That's all, stop editing! Happy publishing. */. Before this comment, add this line.
define ( 'WP_ALLOW_REPAIR' , true);
Step #3: Now navigate to this URL yoursite.com/wp-admin/maint/repair.php. Be sure to change the yoursite.com with your domain, and you will see this screen.
Step #4: Now click any of the two options, but the recommended is second, which is “Repair and Optimise Database“. You can head forward with the first option also. I am not binding you this option.
Step #5: This step is just check. Navigate towards your domain and see if the error is gone.
If it is gone, then the database was corrupted; otherwise, the problem is with login credentials which we will discuss in the next section.
Checking the Database Login Credentials
Entering wrong database details can throw this error. You might check the login credentials first.
Let’s start and check how this method can help us.
Step #1: First thing you have to do is to note down your database credentials in Notepad.
If you don’t remember the login details approach towards web host support team.
Step #2: Now open the
wp-config.php file. Inside it locate these lines.
// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', 'db_name' ); /** MySQL database username */ define( 'DB_USER', 'db_username' ); /** MySQL database password */ define( 'DB_PASSWORD', 'db_password' ); /** MySQL hostname */ define( 'DB_HOST', 'db_host' );
Now, your database login credentials are stored. If they are incorrect, then it will produce “error establishing database connection” error.
Step #3: If you followed step #1, then move onwards. Compare the login credentials you noted down on Notepad with these login credentials. If you found anything wrong, change it accordingly.
Recheck the website. If you are still facing the issue, head towards the web host and ask for help.
I tried my best to keep this tutorial as short as possible and to the point. I hope these methods answered you query “How To Fix Error Establishing a Database Connection In WordPress“.
If you are still facing the issue, please let me know in comments. Might be I will tell you other solution.
Please do share the post with your friends.