1and1-co-uk Web Hosting
AWeber (Autoresponder)
ElegantThemes
HobbyTron
This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. Find out more.

How to Fix the Error Establishing a Database Connection in WordPress

 Posted by  Add comments
 

How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

In this tutorial, will show you how to troubleshoot and fix, the error establishing a database connection in WordPress.

It is heart wrenching when you browse to your WordPress site, and instead of seeing your carefully crafted content, you are greeted with a blank screen, except for the ominous words:

“Error establishing a database connection.”

You quickly realise, the longer your website is down, the more traffic you will lose. That means lost subscribers, lost customers, and lost revenue.

What does the error mean, and how can you fix it?

Unfortunately, Error Establishing a Database Connection in WordPress can be caused for many reasons, so there is not one simple solution to this problem, you may have to try several of the solutions provided.

This is a particularly frustrating error, because it can happen for no apparent reason, as many WordPress webmasters find out

Before You Begin

As you will be messing about with your WordPress database, it is important to backup the database.

  • Important, backup your WordPress database.

 

Video Tutorial

Note: Textual instructions follow the video if that is what you prefer.

 

What Causes, Error Establishing a Database Connection in WordPress?

Well, and this may be a little obvious, you are getting this error because WordPress is unable to establish a database connection. This means, that for some reason, the PHP code was unable to connect with your MySQL database to retrieve the information it needed to build your website pages.

The reasons why WordPress is unable to establish a database connection are many. It could be that:

  • Your database server is unresponsive.
    Your database’s server may be down due to a problem or error on the part of your web hosting company, or you could be experiencing a big traffic spike that your server just could not handle.
  • Your database has been corrupted.
    Any number of things could cause your WordPress database to become corrupted, but the highest probability is a bad plugin.
  • Your database login credentials are wrong.
    If your database credentials have been changed, your website will not be able to access the database with the old credentials.

In all probability though, the error is caused by a server error.

Let us take a look at troubleshooting this problem.

Troubleshooting, Error Establishing a Database Connection in WordPress

How pervasive is the problem?

Firstly, is the Error establishing a database connection, site wide. That is, are you getting the error on both the front-end and back-end of your site (wp-admin)?

If you are getting a different error on the back-end, something like, “One or more database tables are unavailable. The database may need to be repaired“.

Then, as stated, you need to repair your database.

Database Repair

WordPress has a built-in database repair method, but you will have to enable this feature first. You can instigate a database repair job by adding a line of code to the wp-config.php file.

Just before ‘/* That’s all, stop editing! Happy blogging */’ line, add the following line:

define('WP_ALLOW_REPAIR', true);

With this define set, you can visit the repair page by entering the following line in your browser:

http://www.yoursite.com/wp-admin/maint/repair.php

Replace “www.yoursite.com” with your actual URL.

How to Fix the Error Establishing a Database Connection in WordPress

You should see the above screen with two options to repair, or repair and optimise, your database. Feel free to choose either.

Please note that this database repair page is not secure; anyone can access that URL without having to login. This is because, in some instances, you may not be able to login with a corrupt database.

IMPORTANT: When you have completed the repair and optimisation of the database, remove the define from your wp-config.php file.

If you are getting the same error message on both the front-end and back-end of your site, or the above solution did not fix the problem, then continue reading. Otherwise, if the error has been fixed, you are done.

Check Your Database Login Settings in wp-config.php

Have you changed your database root password or user password?

The wp-config.php file controls your entire WordPress installation. This is where you specified the details for WordPress to connect to your database. If you changed your database root password, or the database user name, then you need to change the settings in wp-config.php as well.

Check the following setting are correct:

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

If any of these values are incorrect, WordPress will not be able to connect to the MySQL database.

In particular, is your DB_Host value localhost? Some hosting providers set this differently. For popular hosts like HostGator, BlueHost, and JustHost, the value should be localhost.

If everything in the wp-config.php file is correct, and there are no typos, then there is a very high probability that there is something wrong on the server end.

Check your Web Host MySQL Server

One way to check on your database outside of WordPress is by using PHPMyAdmin. PHPMyAdmin is included with most hosting plans, and can be found in your cPanel dashboard.

Warning: Be very careful using PHPMyAdmin, as you will be dealing directly with your site’s database.

Access the database.

  • Go to your cPanel, run phpMyAdmin and connect to your database.

If you cannot connect to your database, then there is definitely something wrong with the server. Contact your hosting company.

If you can connect, then you need to verify that your database user has sufficient user permissions.

Test Your Existing Credentials.

Create a new file called testconnection.php, containing the following code (copy and paste) and place it in your WordPress folder. The same folder where your wp-config.php file resides:

 

<?php
/***** filename: testconnection.php **************************
*
* db_host Specifies a host name or an IP address
* db_username Specifies the MySQL username
* db_password Specifies the MySQL password
* db_name Specifies the default database to be used
*
**************************************************************/

$link = mysqli_connect('localhost', 'root', 'dummy', 'wp_test_plugins');

if (!$link) {
 echo 'Could not connect to MySQL database. ' . PHP_EOL;
 echo 'Error: ' . mysqli_connect_error() . PHP_EOL;
 echo 'Errno: ' . mysqli_connect_errno() . PHP_EOL;
 exit;
}
echo 'Connected successfully to MySQL database' . PHP_EOL;
mysqli_close($link);
?>

Change, ‘db_host’, ‘db_username’, ‘db_password’ and ‘db_name’ to match your wp-config.php file settings, before running.

When you have done that, change your-wp-site-path, and run the file in your browser.

http://your-wp-site-path/testconnection.php

If you see the “Connected successfully” message, then that user has sufficient permission to connect to that database.

If you see the “Could not connect” message, there is something else wrong. Check for typos in the testconnection.php file and your wp-config.php file.

Getting the “Could not connect” message does not necessarily mean your MySQL server is down. It could mean that your user does not have sufficient permission to access the database. Hence checking for typos is important.

Website Overload

If you site is overloaded with traffic, the Error establishing a database connection can be the result. This is particularly prevalent on shared hosting sites.

The best solution here is to contact your website hosting provider. Ask them to check the MySQL server.

How to Fix the Error Establishing a Database Connection in WordPress Conclusion

So, what now? Well, if your site is still down after the above troubleshooting, it could be a problem with at your host’s end. Contact your hosting provider’s tech support.

Give them all the details you have on the error and the troubleshooting actions you have taken. Hopefully, they can help you fix the problem.

This is a really frustrating error. What have you tried that worked for you? Let us know in the message box below. You may help others find their solution.

Well, I hope you liked this tutorial and found it helpful. If you have any comment, corrections or items you think should be added to, ‘How to Fix the Error Establishing a Database Connection in WordPress’, please do not hesitate to let me have them in the comment box below.

 


KingSolutions.org.uk is hosted on JustHost

 Leave a Reply

(required)

(required)

183 queries in 1.016 seconds (Child).