instagram rss arrow-down

If you’re running a self hosted WordPress blog or website, you have probably already seen or heard for the message saying “Error Establishing a Database Connection”. I’ll show you a couple of solutions on how to fix the Error Establishing a Database Connection in WordPress.

Error Establishing a Database Connection in WordPress

Why did it happen? What did I do? I’m afraid, that is hard to tell unless you have been playing with your database or wp-config.php file. Error Establishing a Database Connection in WordPress can be caused by many different things. It happens to WordPress beginners and the experts as well. I don’t even pay attention to this error, all I know is I need to fix it ASAP and discover what went wrong a bit later.

There is no straight to the point recipe to fix Error Establishing a Database Connection in WordPress, but I will make a list of possible solutions for getting your site back online. If you know what “crashed” your connection or in worst case scenario, your database – it shouldn’t take long to recover it if you’re having an up to date backup of your database, which I strongly recommend.

Even while you currently have the Error Establishing a Database Connection in WordPress, back it up anyways before trying to fix it.

So, how to fix the Error Establishing a Database Connection in WordPress?

What causes this error?

As the message says, because WordPress can’t establish a connection to your database. Now why did this happen, it’s a different pair of sleeves. Reasons may vary on different things. Maybe your database credentials were changed without your knowledge, or you changed them without realizing what you’re doing. Perhaps you tried to add a few more configuration options to your wp-config.php file in the wrong place or the database could be corrupted or database server unresponsive.

Anyhow, seeing this message is not pleasant, especially when you now that a bunch of users are seeing the same thing. Shocked!

Can you access your wp-admin?

Sometimes it can happen that you can access backend of your WordPress site just fine. So first thing to check is your backend access. If you get the same message “Error Establishing a Database Connection”, you should troubleshoot further. In case you’re getting a message like “One or more database tables are unavailable” or similar, then your database needs to be repaired.

Repairing your database for this scenario includes editing your wp-config.php file and adding the following line just before “That’s all, stop editing! Happy blogging”. 

define('WP_ALLOW_REPAIR', true);

Once you’re done with that, save the file and visit this page:

Fix the Error Establishing a Database Connection in WordPress

For this step, you don’t need to be logged in to your WordPress. The purpose of this function is to repair your database, so it is assumed that you don’t have an access to your wp-admin area. Select Repair and Optimize Database and after it is done, delete the line you previously added in your wp-config.php file.

If you were able to Fix the Error Establishing a Database Connection in WordPress with this step, you’re fine. If not, let’s have a look at other possible solutions for fixing it.

Check the wp-config.php file

If you ever wondered which single file is the most important for your WordPress installation, the answer would be wp-config.php. Wp-config.php holds the details of WordPress connection to your database. If you make any changes to your database user password, or the root password, then you will need to update that data here. You should always check is everything in your wp-config file as it should be. These are the defaults:

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

One thing to take a note on, DB_HOST is not always ‘localhost’. It varies on the hosting provider. Some of the providers who have ‘localhost’ as DB_HOST are HostGator, Siteground, Bluehost.. You may need to ask your host to share this info with you.

There is a solution worth trying now. You might be able to Fix the Error Establishing a Database Connection in WordPress by replacing ‘localhost’ with the IP. It’s a long shot, but it often solves the problem on WordPress localhost installation such as on WAMPP or XAMPP.

define('DB_HOST', '');

Different hosts may have different IP’s. Save your file again and try to access your website. If you’re still failing, that it’s most likely the issue on the server end.

Testing connection to your MySQL Server

If you’re website is getting a huge amount of traffic, Error Establishing a Database Connection might occur easily. What’s happening is, your server host simply cannot handle the load at the current time and this can happen low performance hosting plans like shared hosting. To some users site will become unresponsive and some will see the error message. At this point, contacting your host and letting them know about your issue might be the best option.

But in case you want to test connection to MySQL server on your own, there are couple of things you can do. First of all, shared hosting hosts hundreds of sites on the same server. You can easily check else is hosted there. You can use a tool like reverse IP lookup and type in your domain in the search. You will see a list of domains hosted on the same server.

Second thing you could do is try to access your phpMyAdmin from your cPanel and connect the database. If you have successfully connected, then there might be a issue with your database user permissions. To test this, you can create a new file called dbtest.php and add the following code to it:

$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Connection failed: ' . mysql_error());
echo 'Connected successfully';

Make sure to replace username and password with your own and to have opening and closing php tags, and to save the file with php extension.

If the connection was successful, that means that your user can connect and has sufficient perimissions, but the problem is somewhere else.

Double check your wp-config.php file again and make sure everything looks good. The fact you cannot connect to your database with phpMyAdmin, tells you there is something wrong with your server.

When you try to go phpMyAdmin, you will probably get some kind of error message. One of the most common one is permission error.

 If you’re getting this or any other similar message after you tried to Fix the Error Establishing a Database Connection in WordPress with the steps above, you should contact your host immediately and get them to fix it. On a shared hosting, your access to configuration files is limited, and even with all the worlds knowledge, you won’t be able to get it fixed.

Other Solutions that might work

The other solutions to fix the Error Establishing a Database Connection in WordPress might be by checking your site URL. It usually happens after SSL/TLS installation or switching hosts. If you can access your phpMyAdmin, run the SQL query to update your site URL.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

I had a scenario where I simply renamed the plugins folder to plugins1, by doing that, all the plugins are automatically deactivated. So, in that case, one of the plugins caused the error. I could easily isolate the troubled plugin after that.

Check your .htaccess as it can also cause database connection error. Backup your current file, and create a new one with default values.

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

If all that fails, you’ve done something terribly wrong in your past life! You should hire an expert to fix the Error Establishing a Database Connection in WordPress.

Do you have another solution to fix the Error Establishing a Database Connection in WordPress? Please share in comments!


Support New Articles with a Sweet Tee

Leave a Reply
Your email address will not be published. Required fields are marked *