error establishing database connection

How to fix Error Establishing a Database Connection in WordPress

The “Error establishing a database connection” message strikes fear in WordPress users. You try to load a page, but all you see is a white box with a mysterious error message.

In this article, we will show you how to fix an error establishing a database connection in WordPress. Let’s get started with:

How to fix Error Establishing a Database Connection in WordPress?

Don’t be panic, this error is also usually varied easily to fix the following steps.

You can also read this article: How to Fix the Internal Server Error in WordPress?

What is the Error Establishing a Database Connection in WordPress?

A WordPress is a content management system, which is composed of two parts: the files and the database. WordPress has a lot of files which are static files and code files written in PHP.

All of your pages, posts, username, password, comments, and configuration data stored in a MySQL database.

Why does the Error Establishing a Database Connection Error Occur?

The database connection errors mean the username and password in your wp-config.php file are incorrect or we can not connect the database server. It can not get the information it needs to build the page you are trying to load.

There are several possible causes:

  • Incorrect login credentials-Your database uses a set of login credentials like username and password. WordPress is attempting to authenticate with credentials the database doesn’t recognize, so the request is denied.
  • Network connection errors-If your hosting server is crashed, the database can’t be reached on the network. It might be happening, when the database server is overloaded, the database server goes down or the hosting provider has a network issue.
  • The database is corrupted– Your database can get corrupted if you install a defective theme and plugin. Corrupt databases are less likely than corrupt files.
  • WordPress files are corrupted– If your core WordPress files corrupted that can lead to invalid database requests. This problem often caused by malware.

Usually, WordPress database errors mean WordPress can’t connect to the database or the database can’t respond with the right information.

How to Fix Database connection errors in WordPress?

Sometimes, it can be challenging to identify the error’s cause. In this article, I will show you six methods to fix the “Error establishing a database connection” problem.

Method 1.Restore a recent Backup file

All the time, you should have a recent backup of your website available. This backup also includes a copy of your database, and you can restore the entire website from this backup.

If you have already set up automatic backups with your web host, you can usually restore them from your control panel.

Method 2.Check WordPress Database Credentials

The most common reason for database connection errors in WordPress is incorrect database credentials.

WordPress needs your database name, username, password, and server information for connecting to the database. This all information is stored in your WordPress configuration file which is called the wp-config.php file.

The WordPress configuration file contains important WordPress settings including database information. If any of this information is incorrect, WordPress would fail to connect to your database server and you will see the “Error Establishing a database connection”.

This configuration file or wp-config.php file located in the root directory of your WordPress installation. To check these details you can use your cPanel or FTP file. Here I am using cPanel.

Fix error establishing database connection with cPanel

At first login to your cPanel and navigate to File Manager.

error establishing a database connection

Move to your WordPress installation root folder public_html folder. Click on the wp-config.php file with the right mouse button and select the Edit option.

error establishing a database connection

This file should contain the database name, username, and password of the database assigned to your WordPress installation.

error establishing a database connection

You need to make sure that the information for the database name, user name, password, and database host is correct.

You can confirm this information from your hosting account or cPanel dashboard. Simply log in to your cPanel dashboard and click on the MySQL database under the database section.

Error Establishing a Database Connection in WordPress

This will take you to the database management page in your cPanel dashboard. From here you can find your database name and the user name.

error establishing database connection

You will also find the database users and links to change the user password.

error establishing database connection

Once you have confirmed your database name, username, and password, you can change this information in your wp-config.php file if they need.

After that see your website again, the error establishing a database connection error on your WordPress site should be fixed.

If you can still see the error, this means something else is wrong.

Method 3.Check for Corrupted WordPress plugin files

Sometimes your WordPress plugin an issue for error in establishing a database connection on your website. You need to find out which plugin is corrupted.

Using your WordPress Dashboard

To do this go to your WordPress dashboard click on the Appearance button. Then click on the Plugins button.

error establishing database connection

You should deactivate the plugin one by one. If you find which plugin was the culprit, you should delete that plugin and reinstall that plugin from the WordPress directory.

If you don’t have access to the admin area of your WordPress site, then you need to use your FTP Client.

Using your FTP Client

You need to connect your FRP client with the File Manager option in your WordPress hosting control panel.

Once connected, you need to find out the public_html folder and open it. You can see the wp-admin, wp-content, and wp-includes folder are included here.

error establishing database connection

Open the wp-content folder, you will see a plugins folder. All your installed plugins are in this folder.

error establishing database connection

You need to right-click on the plugin folder and rename it as ‘plugins_dactivate’. For doing this all of your installed plugins will be deactivated.

Using cPanel

