How to Fix Error Establishing Database Connection in WordPress
If you have been working with WordPress for some time now, you might have come across a common issue. When visiting your website you see “Error Establishing Database Connection”. This is an error message that appears when WordPress cannot connect with its database.
For your website to show the content, WordPress first needs to be connected with a database as it holds information that is crucial to operating. If for some reason, WordPress is unable to connect to the database, it won’t have access to that information and thus, won’t be able to load your website.
In this article, we will learn what can cause the “Error Establishing Database Connection” problem and how to solve these issues.
Reasons behind “error establishing database connection”
There are several reasons that WordPress might not be able to connect with your database. Let’s take a look at a few reasons:
- Wrong credentials in the wp-config.php file
- Migrating to a new host
- Buggy plugins or theme
- Corrupted database
- Corrupted WordPress file
- Server-side issue (e.g. downtime)
How to fix the Error Establishing Database Connection issue in WordPress
Depending on the cause of the issue, there are several ways to fix it. Let’s find out:
1. Check if your database credentials are correct
In most cases, when you see the error message “Error Establishing Database Connection”, the issue is with your database credentials. In order to check if your credentials are OK, follow the steps given below:
a. First, go to your cPanel and open File Manager.
b. Then open the folder containing your WordPress website (typically public_html)
c. Right-click on wp-config.php and select edit.
d. Now take a look at your database credentials
Verifying the database credentials
Now you need to verify if this information is correct. To check, go back to your cPanel and follow the below steps:
a. Click on MySQL Databases from cPanel.
b. Scroll to the Current Databases section and check if the database name and username match the credentials found in your wp-config.php file.
c. If the password is different, scroll down current users and change the password if you need. Make sure it matches with the password in the wp-config.php file.
After you have ensured that your credentials are perfect, then try visiting your site. The error should be gone. However, if the error still exists, move on to the next step. If you don’t know how to access WordPress files, see our tutorial.
2. Repairing the database
If the previous step didn’t fix your error then it might be possible that your database is corrupted. There are various ways of fixing your database. Let’s see how you can do that:
Fixing the database through MySQL
The easiest way of repairing a database is through MySQL. Let’s see how:
a. Access your MySQL Databases from the cPanel
b. Next, scroll to ‘Modify Database’ and click ‘Repair Database’
This will repair your existing database.
Fixing database through phpMyAdmin
It is possible to repair the database from your phpMyAdmin. Let’s take a look at how you can do so:
a. Access phpMyAdmin from your cPanel
b. Select your database and check the ‘check all’ option
c. Now, with all your tables selected. Select ‘Repair Table’ from the drop-down
This was another way to repair a corrupted database.
👉 Read More – Fix: WordPress Briefly Unavailable for Scheduled Maintenance
Fixing the database through WordPress
WordPress has a built-in function to repair its database. Let’s see how you can do it.
a. In order to start WordPress’s database repair function, you need to edit a parameter in the wp-admin.php file. Follow the steps shown in ‘Check if your database credentials are correct’ step above to access the wp-config.php file.
b. Now add this parameter: define(‘ALLOW_DB_REPAIR’, true);
Then click save changes.
c. Now you have to do is visit your website’s repair page. The URL for the repair page is generally
http://www.examplesite.com/wp-admin/maint/repair.php
Replace ‘examplesite’ with your domain name and hit ‘Enter’. Now you will be directed to your WordPress Repair page. From this section select the ‘Repair Database’ option.
You may delete the “define(‘WP_ALLOW_REPAIR’, true);” parameter from your wp-config.php file now.
3. Removing the theme or plugins that are likely to cause the problem
Sometimes outdated/defective plugins might cause the “Error establishing database connection” error. The theme that you are using might also cause this issue. Let’s see how you can identify and solve this problem.
a. Access File Manager from your cPanel
b. Open your public_html folder
c. Open the ‘wp-content’ folder
d. Now find the two folders, ‘plugins’ and ‘themes’, and rename one of them. For example, rename the ‘plugins’ folder to something like ‘not-plugin’. What I mean is, rename it into anything else other than ‘plugin’. Then access your website. If you see the problem still exists, then rename it back to ‘plugin’. Now do the same thing with ‘themes’
(You can also keep both the plugins and themes folders renamed at the same time.)
If you see that your website is loading correctly, then you likely have found your issue. Uninstall that plugin or theme that was causing the “Error establishing database connection” error.
👉 Learn More: How to Use WordPress Recovery Mode – Complete Guide
4. Change the database URL
A broken URL might also cause this issue so it might be a good idea to try and repair it.
a. Access your phpMyAdmin from cPanel
b. Select your database, and select SQL from the top
c. Now in the query editor paste the following code and replace the ‘experimentalsite.com’ with your own domain. Then click ‘Simulate query’
UPDATE wp_options SET option_value=’experimentalsite.com’ WHERE option_name=’siteurl’
This would very likely solve your issue
5. Reboot the server
If you are using a VPS (Virtual Private Server), dedicated or local server then you will have access to this option.
To reboot your server, log into your hosting account and check the status of your server. Whether your server is running or not, it might be helpful to restart it. To do this find and click the option called “Restart VPS”.
Remember, you will only have access to this option if you have a VPS, dedicated or local server.
6. Restore a recent backup
There is nothing on the internet that is not prone to hacking or crashing. Even if not attacked by hackers, your site might crash sometimes. For this reason, it is a good idea to make some backup of your WordPress website.
You can use plugins like UpdraftPlus, BackupBuddy, BlogVault, etc. to create backups for your website. Create a daily, weekly, or monthly backup to keep your site and your work safe.
For this example, we will use UpdraftPlus. Follow the steps shown below to restore a backup of your website:
a. Log into your WordPress and if you have UdraftPlus, then you will see a UpdraftPlus option in WordPress. Hover your mouse over it, and select Backup / Restore
b. Now scroll down to Existing backups, if you have backup copies, you can simply restore them by clicking the ‘Restore’ button.
It will take some time, but UpdraftPlus will restore your files and database. After it is done, you should be able to access your website.
If you cannot access the back-end of your site, then you need to use any off-site backup method (if available) to restore the site.
7. Call for support
If none of the above-mentioned solutions worked, then you have nothing else to do but call for support. It is likely there is a problem with your server. You cannot always expect your server to have 100% uptime. Sometimes there might be issues with your server. If you are using a shared host, this error might occur more.
Call your host provider and inform them about this error. Once they are done fixing the issue, try to log in to your website again.
Conclusion
While working with WordPress we might face some errors similar to this one. Whenever you face such an error, do not panic because there is a large community of WordPress users and developers. You can be almost certain that the error you are facing definitely has a solution. You just have to look for it.
It is a good idea to always keep your plugins and themes updated. Use plugins and themes from your trusted companies and you might face fewer errors.
Disclaimer: This post may contain affiliate links and we may receive a small commission if you purchase something by following them. However, we recommend services/products that we believe good to serve your purpose.