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

Error establishing a database connection message is a frustrating WordPress error in which instead of opening your site, you get an error like the screenshot shown below:

fix error establishing a database connection in wordpress

If you are reading this post right now, then maybe you too might be facing this error right now, if not then you are lucky.

Read More: WordPress Custom Fields : Tips, Tricks, and Benefit

If you are not getting this message right now, then as a WordPress user it is important for you to know how to fix this.

In this post, you will learn about three things:

  1. What does “Error Establishing A Database Connection” mean?
  2. Some most common reasons that cause this error.
  3. How to fix this error in WordPress.

What does “Error Establishing A Database Connection” mean?

If you don’t know how WordPress works, then know that a WordPress website has two parts:

  • Your files: This contains the WordPress software, your themes, media uploads, etc.
  • Your database: This is where the actual content of your posts and pages is stored (as well as other important information)

When a visitor lands on your website, PHP files go to your database and figure out exactly what information to display.

If the PHP files do not have a working connection with the database, then your WordPress site will not be able to display the information that it should. This is why you get the message “Error Establishing A Database Connection”.

What are the causes of “Error Establishing A Database Connection”?

If you get this error then there can be many reasons for it. In most cases, these can be:

  • Corrupt database
  • Incorrect database configuration in your wp-config.php file.
  • WordPress core files getting corrupted.
  • Problems with your web hosts.

Because you can never be sure what exactly is causing the error, you have to take a diagnostic approach. In this, you will have to check and fix the problem by following a series one by one.

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

Potential Fix 1: Check if you can still access your WordPress dashboard.

If you see this message on the front end of your site, the first thing you should check is whether you are able to access your WordPress dashboard. If you get the same error even after accessing wp-admin then skip this step and follow the next step.

If you get a different message, such as “Database may need to repair”, continue to this section to repair your WordPress database.

Step 1: Turn on the repair utility in the wp-config.php file.

To get started, you’ll need to edit your site’s wp-config.php file. You can do this in one of two ways:

  • By connecting your site to an FTP client. (Filezilla is a great FTP program)
  • By using cPanel’s built-in file manager.
file manager

Your wp-config.php file will be in the root folder of your WordPress site. This will be the same folder in which you will also find the wp-admin and wp-content folders.

edit wp config

Once you have opened your wp-config.php file for editing, add this line of code to the bottom of your file:

define( ‘WP_ALLOW_REPAIR, true );

Before continuing, do not forget to click on Save to save the changes.

Step 2: Automatic Database Repair

Once the code snippet is added, you can access the WordPress database repair tool by going to the URL below:

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

On this page, you have to click on the Repair Database button.

repair database wordpress

Once the tool completes the repair of your database, your site should hopefully work. If it does, all you need to do is remove the code you just added from the wp-config.php file.

If this does not work for your site, proceed to the next step.

Potential Fix 2: Check the login credentials of your database.

If your database is not corrupt, the next thing you need to check is whether your database login credentials are correct or not.

The credentials for your database are also defined in the wp-config.php file.

fix error establishing a database connection wordpress 10 1

If this information does not match the actual database credentials that you created in your host dashboard, WordPress will not be able to access your database.

You can use the mysql_connect() function to check whether your database credentials are working or not.

Using a text editor like Notepad, create a file named databsecheck.php.

After that add this code snippet to the file:

<?php
$link = mysql_connect(‘localhost,mysql_user,mysql_password’);
if (!$link) {
die(Could not connect: ‘ . mysql_error());
}
echoConnected successfully’;
mysql_close($link);
?>

Don’t forget to replace “mysql_user” and “mysql_password” with the actual username and password that you used in your wp-config.php file.

Then upload this file to your website via cPanel or FTP and then go to the URL given below:

YOURDOMAIN.com/databasecheck.php.

If you get the “Connected Successfully” message, your database credentials are working and you can go to the next step by skipping it.

But if you receive a message like the one shown below, there is a problem with your database credentials.

The easiest way to fix this problem is to create new credentials for your database.

Step 1: Go to MySQL databases in cPanel.

I assume your host uses cPanel, you can create a new database user by going to MySQL Databases.

mysql database

Scroll down to MySQL users, and look for the Add New User box.

New one:

  • Username
  • Password

Please enter.

mysql users

Remember these details as you will need them now.

Step 3: Add a new user to the database.

Once you have created a new user, you will need to go to the Add a User to a Database option and add a new user to the database of your WordPress site.

Add user to a database

Don’t forget to give ALL PRIVILEGES to the user on the next screen.

fix error establishing a database connection wordpress 9 1

Step 4: Update the wp-config.php file with the new database user.

To complete this task you need to open the wp-config.php file again and update the DB_USER  and  DB_PASSWORD fields for the new user.

Once you save these changes, your site should hopefully be working again.

Potential Fix 3: Re-upload WordPress core files.

If none of the above methods fix this error, your WordPress core files may also be corrupt, in such a case you will have to re-upload them.

For this work, I recommend you use the FTP program instead of File Manager.

Note: Make sure that before doing all this, you take a complete backup of your site.

Download the latest version of WordPress from WordPress.org:

fix error establishing a database connection wordpress 11 1

Once the download is finished, extract the ZIP file and delete it:

  • wp-content folder.
  • wp-config-sample.php file.
delete

After this, upload all the remaining files to your WordPress site via FTP. Your FTP program will prompt you for duplicate files. When this happens,  do not forget to select the option to Overwrite duplicate files.

Potential Fix 4: Talk to your host.

If none of the above solutions work, then you might be dealing with a hosting issue. Some hosting-related problems that may occur in this regard:

Your database server is down:  Some hosts use a separate server for databases, which means your database could be down, even when the regular server is down.

Your database quota is exhausted: Some shared hosts put limits on your database usage. If your site crosses these limits, this is the reason why your database goes down.

To solve this problem you will have to talk to your host team.

final thoughts

By this point, your “Error Establishing A Database Connection” error should be gone and your site should be working again.

However, there may still be some issues that can cause this error. These issues are usually unique to your site and are therefore difficult to diagnose through such posts.

If you have tried all the solutions mentioned above and talked to your host but are still getting the “Error Establishing A Database Connection” error, you will need to hire a WordPress developer to further investigate and resolve the issue.

Share this post

Facebook
Twitter
LinkedIn
Picture of YouRankus

YouRankus

Drive real results with YouRankus’s professional SEO, WordPress, and digital marketing solutions. Reach your business goals with our personalized approach.

Leave a Comment