How to fix “Error establishing a database connection” in WordPress

A database is software that organizes, stores, and retrieves data. It stores all the content of a site, from WordPress posts and pages to login details and plugin settings, to be used by the (content management system).

However, sometimes a WordPress site fails to connect to its database. Therefore, the message “Error Establishing a Database Connection” may appear on your website. If you have no idea what this means, don’t panic, the problem is quite common.

In a nutshell, this database connection error message indicates that your site was unable to retrieve the information to render the page. If left unattended, a site will become inaccessible and lose traffic, damaging your reputation in the meantime.

This article will help you fix this WordPress error and explain what the possible causes of it are. However, before we can take the first step in fixing this problem, we need to know exactly what the error means.

What exactly is “Error Establishing a Database Connection”?

WordPress is written in and uses as a database system. When someone visits your site, specific PHP commands will fetch your site information from the database which will then be displayed in the user’s browser.

“Error Establishing a Database Connection” message appears when PHP cannot access the WordPress database. Since there is no data to retrieve and display in the browser, the message appears on a blank page.

Fortunately, this error message does not appear on the front-end as long as it is available. However, this database error blocks your access to the WordPress dashboard. Therefore, it is essential to determine the cause of this error and fix it.

Keep reading the next section to find out various reasons that can trigger this database error.

Reasons behind “Error Establishing a Database Connection”

Some of the possible reasons that can cause “Error Establishing a Database Connection” in WordPress are:

  • Database access credentials are incorrect. Please note that the WordPress database uses different login information than your hosting panel. If there is any change to your database username and password, the old login information stored in your WordPress files will be invalid.
  • corrupted database. Issues like plugin crashes and incompatible themes can cause database table corruption or data loss. When any information in the database is deleted or damaged, it will result in an error.
  • Database server error. Sometimes the problem comes from your web hosting side. For example, a sudden spike in traffic or permission issues can make your database unresponsive. This usually happens in a shared hosting environment where multiple websites use the same resources.
  • Core file corruption. WordPress core files define how your site works. Many reasons can be behind corrupted files, from faulty plugins and themes to problems transferring via a . When incorrectly modified, kernel files can also become corrupted and cause database failures.
See also  Wall of Fame

If you see the following window on the back-end of your WordPress site, the issue is likely due to invalid login credentials.

However, if a different error message appears, then the cause could be a corrupted database or spike in traffic. Luckily, fixing this database error only takes a few steps. The following section explains different methods to solve this problem.

How to fix WordPress “Error establishing a database connection”?

Here are the five most common solutions that you can use to try to fix the WordPress error associated with a failure to establish the connection with the database. But before trying them, we recommend downloading a backup of your data. You can find more information on how to do it.

Quick methods to fix the error

  1. Verify login credentials
  2. Repair WordPress database
  3. Repair corrupt files
  4. Check your database server
  5. Create a new database

Method 1: Verify login credentials

One of the most common reasons why establishing a database connection fails is due to incorrect login credentials. This tends to happen after a website owner migrates to a different hosting provider. If both the frontend and backend of your site show the same “Error establishing a database connection” warning, this method might solve the problem.

First things first, you need to check if the database login information matches what your site stores in the file wp-config.php.

To access the file wp configuse a client like or the file manager of WordPress in your hosting control panel.

If you use the file managernavigate to the folder /public_html/right click on the file wp-config.php and select Edit.

Next, find and take note of:

  1. DB_NAME: database name
  2. DB_USER: Username
  3. DB_PASSWORD: password to login
  4. DB_HOST: database host

After this, navigate to MySQL databases and look for the name of your current database in the section List of current MySQL databases and users. There, you will find the database name, database user, and host.

If the login details in your file wp-config.php are different from the configuration of the MySQL databasecombine them by modifying the file wp-config.php.

See also  How to Configure Zoho Mail - Complete and Detailed Guide

Edit these lines:

define(‘DB_NAME’, ‘fill_this_with_the_current_name_of_your_database_name’); define(‘DB_USER’, ‘fill_this_with_your_current_username’); define(‘DB_HOST’, ‘fill_this_with_your_current_localhost’);

Once you’ve edited the file, refresh your WordPress site to see if the error is gone.

If the error persists, it is possible that the value of DB_PASSWORD still wrong. To fix this problem, you need to reset your MySQL password.

Just go to the menu MySQL databasesclick on your current database and select Change Password. After entering the new password, press the button To update.

If the database name, username, and host are correct, you may want to check your database host information. With WordPress, web hosting companies tend to use localhost as a database. While managed WordPress hosting companies can use separate servers.

If this is the case for you, you will need to contact your web hosting company to confirm your database information.

Note: If you’ve recently changed hosts, be sure to double-check the host details. DB_HOST. In most cases, localhost it won’t change, but some web servers use a different IP address or URL.

Method 2: Repair WordPress Database

Your database could be corrupted if the WordPress admin panel displays the error message “One or more database tables are unavailable. The database may need to be repaired» (“One or more database tables are unavailable. The database may need to be repaired.”) This error will only appear on the back-end, while on the front-end you will see the error message establishing a database connection.

Fortunately, you can easily repair the database by inserting the following function in the file wp-config.php:

define(‘WP_ALLOW_REPAIR’, true);

Be sure to place the function just before the “That’s all, stop editing! Happy blogging.”

Once you’ve saved your changes, go to http://www.yoursite.com/wp-admin/maint/repair.php. Here you will have two options to repair your database.

Then select the option repair database either Repair and optimize the database. Choose the one that best suits your needs.

Once the database is repaired, don’t forget to go back to your file wp-config.php and remove the function immediately. This is important to prevent any abuse of the website, as anyone can access the repair page URL without logging in.

Method 3: Repair damaged files

Another possible reason for error establishing a connection to the database it could be a damaged theme or plugin.

See also  Cheap Domains - Register Your Domain For Only €0.99

Your theme may be the problem if the error occurs after activating a new one. To solve the problem, simply navigate to Appearance -> Theme in your Admin Panel and then activate another theme from the available options.

Alternatively, you can disable all your plugins if changing the theme doesn’t solve the problem. After that, enable each plugin one by one to find the culprit.

Yes, disable your WordPress theme or WordPress plugins by renaming the corresponding folders from the file manager in your hosting control panel.

go to file manager and open the folder wp_content located in the directory public_html. Then change the name of the currently used theme or the folder of plugin. This will disable your theme or all your plugins at once.

Remember not to disable WordPress themes and WordPress plugins, so you can discover the root of the problem.

For example, first rename the folder from plugin and then update your site. If the error goes away and you can access your WordPress dashboard, then you can be sure that one of the plugins is the problem.

But how do you know the exact WordPress plugin causing the problem?

Well, after disabling them all, it restores the plugins folder with its original name. This way, all WordPress plugins will still be inactive, but you can adjust the settings from the admin area.

After that, activate the WordPress plugins one by one, update your site and continue doing so until you find the one causing the error establishing a database connection.

Pro tip: Don’t forget to remove the WordPress theme or plugin causing the error.

If that doesn’t work, you can also try reloading the core WordPress files. To do this, download a new version of WordPress and unzip the package on your computer. Inside the folder, delete the file config.php and the folder wp-content. After removing them, open a client FTP and upload the remaining files to your folder root of WordPress.

This should safely overwrite all files on your live server.

Method 4: Check your server…

Loading Facebook Comments ...
Loading Disqus Comments ...