How to Debug the Error Establishing a Database Connection in WordPress

The “error establishing a database connection” is undoubtedly one of the common errors that WordPress users might face. It is closely related to the white screen of death (WSOD). This error indicates that your website is no longer interacting with or having access to your WordPress database. As a result, your entire website is unavailable.

Further, you should fix this problem immediately since it affects your sales, traffic, and analytics.

This article will explain what “Error Establishing a Database Connection” means, how to debug it in WordPress, and the likely reasons. However, before we can proceed with the first step in solving this issue, we must first understand what this database error means.

What Does “Error Establishing a Database Connection” Mean?

The “Error Establishing a Database Connection” warning displays when WordPress is unable to connect to the database.

WordPress was originally written in PHP and used MySQL as its database system. When someone visits your site, certain PHP commands will retrieve information from the database and show it on the visitor’s browser.

As there is no data to retrieve and show in the browser, the WordPress “Error establishing a database connection” notice will appear on a blank page.

Fortunately, this error notification does not appear on the front end as long as your website’s cache is available. However, this database connection error prevents you from accessing the WordPress dashboard. With that in mind, it’s important to figure out what’s causing this error and fix it.

What Are the Most Common Causes of This Error?

So, what causes this? Here are a few common causes. And don’t worry; we’ll go through each of them in further detail so you can figure out how to fix them. This problem is usually resolved in less than 15 minutes.

  • The most common error is that your database login credentials are incorrect. Your WordPress site connects to its MySQL database using a different login.
  • Your database has been corrupt. Since there are so many moving parts with themes and plugins, and users continuously remove and install them, databases can become damaged. This might be due to a missing or damaged table or because you accidentally deleted some information.
  • Your WordPress installation may include corrupt files. This can also occur as a result of hackers.
  • Problems with your database server. Several things might go wrong on the web host’s end, such as a traffic spike overflowing the database or becoming unusable due to too many concurrent connections.
  • An increase in traffic. Depending on the web host you choose, your server may be unable to manage many concurrent database connections. An increase in traffic might cause temporary issues with the database.

How To Debug The “Error Establishing a Database Connection”?

Before debugging the error, we recommend taking a WordPress site backup. Many of the suggestions below require modifying data in your database. As a result, you don’t want to make matters worse. No matter how tech-savvy you believe you are, you should always backup your WordPress site before trying to modify it.

To backup, your files and database, use a popular WordPress backup plugin like UpdraftPlus or BackupBuddy.

Once your backup has been completed, it’s time to look into your database connection error.

1. Check Your WordPress Database Credentials

The most common cause of a database connection error in WordPress is incorrect database credentials. This is most probable if you just transferred your WordPress site to a new host.

The wp-config.php file saves your WordPress database credentials. It is the WordPress configuration file that includes crucial WordPress settings, including database information.

Look for the following lines in the wp-config.php file.

// ** MySQL settings - You can get this info from your web host ** // /** The name of the database for WordPress */ define( 'DB_NAME', 'database_name_here' ); /** MySQL database username */ define( 'DB_USER', 'username_here' ); /** MySQL database password */ define( 'DB_PASSWORD', 'password_here' ); /** MySQL hostname */ define('DB_HOST', 'localhost');

You must ensure that the database name, username, password, and host are all correct.

You may confirm this information from the dashboard of your WordPress hosting account.

We use Bluehost as an example for this article, but the process is similar for other hosting control panels.

Go to your hosting account and choose MySQL databases from the database section.

Bluehost MySQL Database

This will send you to the database management page in your hosting dashboard. From here, you will see your database name and username.

Database Name and Username

Below that, you will see database users and links to change the user password.

Change Database User Password

Once confirmed your database name, username, and password, you can update that information in your wp-config.php file if needed.

Then, return to your website to verify whether the database connection error has been resolved.

If you can still see the error, something else is incorrect. Continue reading for further troubleshooting tips.

2. Repair WordPress Database

A corrupted database is another cause of the “Error Establishing a Database Connection” in WordPress. This might occur as a result of data failure from plugins or themes that are often installed or deleted.

If your WordPress dashboard warns, “One or more database tables are unavailable. The database may need to be repaired,” you have a corrupted database.

Fortunately, you can simply solve this problem by adding the below code to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Once you have added the code, go to http://www.example.com/wp-admin/maint/repair.php in your browser.

From here, you will see two options for repairing your database; select the one that best meets your needs.

