How to Fix Error Establishing a Database Connection in WordPress

Jan 22, 2020 | Errors and Troubleshooting, WordPress

When you use WordPress for your website, it is very common to encounter some errors or warnings if you don’t run or secure the website properly. But it is not hard to fix these errors if you know about it. One fine morning you encounter an error establishing a database connection in WordPress. It may frighten you, but it not hard to fix the database connection error in WordPress. It is just a fatal error that makes the WordPress website unreachable to the user.

The error establishing a database connection in WordPress does not create much damage to your website. This database connection error is quite common. Let’s take a look at what is the error establishing database connection in WordPress, reasons that cause the error, and how to fix it.

What is the Error Establishing a Database Connection in WordPress?

All information on your WordPress website, such as publication date, page data, Meta information, plug-in settings, login information, is stored in the MySQL database. Your plugins, theme, images, and core files, such as wp-login.php, index.php, are not stored in that database. The database shows all the stored data when someone visits the website, and if the database does not connect with the website, the browser displays the error establishing a database connection in WordPress. It not only shows you a blank page, but it also prevents you from accessing the WordPress dashboard. 

Reasons that Cause the Error Establishing a Database Connection

You need to understand why you are facing the error, what could be the reason for this error. Here are some of the reasons that cause the error establishing a database connection.

  • Incorrect database login attempts can cause the error because you won’t be able to access your database without the right credentials.
  • The corrupted database can be the reason for the connection error; this happens due to malware attack, deletion of database files by accident, plugin or theme vulnerability, and interruption in data transfer.
  • Corrupted WordPress files can also cause an error while connecting the database. Failed plugin, theme, or WordPress update can be the reason behind it. It can also be caused due to hacker’s attacks.
  • Hike in traffic can slow down the database and be the reason behind the error.
  • Slow or no database server response is also one of the reasons to cause the connection error.


How to fix Error Establishing Database Connection in WordPress

Before fixing the error establishing a database connection in WordPress, take a backup of your website. Now let’s discuss how to fix error establishing a database connection issue.

1. Check your WordPress Database Login Credentials

First, check the WordPress database login credentials. This is by far the most common reason for a database connection error. Especially after migrating the WordPress website to the new hosting service. The login information for your WordPress website is stored in the wp-config.php file, which is usually located in the root directory of your WordPress website. The login credentials have four main information that needs to be correct for establishing a successful connection.

// ** MySQL settings ** //

 /** The name of the database for WordPress */

define( ‘DB_NAME’, ‘your_database_name’ );

 /** MySQL database username */

define( ‘DB_USER’, ‘your_database_username’ );

 /** MySQL database password */

define( ‘DB_PASSWORD’, ‘your_database_password’);

 /** MySQL hostname */

define( ‘DB_HOST’, ‘localhost’ );

You can get this information from your web host. And you can confirm the information from your hosting account dashboard or suing SFTP. If the information found incorrect, then correct it to fix the error.

 2. Repair Corrupted WordPress Database

If you face an error, “One or more database tables are unavailable. The database may need to be repaired”, then it means your WordPress database is corrupted and need to be fixed. Normally you see this error when you try login your WordPress dashboard, it does not show on the frontend of the website. The WordPress database is quite complex, and many small things should work very well together to avoid problems.

WordPress has a database repair option that you can activate. Just add the following code at the end of the wp-config.php file.

define(‘WP_ALLOW_REPAIR’, true);

After that, you can access the WordPress database repair mode in the browser by entering the following URL.

Replace your site with the name of your WordPress website and open the link in the browser.

WordPress repair mode will open, and you will see two options repair database and repair and optimize database either of the options can repair the corrupt database. After the repair, makes sure to remove the code you entered in the wp-config.php file.

3. Repair WordPress Corrupted Files

You need to fix the corrupted files carefully because you are going to deal with the core files. To fix the WordPress corrupted files, you need to take the backup first then download the fresh copy of WordPress. After that, unzip the WordPress files on your computer. Now delete the wp-content folder along with wp-config.php file, and upload the remaining files to the WordPress root folder using the file manager or FTP.

 This will remove all the corrupted files from your WordPress website. Now reload your website, and if you still see the error, then try cleaning the cache of the web browser.

4. Check if the WordPress Database Server is Down

If everything works fine and you still can’t connect to your WordPress database, then your WordPress database server may be down. This could happen due to the hike in traffic on your database server. You may be using shared hosting for your WordPress website. All you can do is contact your WordPress hosting provider and ask them if your database server is responsive or not.


We hope this article will help you fix the error establishing a database connection in WordPress. You can also avoid this WordPress error by choosing a reliable and quality hosting provider for your website. And don’t forget to always keep the backup of your website. If you still have a problem fixing the issues, then we are here to help you out.