How to Fix the Error Establishing a Database Connection in WordPress (Step by Step)

How to Fix the Error Establishing a Database Connection in WordPress (Step by Step)

Do you encounter an error when establishing a database connection on your WordPress site? There can be multiple reasons behind this error. Sometimes, it appears unexpectedly and you may wonder what’s changed on your site to create this problem.

Wonder no longer! In this article, we’ll share how to fix the error establishing your database connection in WordPress by troubleshooting all the possible reasons.

Error Establishing a Database Connection – When and Why?

Normally, this error occurs because your website isn’t able to create a database connection. But the reason why it isn’t making a database connection can be different for everyone. Here are a few reasons:

  • Your web server isn’t responsive to your request.
  • The server is getting too many requests and it stopped responding.
  • Your database login credentials are wrong or outdated.
  • Your database is corrupted with malware.
  • Your database is corrupted due to a change in code or by an automatic WordPress update.

Most of the time you’ll get this error when there’s a problem with your web hosting server. Though there can be many other reasons as well. It can be difficult for you to troubleshoot because you don’t know why it’s happening on your site. We’ll walk you through, step-by-step, to find the issue and solve it.

Fixing the Error Establishing Database Connection

Before we start, you should create multiple backups of your database and store them in different locations. If you’re not able to track or fix this error, then you can go back to the main issue by uploading the backup and try again with a new approach.

We also recommend you keep your WordPress site, themes, and plugins up-to-date, so there is a slimmer chance you’ll see an error. You should also back up your WordPress site regularly, so you can restore it, if necessary.

Checking Your WordPress Admin Login Page

You need to check your WordPress admin login page to see what error is displayed there. If this error is similar to the error on front-end of your site, then go to the next step.

If there’s a different error on the /wp-admin/ page like “One or more database tables are unavailable. The database may need to be repaired”, then you’ll need to edit your wp-config.php file to add a line of code.

To edit wp-config.php, you can log into your WordPress site with an FTP client. You’ll need to add this code before the “That’s all, stop editing! Happy blogging” line in wp-config.php.

define('WP_ALLOW_REPAIR', true);

Make sure that you save this file properly.

After adding the code in the wp-config.php file, visit (replace with your website URL) to see the settings to repair your database.

You don’t need to be logged into your WordPress admin account to gain access to repair the database. If you add the code correctly in the wp-config.php file, then it’ll help you repair the database issue. Upon repairing, you can go ahead and remove the code from your wp-config.php file.

If the problem is still there, then you can read this guide further to look for another solution.

Editing Your WP-Config.php File to Find the Problem

You may know that the wp-config.php file is the main file that connects your WordPress website with your database. It has your database login credentials. If you’ve changed the root password or database user password, then you’ll also need to update these details in your wp-config.php file. If you don’t change the details in wp-config file, then you’ll encounter an error establishing your database connection.

You need to be sure that the login details for database are same in wp-config.php file as you set them for your WordPress database.

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

In this code, DB_Name represents your database name, DB_User is your username of the database, DB_Password is the database password, and DB_Host is the host value.

You can add DB_Host value as localhost if your WordPress hosting provider is BlueHost, HostGator, etc. For other hosting companies, you can either contact their support team or check this guide on WordPress configuration tricks to find other host values.

We also found out that if you are running your WordPress site on a local server like XAMPP or MAMP, then you can replace DB_Host value from localhost to IP to fix this error.

define('DB_HOST', '');

This IP can be different for online web hosting companies. You may also want to check out this guide on how to transfer WordPress from local server to a live site.

If your issue is still not resolved, then continue reading below to find other possible solutions to apply to the server end to fix the error.

Looking Into Your Web Hosting Server

Sometimes you’ll get a lot of traffic unexpectedly and your web hosting server stops responding to the requests. First, it will slow down your website and then display an error establishing a database connection to the users trying to visit your website. This happens mostly on shared or VPS hosting environments.

You can directly contact the support department of your web host and check this issue. For managed hosting companies like WPEngine and Siteground, the support team will quickly help you fix the error.

In case you want to troubleshoot this error yourself on the server end, then we recommend you check a few things on your MySQL server to track the issue.

First, you should try to access other websites on the same server and see if they are running fine or not. If other websites are also displaying the same error, then your MySQL server is corrupted.

When you do not have other websites on the same server for testing, then you’ll need to go to phpMyAdmin in your cPanel and connect to your database. Upon connecting, you’ll need to check if you have sufficient permission or not. Simply add a new file, testconnection.php, and add this code:

You’ll need to replace the username and password with your database login credentials. Once connected, it means that there’s no issue with permissions, and you can go ahead and check your wp-config.php file again to see if there’s any spelling mistakes in the username or password.

If your connection fails, then there’s a fair chance that your server has issues. However, it cannot be said that your MySQL server is down. It’s possible your database user doesn’t have sufficient permission to access the server.

You can contact your web hosting provider and ask them to give you permission. That way, you can check for errors and solve the issue.

There are still many other reasons that you might get this error and you’ll need to troubleshoot your website different ways. If your solution is different than the methods we described above, then please share it in the comments below. Others who are experiencing this error would benefit from your valuable advice!

We hope this guide helped you fix your error establishing a database connection in WordPress. You may also want to check out how to switch from one web host to another without downtime.

The post How to Fix the Error Establishing a Database Connection in WordPress (Step by Step) appeared first on IsItWP – WordPress Technology Lookup Tool.

What’s My SEO Score?

Enter the URL of any landing page or blog article and see how optimized it is for one keyword or phrase.

Wordpress Expert

Leave a Reply