Options for Repair WordPress Database

After the repair process is completed, check your WordPress site and see whether everything is back to normal. If so, go straight to your wp-config.php file and remove the function. This avoids website misuse since anybody may view the repair page without logging in.

3. Fix the Corrupted Files

Corrupted files, in addition to corrupted databases, might also be the cause of “Error Establishing a Database Connection.”

One of the causes of file corruption is a defective theme or plugin. Checking each one separately will help you determine which element is the problem. If the error occurs after activating a new theme, your theme may be the source of the problem.

To fix this, go to Appearance » Themes from your WordPress dashboard and then switch to a new theme.

WordPress Themes

If changing the theme does not resolve the issue, it might be caused by a faulty plugin. In this situation, consider deactivating all of your plugins and then re-enabling them one at a time to identify the cause.

Alternatively, you may disable all WordPress plugins by renaming the plugins folder in your hosting control panel.

By doing this, all of your plugins will be disabled at once. If the error goes away after renaming the plugins folder, it indicates that one of the plugins is the issue.

Once you’ve got access to your site’s backend, rename the plugins folder to its original name. By doing this, all of the WordPress plugins will remain inactive, but you may activate them one by one from the admin area.

If none of the above tips work, try re-uploading the core files to your WordPress site. To do so, install a new version of WordPress.

Download WordPress

When the download is finished, unzip the package. Delete the wp-content folder as well as the wp-config-sample.php file from the folder.

After deleting them, open an FTP program and upload the remaining WordPress files to your root folder. This should now safely replace any corrupted files on your server.

4. Check if Your Database Server is Down

When your MySQL server is down, the “Error Establishing a Database Connection” warning may show on your WordPress site. While this error can be caused by a number of factors, too many connections are usually the cause.

A database server can only have a specific number of clients connected at one time.

When it exceeds that limit, it could be unable to handle more connections. As a result, we recommend that you contact your hosting provider to get your MySQL server checked.

If you host many sites on the same server, you may also check if your MySQL server is down on those sites. Meanwhile, if you only host one site, use PHP to connect to MySQL and see if your WordPress database user has enough permissions.

Go to your hosting control panel and open the public_html folder. Create a new PHP file and give it whatever name you want. In this example, we’ll call the file dbconnection.php.

Then paste the following code into it:

$link = mysqli_connect('localhost', 'username', 'password'); if (!$link) { die('Could not connect: ' . mysqli_error()); } echo 'Connected successfully'; mysqli_close($link);

Remember to replace the database password and username with your credentials. Finally, save this file to your website and view it using a web browser.

If it connects successfully, it means your user has sufficient permissions and that something else is causing the error.

5. Check Your Database Host Information

If your database name, username, and password are correct, double-check that you are using the right database host information.

Most WordPress hosting company uses localhost as your database host. However, some managed WordPress hosting providers host databases on different servers. In such a situation, your database host information will not be localhost.

You must contact your WordPress hosting company to check your database host details.

6. Create a New Database

If none of the ways above show any results, your database may be corrupted and no longer usable. In this case, you must use your hosting control panel to set up a new MySQL database and user.

Once a new MySQL database has been created, restore your website backup to avoid losing any vital website data.

Conclusion

It might be frustrating to see the “Error Establishing a Database Connection” on the WordPress site. The website will not load, resulting in lost traffic and customers.

This database error occurs when the WordPress website is unable to create a connection to the server. It might be caused by different factors, including incorrect database login credentials in the wp-config.php file, damaged files, or server overload.

Fortunately, there are several ways to debug this error:

  • Check Your Database Credentials – match the database credentials in your wp-config.php file with the ones in your MySQL Database.
  • Repair Database – by adding define(‘WP_ALLOW_REPAIR’, true); function in the wp-config.php file.
  • Fix the Corrupted Files – by deactivating corrupt themes or plugins through your WordPress dashboard or by downloading the most recent version of WordPress and re-uploading your core files.
  • Check Your MySQL Server – create a new file with the mysqli_connect function to see if your database user has enough permissions.
  • Check Database Host Information – double-check that you are using the right database host information.
  • Create a new database – Create a fresh MySQL database from scratch, then import a backup.

We hope this article has helped you debug the “Error Establishing a Database Connection” in WordPress. Identify the likely reason for this error, then follow the steps to fix it.

You may also want to see our following articles:

Lastly, follow us on Twitter for regular updates on new articles.