WordPress Error Establishing a Database Connection (Solved)

No matter how many times you refresh, you’re unable to access the website. You’re only left with the “error establishing a database connection” curt message.

So, what are the hiccups that cause this error? There could be many reasons for getting dismaying “Error Establishing a Database Connection” message on your screen.

In today’s WordPress guide, we will try to dig deeper into this WordPress error, and then cover some solutions that you can use to restore your site.

Why Error Establishing a Database Connection occurs?

To understand it better, you should know that WordPress has two fundamental components which are PHP and MySQL. PHP is in charge of all functions on your WordPress site whereas MySQL is in charge of data storage.

For instance, when a user copies the URL in the browser’s address bar and hits enter, PHP kicks in, fetches and displays the contents of the homepage from your MySQL database. Images, videos and such media are, however, stored in the wp-content folder, from where they’re retrieved by PHP. But, when PHP can’t access the MySQL database for any reason, you get the dismaying “error establishing a database connection” message on your screen.

There are many reasons that cause this “error establishing a database connection”, but they generally fall into these categories:-

1. Wrong database’s login credentials: Your database uses separate login credentials (username and password). If those credentials have recently been changed, your website won’t be able to access the database with the old credentials and you should immediately set a new one.

2. Corrupt database: If your database gets corrupted due to any reason such as from uploading a bad plugin, to lightning strikes to the server: you will definitely see this error message on your screen.

3. Unresponsive database server: If the database server slows down due to huge traffic spike that your server just couldn’t handle or any other server side technical error can cause this error.

4. Corrupted WordPress files: You can experience this error due to a problematic WordPress file upload.

It would be insane if we try to fix the problem without knowing the main cause. So, first we need to determine the root of our error, then we can get a viable solution.

Let’s narrow down the problem for your website, but before we recommend you to take a backup of all your files including MySQL Database. Now, let’s fix the “error establishing a database connection” problem.

Is Your WordPress Database Corrupted?

First, we are going to check whether the error is as a result of a corrupted database. For this, we have to test if the error has crippled the back-end as well.

All we have to do is navigate to yoursite.com/wp-admin/. Please don’t forget to replace yoursite.com with your own domain name.

If loading the front-end (i.e. yoursite.com) and the back-end (i.e.  yoursite.com/wp-admin/) results in the “Error Establishing a Database Connection” error, your database is fit as a fiddle, and you needn’t concern yourself with the next step. You can skip all the way to determining whether you have the correct login credentials in your wp-config.php file.

However, if you’re getting “One or more database tables are unavailable…” or something like the infamous “Error Establishing a Database Connection” error, your database is corrupted and in dire need of repair.

Don’t worry though, as repairing your WordPress database is easy-peasy work.

First, all you need to do is to access your wp-config.php, open it in editing mode.

define ( 'WP_ALLOW_REPAIR' , true);

Add the following code at the bottom and save your changes.

The above code will allow you to repair the WordPress database without having to login into phpMyAdmin manually.

The second step involves navigating to yoursite.com/wp-admin/maint/repair.php. This should open the following screen:

Error Establishing a Database Connection

Third step, just choose either “Repair Database” or “Repair and Optimize Database” and let WordPress do the rest.

Note: Once the database is repaired, we are going to delete the code which was added to wp-config.php file. Otherwise, we are at risk of granting hackers access to our database, where they will wreck havoc.

If this method solved the “error establishing a database connection” error, well and good, otherwise the error stems from incorrect login details, corrupted WordPress files or an unresponsive database server.

Now, let’s check whether your database login details are the problem.

To do that, we need to open the wp-config.php once again and check for the following lines of code:

// ** MySQL settings - You can get this info from your web host ** //

/** The name of the database for WordPress */
define('DB_NAME', 'database_name');

/** MySQL database username */
define('DB_USER', 'database_user');

/** MySQL database password */
define('DB_PASSWORD', 'database_password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

If any of above credentials are incorrect, you will get the “error establishing a database connection” error. So, check these credentials carefully which are stored in your database.

First, let’s check if you have the correct database name in your wp-config.php. Follow the following steps:

One way to check on your database outside of WordPress is by using PHPMyAdmin, a tool for accessing and editing MySQL databases. PHPMyAdmin is included with most hosting plans, and you can find it in your cPanel dashboard.

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

Once you’ve logged in to PHPMyAdmin, you will see a list of databases on your server in the left-hand column. Click on the one that matches the name in your wp-config file (it will be the value after DB_NAME, where it says “database_name_here” in the above sample code).

(Don’t see any databases? Contact your hosting company, since the entire problem may be an issue with your server.)

After clicking on your database name, you’ll see the main screen populate with the names of the tables in your database. To make sure this is the correct database, you can find the table named “wp_options” and click on the ‘Browse” option next to it. You should see your website’s name, URL, and your other general settings here. Now you know whether or not you have the correct database name in your wp-config file. If it’s not correct, go ahead and fix it in wp-config.

Now let’s check on the username and password.

There are a few ways to do this, some more convoluted than others. I’ll give you two options.

  1. You can either create a simple .php file to test whether you are able to connect to your database with the credentials in your wp-config file, or
  2. You can just create a new user and password, and update your wp-config file with the new info. You’ll have to do this if #1 fails, so you might just want to skip to this step.

Option 1: Test your existing credentials.

Create a file in your WordPress directory called something like testconnection.php (the name doesn’t matter; just the .php extension). Paste the following code:

<?php
 $test Connection = mysql_connect('localhost', 'root', 'password');
 if (!$testConnection) {
 die('Error: ' . mysql_error());
 }
 echo 'Database connection working!';
 mysql_close($testConnection);
 ?>

Once you’ve created that file, just navigate to the URL of the file in your browser (e.g. your site.com/testconnection.php). You’ll see either a successful connection message, or an error with more details.

If the username and password isn’t working, we can just create a new one.

Option 2: Create a new database user and password.

We can do that using a different tool available in cPanel called “MySQL® Databases.” Go ahead and click on that, then scroll down to the heading: MySQL Users: Add New User.” Choose a username and strong password, and make note of them for your wp-config file. Click “Create User.” Now scroll down to the heading “Add User To Database,” and choose your new username and your WordPress database, and click “Add.” Update your wp-config file with the new username and password.

Now your database name, username, and password are all correct. That just leaves DB_HOST.

In most cases, this value will be “localhost,” but it depends on your host’s settings. WordPress has a list of common hosts with their DB_HOST values on their website. If your host isn’t listed there, you can contact your hosting company to ask what value to use.

Are you still getting the “error establishing a database connection” error? If that’s a yes, the problem could be corrupted WordPress files.

Fixing Corrupted WordPress Files

To fix the corrupt WordPress files you need to restore your WordPress files. But, make sure you back up your entire WordPress site before proceeding, otherwise a tiny mistake can wipe away your entire site.

Next you need to download a fresh copy of WordPress from WordPress.org. Unzip the package on your computer and then delete wp-config.php as well as the wp-contents folder. This will ensure you don’t break your existing wp-config.php file. It will also ensure you don’t lose the themes, plugins and media stored in wp-content once you restore WordPress.

Once you have this covered, upload the remaining files to your WordPress root folder using FTP or the File Manager. This should replace all problematic files.

Now, clear your browser’s cache and try reloading site. The website should open now if you’re still seeing the error, then contact your web host because the problem could be on their side.

Also read:

We believe this article was helpful to troubleshoot the “error establishing a database connection”. If you liked our article then please do share this over your favourite social media.

LEAVE A REPLY

Please enter your comment!
Please enter your name here