Error Establishing a Database Connection in WordPress is a common error in WordPress. If you work on WordPress regularly, you may have faced these issues quite often. This means that your site/WordPress is unable to make a connection with the database. If you are seeing it for the first time then this may irritate you.
In this article, we will explain everything about this error and easy ways to fix it.
Reason Behind Error Establishing a Database Connection in WordPress
The Error Establishing a Database Connection in WordPress can occur for many reasons. If you read the error closely it says a lot about the error. It indicates that most of the time the issue lies in the database.
WordPress has two parts that make it functional. The first part is the site/WordPress itself and the second part is the database. In case you don’t know a database is where WordPress stores all your content, images, comments, etc.
Whenever someone visits your site, it requests data from the database. Once it successfully receives the data it shows that data to the user. If the database has any error then this process doesn’t get completed. As a result, it shows the error in establishing a database connection on the screen.
- A corrupted database
- Wrong Username
- Wrong Password
- Issue with Server
WordPress stores all its database username and password-related information in the wp-config.php. How you can check and fix the error, we will discuss it later.
How to Fix the Error Establishing a Database Connection in WordPress?
As you have understood the issue, it is the time to fix it.
1. Check Username and Password
In most cases when the error occurs, the issue lies in the wrong username or password. If you have more than one site and have many username and password combinations. It is likely that you are entering the wrong username and password. To do this you need to visit the wp-config.php file inside your WordPress.
Go to your cPanel and select file manager. Inside the file manager search for the file. You will find it inside the public_html directory. To make it simpler, I will mention the path below.
cPanel – File Manager – public_html – WordPress – wp-config.php
Inside the wp-config.php file scroll down and look for username and password. Before doing any of this, I would highly recommend copying the file data in a separate place. This way you will be able to restore the file to its initial condition, in case you don’t understand or break anything.
Once you find your username and password cross-check it with what you are using. You will get to know if you are using the wrong credentials. However, if you are still facing the issue then check the database name. Do not confuse this with your username and password. It is the name of your database.
If the issue still persists, then it is clear the issue is not related to the credentials.
2. Check if your Database Server is Down
If your credentials are correct this means the issue is related to the other things. For instance, it could be related to your server. If your database server is down, you face the same error establishing a database connection error.
A database server has a certain capacity. If it reaches its limits, it will cause an error. There could be numerous reasons behind this. For example, if you have more than one site in the same hosting, you may see the error. Although it can only happen if it reaches its handling capacity.
Generally, this happens when people or businesses go for a cheap hosting plan to save money. But later when they need to host many sites in the server, issues like this occur often.
Now check if your server is really down or if something else is causing the issue. You need to create a file to test it. To do this go to the root directory of your public_html folder.
Here you need to create a file. For testing purposes, we are creating check.php, you can give it any name. Now paste the code given below inside it. Once you create this, access it from the search bar like this yourdomain.com/check.php. If you can the content inside the file you have just created, it means your server is working fine.
<?php $link = mysqli_connect('localhost', 'username', 'password'); if (!$link) { die('Could not connect: ' . mysqli_error()); } echo 'You have Connected successfully'; mysqli_close($link); ?>
3. Check for Corrupted Files or Folders
Many a time a corrupted file or folder causes the error. To fix this you need to visit your WordPress dashboard and then themes. Now change your current theme. It will save the issue if the theme was causing the error. If the issue still persists, it is clear that there have no issues.
Now look for corrupted plugins. To do this go to the plugins from the dashboard and deactivate all the plugins. It will fix the issue if related to plugins.
4. Fix Corrupted File/Folder Through cPanel
The above method will work if you can access the dashboard of your WordPress. What if you can’t access the dashboard? In this case, you need to do it from your cPanel. From here go inside the theme folder and change the current theme folder name and go back to check if the issue persists.
If yes then do the same with the plugins folder. Here the method will be slightly different. Instead of changing a single plugin folder, change the whole plugin folder. After doing so, check for the error. If the issue still persists, you need to change the method.
5. Repair Database
The database stores data in tables. If your site has huge data, the database will have hundreds of tables inside it. For any reason, if the issue occurs here, you will se the error. To fix this, you need to repair your database.
To repair, go inside your wp-config.php file and paste the below-given code. Once you do this access the file from the search bar like this.
https://domain.com/wp-admin/maint/repair.php.
Now you will have database repair options. 1. Repair database. 2. Repair and optimize the database. I would recommend choosing the first option. You can do this from your phpmyadmin too.
Inside the database select all the tables and select repair from the dropdown options. Once completing the process, don’t forget to remove the repair code from the wp-config.php file else anyone will have the power to repair it.
6. Create new Database
If you have tried all the above-given methods still the problem persists, you need to create a new database. To do this, from phpmyadmin add create a database and create. At this moment your local backup will save you. That’s why we recommend creating backups strongly.
After creating the database import the database backup file. Once the process gets completed, it will hopefully fix your error.
Final Thoughts
In this article, we have given all the methods that are most effective in solving error-establishing database connection errors. If for some reason these methods fail, you must contact your hosting provider to fix the error. They will guide you through the problem.
You also have the option to hire an expert developer who can fix this issue for you. We hope this guide helped you resolve your issue. You can also check our detailed guide on how can you uninstall your WordPress from cPanel.