A blog for technology, SEO tips, website development and open source programming.

How to Fix the Error Establishing a Database Connection in WordPress

2 2,363

This could be awfully frustrating specially when it happened on its own without you changing anything. This post will show you how to fix the error establishing a database connection.

“Before you make any database changes, make sure you have sufficient backups.

Why do I get this error?

WordPress is unable to establish a database connection. Maybe  your database login credentials are wrong or have been changed.  It could be that your database has been corrupted.

Don’t miss Out -> How to switch back to the classic editor

Check up list

  • make sure that you are getting the same error on both the front-end of the site, and the back-end of the site (wp-admin).
  • error messages is the same on website pages

If you are getting a different error on the wp-admin for instance something like:

“One or more database tables are unavailable. The database may need to be repaired”

, then you need to repair your database. You can do this by adding the following line in your wp-config.php file

define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page: http://www.yoursite.com/wp-admin/maint/repair.php

Checking the WP-Config file

WP-Config.php is probably the single most important file in your entire WordPress installation. This is where you specify the details for WordPress to connect your database. If you changed your root password, or the database user password, then you will need to change this file as well. First thing you should always check is if everything in your wp-config.php file is the same.

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

Remember your DB_Host value might not always be localhost. Depending on the host, it will be different. For popular hosts like HostGatorBlueHostSiteGround, it is localhost. You can find other host values here.

Some folks suggested that they fixed their problem by replacing localhost with the IP. It is common to see this sort of issue when running WordPress on a local server environment. For example on MAMP, the DB_Host value when changed to the IP may seem to work.

1define('DB_HOST', '');

IP’s will vary for online web hosting services.

If everything in this file is correct (make sure you check for typos), then it is fair to say that there is something wrong on the server end.

Check your Web Host (MySQL Server)

Create a new file called testconnection.php and paste the following code in it:

$link = mysqli_connect('localhost', 'username', 'password');
if (!$link) {
die('Could not connect: ' . mysqli_error());
echo 'Connected successfully';

Source: www.wpbeginner.com

Follow me on Instagram

That’s it for now. 

If you liked this article, then please subscribe to my YouTube Channel for video tutorials.

You can also find me on Twitter and Facebook.

  1. wikilogy says

    Great. It worked for me. Thanks

    1. Panayiotis Georgiou says

      Awesome, very good news!

Leave a Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More