How to Fix the Error Establishing a Database Connection in WP

How to Fix the Error Establishing a Database Connection in WordPress

There’s nothing more frustrating than seeing Error Establishing a Database Connection when accessing your WordPress website. Below, we will explain the causes of this error and various methods you can use to correct the problem.

This page is meant to be as comprehensive as possible to cover the majority of issues we’ve seen. Many of the solutions and corrections we list are based on our own experience working with clients and it also includes solutions discovered from other users.

If you are making database changes, we strongly recommend backing up your database. Go to your cPanel or web control panel and look for a backup icon. Download your database and keep it in a safe location just in case.

Why Is This Error Occurring?

The main reason you are seeing this error is because your WordPress installation is unable to authenticate to your MySQL database. Meaning, the database username and password that is set in the wp-config.php file is incorrect and because of this, your WordPress installation can’t connect to the database to begin the process.

If you have an existing site that was working fine but suddenly you are receiving this error, there’s other reasons that cause this error to appear which we will cover below.

Database Connection Issues on New WordPress Websites

The main culprit to this error on new WordPress installations is the wp-config.php has not been properly configured or has the wrong information. Without the configuration file being set up 100% correctly, you will receive the database connection error.

If you are setting up your WordPress manually, consider using that linked guide. It will walk you through the entire process including updating the following information in your wp-config.php file.

define('DB_NAME', 'database_name_here');
define('DB_USER', 'username_here');
define('DB_PASSWORD', 'password_here');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

Once everything is  configured correctly and uploaded to your server, you should now be able to continue with your WordPress installation.

Did You Know? Most web hosts will install and pre-configure your WordPress for you without having to go through the manual setup process. Log in to your web host control panel and look for a WordPress, Softaculous, Fantastico or SimpleScripts icon. All of these options will automatically install WordPress for you. If you haven’t signed up for hosting yet, consider InMotion Hosting because they offer an automatic WordPress installation service during the sign up process which is even easier!

Running WordPress on Localhost

If you have used LAMP or MAMP to install a PHP/MySQL server on your local machine and you know the defined variables are 100% correct, you may need to specify an IP address instead of using DB_HOST ‘localhost’. Try specifying the localhost IP address like this:

define('DB_HOST', '127.0.0.1');

Some users have reported that using an IP address has fixed their issue.

Database Connection Issues on Existing WordPress Websites

For existing websites, there’s some troubleshooting to do to help detect the primary reason why your website will not load. If this is you, then you are very likely in a hurry to start testing and trying things so let’s get started right away.

Check for Database Corruption

WordPress Database Corruption

Your main site and your /wp-admin/ WordPress dashboard will show a differently styled error page. Add /wp-admin/ to the end of your domain name and see what it says there.

If you see “Error establishing a database connection,” continue on to the next step, database corruption isn’t the cause.

Are you seeing the error “One or more database tables are unavailable. The database may need to be repaired?”

Your database is corrupted. You will see an error similar to the above screenshot with the repaired link taking you to domainhere/wp-admin/maint/repair.php?referrer=is_blog_installed

You will then be taken to a page where it asks you to modify your wp-config.php. Log in to your server with FTP software like FileZilla and download your wp-config.php file. Open the file in a plain text editor (notepad is fine) and add the following line anywhere:

define('WP_ALLOW_REPAIR', true);

Once you add the line to your wp-config.php, upload that to your server, reload the page and you will see the following screen:

Repair Database Tables in WordPress

Again, backup your database. Now, go ahead and click the button Repair and Optimize Database. Hopefully, this will fix your database corruption and get you back online.

Once your database is repaired, remember to remove the WP_ALLOW_REPAIR line from your wp-config.php and reupload. If you leave this line in there, anonymous users will be able to access the repair page and potentially cause serious problems.

Error Establishing Database Connection in /wp-admin/

Error Database Connection in wp-admin

If you see the above error at your yourdomainhere/wp-admin/ URL and your site was working previously, then your database is either unavailable, inaccessible, or something has happened to your wp-config.php.

Step 1: Check Your wp-config.php

wp-config Last ModifiedLog in to your server via FTP, look for your wp-config.php and look at the Last modified date. If this is showing a recent date, then either a hacker or your web host has made modifications to the file.

If this applies to you, then please change your server’s FTP authentication password, notify your web host of the problem and have them restore your previous wp-config.php (if possible). Otherwise, restore the file from your own backup.

Step 2: Connect to Your Database Manually

Use the wp-config from one of your backups that you know has the information right and test for a database connection to your server.

Copy the following code to a new php file, something link testing.php, change the database, username, password, hostname (most use localhost, but not all) and upload it to your server.

<?php
$database = "database_name_here";
$username = "username_here";
$password = "password_here";
$hostname = "localhost";

//Connection to Database. Do not edit below this line.
$t5a = new MySQLi($hostname, $username, $password, $database);
if ($t5a->connect_error) {
   echo "<strong>Error. Unable to connect to your database. Here's the error:</strong><br />" . $mysqli_connection->connect_error;
}
else {
   echo "You connected to your database successfully!";
}
mysqli_close($t5a);
?>

When you point your browser to the new yourdomainhere/testing.php, it will report one of two things: You connected to your database successfully! or Error. Unable to connect to your database. Here’s the error.

If you are able to connect to the database with this method, you may have a typo in your wp-config.php. Fix that and you are golden.

If you are unable to connect to the database and you know the username/password/database are correct, it’s time to move on to step 3.

Verify the information is correct. Consider logging in to your cPanel or web control panel and add a new user and password with full rights to your database. Use that new user/pass that you know is 100% correct and test that with the above script. Not sure how to create a new user/pass for your database? We explain it on our how to manually setup WordPress guide at step 6

If your new database username and password allow you to access the database, then your previous database user may have been deleted, password/name altered or the permissions reset. If that is the case, update your wp-config.php with the new username and password and you should be able to restore functionality to your website again.

Step 3: Contact Your Web Host

If your wp-config appears fine and configured correctly, then the next step is to assume your MySQL database is offline, inaccessible or the host is going through some sort of an attack that is causing their database server to go offline.

Start a live chat, ticket or phone call and find out if they are aware of database connectivity problems. Let them know that your site is showing “Error establishing a database connection,” that you’ve done your best to troubleshoot the problem and have determined that the server/host is having an issue.

About Server Performance Issues

If you receive Error establishing a database connection sporadically on the front end and the back end of your website, the most likely culprit is your web host. You are likely running out of RAM which is causing the server to terminate connections to your database for both you and your users.

One thing that might work for you if you are seeing this problem is asking the web host to restart the server, especially the MySQL service. Something might be hanging up the server that causing the errors.

If that is the case and your host is unable to rectify the problem for you for the long term, then you may need to consider upgrading to a VPS plan or switching to a reliable WordPress web hosting service. Remember, the most important component to a website is reliable web hosting. Without it, you will end up frustrating your visitors, you could potentially see demotion in search engine rankings due to slow response time algorithms and most importantly, your revenue will decline.

About Scott Buehler
 

Scott is the owner and founder of Top Five Advisor. He specializes in business to business products and services. He offers digital marketing services to any local business that wants to drive traffic and customers from the Internet including search, pay per click and social media marketing. This page is maintained by Scott Buehler.