Sun

How to fix “Error Establishing A Database Connection” in WordPress

  1. Home
  2. Knowledge Base
  3. How to fix “Error Establishing A Database Connection” in WordPress

How to fix “Error Establishing A Database Connection” in WordPress

Error Establishing a Database Connection is pretty standard error and it indicates that your website is not working correctly and failed to get the information needed to access the page successfully.

Since WordPress uses PHP as the primary language and MySQL as the database, specific PHP commands get your website’s information from the database when your website visitor visits your website. So if something fails in this step, your website will not load successfully, and you or website visitors will see this error Establishing a Database Connection error in the browser.

Although this error is quite serious, usually it can be fixed really quickly. So here you will find the main solutions how to fix this error.

Solutions

1. Check the login credentials

The most common cause of this error is incorrect login details for your database.

First of all, log in to your cPanel account and navigate to File Manager, where you need to find your website folder.

Note. Also, you can access your website files via FTP.

So after opening it, then you should find the wp-config.php file of your website. Then you need to open that file in editing mode. There you should find MySQL details of your database name, username, and password.

Also, you will need to navigate to MySQL Databases and check there your database name, username of this database.

So if login details in your wp-config.php file are different from your MySQL database settings, you will need to update them that these details would match your details in the wp-config.php file. Also, do not miss to update the database password in wp-config.php file by resetting the password in the MySQL database.

After updating these details, you need to try to reaccess your website and check whether or not the error message is not shown.

Note. Your browser might be cached, so before opening your website, try to access your site via another browser or clear your browser’s cache.

However, if the error message is still shown, you might need to check your database host’s information. At Hostens, you should use localhost as your database host.

2. Repair the WordPress database

WordPress MySQL database contains a lot of information, so if something is corrupted or deleted in your database, you might get this error message when you try to access your website.

If something is corrupted in your database, you might confirm that by accessing your WordPress dashboard. There you might get an error message that indicates that you should repair your database.

So you can easily repair the database by inserting the following command into your website’s wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Also, make sure to put that command right before the section “That’s all, stop editing! Happy blogging.”

After saving these changes, you should open your website link by adding some details to the URL

http://www.yourwebsite.com/wp-admin/maint/repair.php

There you will see two options to repair your website’s database:

  1. Repair Database
  2. Repair and Optimize database

Once the database is repaired, do not miss to go back to your website’s wp-config.php file and remove added command. If you do not do that, anyone could access the repair page URL without being logged in.

3. Fix your site’s corrupted files

One more possible reason might be that your website’s theme or plugins are corrupted. If you have activated a new theme for your website and then received an error message Error Establishing A Database Connection, then you can log in to your WordPress dashboard again and navigate to Appearance –> Theme, and there activate another theme from the available options.

Also, you can disable all of your used plugins if changing the theme doesn’t solve the issue. After that, try to enable plugins one by one to find the culprit.

However, if you cannot access your WordPress dashboard, you can deactivate your WordPress theme or plugins by renaming the corresponding folders in your cPanel. You can do that by accessing your cPanel and navigating to File Manager.
There you will need to open the wp_content folder of your website. Then, you need to rename the currently used theme or the plugins folder. Folder renaming will disable your themes or used plugins at once.

Note. Try not to disable themes and plugins simultaneously to find which exact theme or plugin is causing this problem.

If you find which theme or plugin caused this issue, do not miss to delete that WordPress theme or plugin.

4. Check your database server

It might be that the database server is down. If all your websites show this error, you can contact us, and we will check that.

There are many reasons why this could happen, but usually, it might be caused by heavy traffic on a server. In this situation, the server cannot handle the number of users trying to access it, and that’s why websites show Error Establishing A Database Connection error.

Also, you can check phpMyAdmin accessibility by yourself. You can connect to cPanel and navigate to phpMyAdmin. If you cannot reach your databases, it might confirm that MySQL server is down.

Furthermore, it might be that due to increased traffic of your website, the server is being overloaded, and that’s why you get this error. So you should use some caching plugins that will help you to prevent further issues in the future.

5. Create a new database for your website

If you still see that error message when you try to access your website after performing the above solutions, it might be that your database is completely corrupted and cannot be used anymore.
So to solve this problem, you can try to a new MySQL database and user for your website. Here is a guide on how to do that. Once the database is created, try to import your database backup.

Note. Before making any attempts to fix this error, we recommend you backup your website files and database just in case something went wrong.

Was this article helpful?