Fix “Error Establishing A Database Connection” In WordPress 2024

To Fix “error establishing a database connection” first Get In Touch With Your Web Host Provider, Check If Your Plugin or Theme Files Haven't Been Corrupted, Check If Your Database Hasn't Been Corrupted. Check Your Database Connection Credentials.

Getting Error Establishing A Database Connection On WordPress While login or browsing your WordPress blog? This is because the WordPress site is unable to connect to the database to fetch the contents of your website. The database is where all the contents and information of your website is stored. If you are struggling with this error here are some effective solutions to fix the Error Establishing a Database Connection in WordPress.

Error establishing a database connection

WordPress is made up of a combination of code written in PHP and a database, typically running MySQL. PHP is required for running the functions on your WordPress site that allows posting comments, sharing files, retrieving pages, and so forth. WordPress also requires a database to store settings, posts, contributions, links to uploaded files and much more. If the connection to the database cannot be established, only an error message will be visible instead of your website.

Why is This Error Establishing a Database Connection?

This error simply means that WordPress can’t connect to your database to fetch and display the contents of your website. When you see this error on your website, one of the following can be the case:

  • The login credentials of your database are wrong: This is what happens when you change the login credentials of your database and forget to update them in the wp-config file.
  • Your database is corrupted: There are a lot of reasons why this could happen to your database. One of which is uploading a badly coded plugin. It can also happen due to an error in the database hosting server.
  • Your database server host is down: If you are hosting the database on your own servers, then you will have to contact your developer. But if your web hosting company is responsible for hosting the database, there’s nothing you can do other than call them and let them know that it’s down. Most web hosting services offer great support and will fix this problem within minutes.

In most cases, this error shows up either when the database is corrupted or the database login credentials aren’t valid. Both of which are easy to fix and won’t take more than a few minutes to fix.

Fix Error Establishing a Database Connection?

After understand What is Error Establishing a Database Connection And Reason Behind this issue Now let’s move to Troubleshoot part.

Before you start making any changes to your website, we would highly recommend you to make a backup or make sure you have a backup of your website. In the unlikely case that you break something, a backup will allow you to go back to the previous state.

When you try logging into your website dashboard, are you greeted with a message along the lines of “One or more database tables are unavailable” or “The database may need to be repaired?

If this is the message you see on the back-end, then it simply means that your database is corrupted. On the other hand, if you are seeing the same “Error Establishing A Database Connection.” message, then you can move on to the next section.

Fix Corrupted Database

Fixing a corrupted database is easy and doesn’t take more than a few minutes. Here’s how you can do it:

  • First, Login to cPanel,
  • Open the File Manager app
  • Navigate to the WordPress installation folder.
  • Now, find the wp-config.php file,
  • select the edit option and add the following line of code at the end of it:

define(‘WP_ALLOW_REPAIR’, true);

This line of code will do is it will allow you to enter the database repair mode for your WordPress website.

To start the repair process, visit the following page in your browser:


WordPress Database Repair

On this repair page, you can either choose repair or repair and optimize. We recommend you to go with repair and optimize, but you should note that it takes more time than just the repair function.

One thing you need to note is that this page is accessible by everyone. You don’t need to login to visit this page and use the repair functionality. So, to make sure the users of your website can’t use this page, you will have to remove the line of code you added to your wp-config.php file once the process is over.

Check Incorrect Database Login Information

WP-Config.php is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect your database. If you changed your root password or the database user password, then you will need to change this file as well. To do this, you will have to log in to your cPanel account and open the File Manager app. Then navigate to the installation directory of your WordPress site. Now, view or edit the wp-config.php file to inspect the code.

You should see the login credentials used by WordPress to connect to your database. It should look something like this:

define(‘DB_NAME’, ‘database-name-here’);
define(‘DB_USER’, ‘database-username-here’);
define(‘DB_PASSWORD’, ‘database-password-here’);
define(‘DB_HOST’, ‘localhost’);

The above four lines contain your database name, username, password and the hostname of the database.

If you find any of these values in incorrect, you should change them with the correct values.

If you don’t know the name of your database, you can find it with PHPMyAdmin. It’s a free application that comes with all hosting solutions.

To use it, you will have to log in to PHPMyAdmin from your cPanel. Once you have logged into the PHPMyAdmin dashboard, you will see a list of databases. One of these databases should have the same name as the value right next to “DB_NAME” in your wp-config.php file. Click the Browse link for the database that has the same name.

Now, you will need to verify if this is the correct database that contains your website’s information. To do this, click the Browse link right next to the “wp_options” table.

If in this table, you can see the name of your website and URL, then it means this is the correct database. If this is not the case, you will have to do the same thing for all the other databases to find the correct database. Once you find the correct database, change the name of your database in the wp-config file with the name of this correct database.

If the name of the database is already correct, then the username and password can be the problem. To check if you are using the correct username and password, create a new file in the installation directory of WordPress and name it “db-check.php” and copy the following code to it:

$test = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$test) {
die(‘MySQL Error: ‘ . mysql_error());
echo ‘Database connection is working properly!’;

Now, visit the following URL:

If you see an error message, then it means the username or password is incorrect. To fix this, you can create a new username and password with the MySQL Databases app in cPanel.

To do this, open up the MySQL Databases app in cPanel and click the Add New User link. All you have to do now is choose a good username and a strong password, and click the Create User button. These new database credentials are what you will now use.

Now, scroll down to the Add User To Database section, select the username of the user you just created and the WordPress database, and click the Add button. Doing this will add this new user that you just created to your WordPress site’s database.

Now that you have a new user, you will have to update the wp-config.php file with the new database login information.

To do this, open the File Manager app in cPanel, navigate to the WordPress installation directory and edit the wp-config.php file. In the wp-config.php file, change the value next to ‘DB_USER’ with the new username and the value next to ‘DB_PASSWORD’ with the new password.

This should fix the problem if it was due to incorrect database login credentials.

If your website is still showing the same error message, move on to the next section:

Still, Seeing The Same Error Message?

If this is the case, then most probably it’s a problem related to your Web Host’s database servers. The only thing you can do in this case is to contact your Web Hosting provider and ask them to fix this problem.

If you have tried all the steps in this article, you shouldn’t hesitate to contact your Web Hosting provider. Because a lot of times this error can be a result some recent update from your Web Host’s side.

Most web hosts offer 24/7 support and are really responsive. If you contact your web hosting service, they will help you find the issue and fix it if it’s on their end. If your web hosting service suggests it’s an error on your side, then you should hire a Web developer to fix the problem.

Restore the Previous Backup

Alternatively, you can also try reverting your site to a previous backup if you are sure you won’t lose any new data like your latest posts.

Did these solutions help to fix Error Establishing A Database Connection in WordPress? Let us know in the comments below.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More