You enter your website's URL… but what greets you isn't your familiar home page. Instead, it is a blank screen. No matter what link you try, everything's missing, aside from the ominous words:
“Error establishing a
database connection.”
The longer your website is down, the more traffic you'll lose. And that might mean lost subscribers, lost customers, and ultimately lost revenue.
But what exactly does that error even mean? And more importantly, how can you fix it and get your website back to normalcy when possible?
In this informative article, we'll review just what the error message means and what could possibly be causing it, and then I'll walk you through some steps to troubleshooting and fixing the problem.
What Does “Error Establishing a Database Connection” Mean?
First, let's have a look at how WordPress works to show your website, and it'll become clear why this error is really devastating.
The WordPress software is written using PHP and MySQL. Why two languages? Because all the info that produces up your website is stored in a MySQL database, and WordPress uses PHP to store and retrieve information from that database.
Generally, every time a page is loaded in your website, it's built on-the-fly. PHP code is employed to access your MySQL database and retrieve every little bit of information needed to generate the page. There is a PHP query to access the post title, another to seize mcdougal name, another to retrieve the publication date, and so on.
Back to your error message: “Error establishing a database connection.” Which means that for reasons uknown the PHP code was unable for connecting with your MySQL database to retrieve the info it takes to build that page.
What Causes This Error?
There are lots of reasoned explanations why your website could be having problems connecting to your database, but they generally fall under three categories :
- Your login credentials for your database are wrong. 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.
- Your database is corrupted. Any number of things could cause your WordPress
database to become corrupted, from uploading a bad plugin, to lightning
strikes to the server (seriously — though luckily that isn’t too common).
- The server that hosts your database is down. 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 couldn’t handle.
So with all these possibilities, how
can you narrow down the problem and fix it?
1. Determine where the error is occurring
Is it possible to see your website normally, but get the error when trying to access wp-admin? Or maybe your website is down, but you're obtaining a slightly different error once you attempt to login: “One or more database tables are unavailable. The database may need to be repaired.”
They're signs of database corruption.
WordPress has a built-in way to attempt to repair your database, but you will have allow this feature first. To do so, you will need to access your wp-config.php file, which contains your WordPress installation settings and configuration.
You will find wp-config in the your root WordPress file directory. You are able to access it by logging into your cPanel, selecting File Manager, and navigating to the folder where you installed WordPress.
After you have wp-config open, add this line of code to the underside:
define('WP_ALLOW_REPAIR ', true );
This line will enable one to optimize and repair your database by navigating to www.yourwebsite.com/wp-admin/maint/repair.php (just replace “yourwebsite.com” together with your actual URL).
You need to see the above mentioned screen with two options to repair, or repair and optimize, your database. Feel free to select either; just note that optimizing will take longer.
Please note that database repair page is not secure; anyone can access that URL without having to login. Once you're done repairing your database, be certain to take away the line of code you put into wp-config. This may disable usage of the repair page and prevent anyone else from messing together with your database.
(Just as a side note, you will find different ways available to repair and optimize your WordPress database, including via PHPMyAdmin in cPanel. I wrote a bit about this here:
Cleaning Up Your WordPress Database To Optimize Your Website's Performance)
Keep wp-config open for now until you're certain the error is resolved and your website is working again. If you're still getting the exact same error message, you will need wp-config open for the following step.
If this doesn't apply for you, or if it didn't work, proceed to another location step to carry on troubleshooting.
2. Check your database login settings in wp-config
If the above step didn't work or didn't apply for your requirements, then a next thing you can do is have a look at your database settings in your wp-config file.
Wondering how your database login credentials would have suddently stopped working? Things such as switching hosting companies or changing anything about your database name or user information might lead to the error if your wp-config file wasn't updated to the brand new information.
You may still have the wp-config file open from the final step. If not, log into your host's cPanel and open the File Manager. See a directory where you installed WordPress, and locate a file called wp-config.php. Go ahead and open that for editing.
You'll see the login credentials for the database, probably close to the the top of file. It should look something like this:
//
** MySQL settings - You can get this info from your web host ** //
|
/**
The name of the database for WordPress */
|
define(
'DB_NAME', 'database_name_here' );
|
|
/**
MySQL database username */
|
define(
'DB_USER', 'username_here' );
|
|
/**
MySQL database password */
|
define(
'DB_PASSWORD', 'password_here' );
|
|
/**
MySQL hostname */
|
define(
'DB_HOST', 'localhost' );
|
As you can see, there are four pieces of information WordPress needs to
access your database:
- The name
of the database (“DB_NAME”)
- The login
username (“DB_USER”)
- The login
password (“DB_PASSWORD”)
- The
database host (“DB_HOST”)
If any one of these brilliant values is incorrect, WordPress won't have the ability to hook up to the database.
One method to check in your database outside WordPress is by utilizing
PHPMyAdmin, an instrument for accessing and editing MySQL databases.
PHPMyAdmin is roofed with most hosting plans, and you will find it in your cPanel dashboard.
Warning: Be careful using
PHPMyAdmin, since you'll be dealing directly with your site's database.
Once you've logged directly into
PHPMyAdmin, you will dsicover a listing of databases in 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 whole problem might be a problem with your server.)
After clicking in your database name, you'll see the main screen populate with the names of the tables in your database. To be sure this is the correct database, you will find the table named “wp_options” and click the ‘Browse” option close to it. You must see your website's name, URL, and your other general settings here. So you know whether or not you've the proper database name in your wp-config file. If it's not correct, go ahead and fix it in wp-config.
Now let's check into the username and password.
There are always a few ways to achieve this, more convoluted than others. I'll give you two options.
- 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
- 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 are able to accomplish that utilizing a different tool obtainable in cPanel called “MySQL® Databases.” Go ahead and select that, then scroll down seriously to the heading: MySQL Users: Add New User.” Select a username and strong password, and make note of them for the wp-config file. Click “Create User.” Now scroll down seriously to the heading “Add User To Database,” and choose your username and your
WordPress database, and click “Add.” Update your wp-config file with the brand new username and password.
Now your database name, username, and password are all correct. That just leaves DB_HOST.
Generally, this value will soon be “localhost,” but it depends in your host's settings. WordPress has on their website. If your host isn't listed there, you can contact your hosting company to ask what value to use.
3. Still getting the error message? Check with your hosting company
If you've followed the above troubleshooting steps and your website remains down, it is actually a problem in your host's end. Contact your online hosting company's support and give them all the facts of the error and the troubleshooting actions you've taken, and they might have the ability to assist you to determine the reason for the error.