How to fix error establishing database connection in wordpress

Reading Time: 2 minutes

If you open your pc, type your websites URL, and gets the message: “Error establishing a database connection.”

Now what? What does this message mean? Don’t worry. We are here to help you. In this article, we will discuss how to fix Error establishing a database connection in wordpress.

What is Error Establishing a Database Connection?

The Error establishing a database connection means that for some reason the PHP code is not able to connect to your MySQL database to retrieve the information it needs to form that page.

Reasons for getting this error?

It may be due to your database login credentials are wrong or have been changed, due to your database server is unresponsive or due to the database has been corrupted.

The solution of your problem is as follows:

Step 1:

Go to wp-admin and check you will get the same error both pages then proceed to the next step. If it shows, “One or more database tables are unavailable. The database may need to be repaired”, then first repair database. Add the following line in the wp-config.php file. Add it before ‘That’s all, stop editing! Happy blogging’ line.

define('WP_ALLOW_REPAIR', true);

Now you can see the settings by visiting this page:

Select any of option from above. After repairing and optimizing your database, make sure to remove this the line of code from your wp-config.php.

If this step did not fix the problem then go to next step

Step 2:

wp-config.php is most important file in WordPress first, you should always check is in your wp-config.php file is the matching.

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

Sometime the problem is fix by replacing localhost with the IP.

For example,

define('DB_HOST', '');

IP’s will differ for online web hosting services.

If everything is correct here, then there is something wrong from the server side.

Step 3:

Usually, you will notice this Error establishing database connection because your host server just cannot handle the load. There may be a problem in MySQL. So next step is Create a new file named testconnection.php and paste the following code in it:

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

Make sure that you have to change the username and password. If the connected successfully, then it means that your user has enough permission, and there is something else that is incorrect. Go back to the wp-config file and check that everything there is correct.

If you cannot connect to the database by going to phpMyAdmin, then you know it is something with your server.

If it shows #1045 – Access denied for user ‘foo’@’%’ (using password: YES) then you must communicate to your host to get them to fix it.

Note: Solutions that Worked for Others: It may not work for you. So first take backup and try.

1) Ran the SQL query by going to phpMyAdmin:

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Make sure to replace YOUR_SITE_URL with the actual URL.

2) Connect the database with testconnection.php, i.e., changed the wp-config.php user to the root user.

What have you tried for this annoying error? Which one is work for you? Share them with us so that you can save someone’s time. Thank you for reading.

Leave a Reply