Log in to your cPanel dashboard and open the File Manager menu, go to the root folder like public_html file.

 

Click on the wp-content file, you can see there is a plugin file. Now you need to change the plugin file name (like- plugins_disable). This will disable all your installed plugins.

If the error like the error establishing a database connection is gone, it means one of your active plugins was the culprit.

To find out which one, you can do this one by one renaming the plugin file name then return the plugins folder to its original name. Changing its name each time, then checking to see if the error goes away. Keep disabling plugins one by one until you fix the problem.

If your website is now loading fine, you have found the troublemaker. Now, you can fix the problem by reinstalling that corrupted plugin.

If none of your plugins are causing problems, it’s time to turn towards your active theme.

Method 4.Check for Corrupted WordPress theme files

Sometimes your WordPress theme an issue on your website. You can easily find out if your theme is corrupted. You can be switching to a default WordPress theme like Twenty Nineteen or Twenty-Twenty.

Using your WordPress Dashboard

To do this go to your WordPress dashboard click on the Appearance button. Then click on the Themes button.

error establishing database connection

You can see the Theme page and then click on the Activate button of the default theme like Twenty Nineteen or Twenty-Twenty.

However, if you don’t have access to the admin area of your WordPress site, then you will need to use your FTP Client to switch the theme.

Using your FTP Client

You need to connect your FRP client with the File Manager option in your WordPress hosting control panel.

Once connected, you need to navigate to the public_html folder and open it. You will see the wp-admin, wp-content, and wp-includes folder.

 

Inside the wp-content folder, you will see a folder called themes. All your installed themes are stores in this folder.

You need to right-click on the themes folder and rename this folder as ‘themes _dactivate’. For doing this your installed themes will be deactivated.

WordPress will automatically activate the default theme like Twenty Fifteen or Twenty Sixteen.

Using cPanel

Log in to your Cpanel dashboard and navigate to the File Manager menu, move to the root folder like public_html file. Click on the wp-content file, you can see there are two files like plugin and themes file.

Now you should change the theme file name (like themes_deactivate). For doing this WordPress will revert to one of its default themes.

If your website is now loading fine, you can fix the problem by reinstalling the theme.

Method 5. Use Database Repair Functionality

Database repair functionality can help you to fix corrupted tables.

To do this, login to your Cpanel dashboard and navigate to the File Manager menu, move to the root folder like the public_html folder. Look for the wp-config.php file, open it using a text editor.

error establishing database connection

And using FTP client

Open it a text editor

Scroll to the bottom of the file and look for the line:

define('WP_ALLOW_REPAIR', true);

Then save the changes to wp-config.php,  you can access the WordPress database repair feature by visiting the following URL:

yourwebsite.com/wp-admin/maint/repair.php

Replace the yourwebsite.com placeholder with your own domain. You will see the following screen:

error establishing database connection

When you are ready to click on the Repair Database Button and let WordPress do its thing.

Once the repair is over, you’ll see a success message, the error establishing a database connection in the WordPress message is gone.

Method 6.Reinstall WordPress Core Files

The easiest way to fix this to replace all your core files with a clean and latest version of WordPress. To do this download the latest version of WordPress software from wordpress.org.

Once you get the file, which is a zip file. Unzip it and delete the wp-content folder in the downloaded files.

Using your FTP Client

Next, you need to connect to your WordPress website using an FTP client. Go to your site’s root folder. It is the folder that has wp-admin,wp-content, and wp-includes folders inside it.

In the left column open the WordPress folder on your computer, which is downloaded earlier.

Then you need to select wp-admin and wp-includes folders and then right-click and select the ‘Upload’ option.

Your FTP client will transfer those folders to your server. It will ask you whether you would like to overwrite the files.

Then select the ‘Overwrite’ option and select ‘Always use this action’.

Your FTP client will replace your older WordPress files with newer and fresh copies.

Using your cPanel

To upload the clean and latest files, use your cPanel dashboard and navigate to your site’s root folder public_html folder. Next, upload all the WordPress files from your computer.

This process might take a while and you will need to say yes when you ask if you want to overwrite the files on your server. This process will also overwrite the .htaccess file.

When the process is done, go ahead and try to access your website once more.

Conclusion

There are a lot of issues that can cause errors when connecting to your database. I hope you can fix the errors by applying those processes. If you are still seeing the error: “Error establishing a database connection”, you should contact your hosting provider.

You may also want to see another guide on How to fix Error Establishing a Database Connection in WordPress, How to Fix “Briefly Unavailable for Scheduled Maintenance” Error in WordPress, and How to Fix 500 Internal Server Error in Your WordPress

If you like this article, please share this with your friends and more people.

Leave a Reply