Error Establishing a Database Connection in WordPress Fix

647 0
Error Establishing a Database Connection in WordPress Fix

Error establishing a database connection simply means your website is not able to connect to the database. If you manage a WordPress website, and you are facing the same error, it could be awfully frustrating, especially if you are new to WordPress and does not know what to do and how to do.

It is very much clear that there is a database connection related issue as the error suggests. There could be many reasons behind this error. Sometimes, it takes not more than 2 minutes to repair but it could also waste your 20 minutes.

In this article, I will show you how to fix the error establishing a database connection in WordPress by going through the list of solutions. But before you make any changes in the database, make sure you have all the sufficient backups including website and database.

Why do we get this error?

There could be various reasons. It could be that your database login credentials are wrong. It could be that the database of your website is corrupted. Let’s have a look at how to go about troubleshooting this problem.

Solution 1: Check Database Credentials

First of all, open the root directory of your website and look for the file wp-config.php and look for the following lines and check whether the database name, database username and password are correct or not.

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', YourDatabaseName');
/** MySQL database username */
define('DB_USER', 'Username');
/** MySQL database password */
define('DB_PASSWORD', 'YourPassword');

It should work immediately once you enter and save the correct login credentials. If you are still facing the issue, you need to move further and dive into some of the advanced troubleshooting.

WP Engine hosting

Solution 2: Check at Backend/Admin End?

Enter your website URL and then /wp-admin. e.g., If you are getting the same “error establishing database connection” on both front-end and the backend of the website, you need to repair the database.

Repair the website database

You can repair the database by adding the following line in your wp-config.php.

<predefine('WP_ALLOW_REPAIR', true);

Once you update the


file, you can repair your database by visiting the following page.

Note: Put your domain name in place of

The user does not have to be logged in to use this repair functionality. So make sure you remove the define(‘WP_ALLOW_REPAIR’, true); once you are done with the database repair.

If this repair does not fix the problem, check the next troubleshooter.

Solution 3: Check The wp-config.php File

It is certainly the most important file in entire WordPress. It has got all the details to connect your WordPress website to the database. So you need to look for some of the key configurations. You need to cross check whether configuration in the wp-config.php is right or not. Once again check the following lines including DB_HOST:

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

Remember one thing, DB_HOST value in most of the cases is localhost. But it is not necessary. Depending upon the host the value might be different.

You can try fixing this issue by putting the IP to DB_HOST. Somewhat like this:

define('DB_HOST', '');

It can also solve your problem but the IP for different web hosts services are different. So if you are thinking of using IP address, make sure you contact your host and confirm the IP before you proceed.

Solution 4: Check MySQL Server

You can check some other sites using your MySQL server database. If those websites still showing the database error, there might be an issue with your MySQL Server, and you need to contact your host to repair it.

If you do not have any other website to test, simply go to your cPanel and try accessing phpMyAdmin and connect. If you get connected to your phpMyAdmin, means no problem with your MySQL Server.

Now you need to verify whether the user defined for your database has sufficient privilege to access the database or not. To check that you need to create a new file called testconnection.php inside the root directory of your website. Once you are done with creating file, just copy and paste the following lines of code into testconnection.php:

<?php $link = mysql_connect('localhost', 'root', 'password'); if (!$link) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully'; mysql_close($link); ?>

Make sure you are using the right username and the password. If you are connected successfully, the user must have sufficient permission to access the database. Now, go to your wp-config.php and make sure database credentials are correct, similar to what you have typed in

If you go to phpMyAdmin and still cannot connect to the database, it means that there might be some issue with your server.

In case you get an error #1045 – Access denied for user ‘foo’@’%’ (using password: YES). I would advise you to contact your host to fix the error.

Useful Resources:

Fix 504 Gateway Timeout Nginx Error
Fix 502 Bad Gateway Nginx Error

I hope this post will be useful to get rid of this error. Do you have another method of fixing this error? Share with us in the comment section below or email me at [email protected].

In this article

Join the Conversation