Solutions that Work: How to fix Database Connection Error in your WordPress Website


How to fix Database Connection Error in WordPress“Error establishing Database Connection”

This error is like a cockroach: no matter what you do, it always comes back, and you never know where it returns from.

The causes and solutions to this pesky error can be tabled (as depicted here). But knowing the technicalities doesn’t reduce the increased heart palpitations this error’s occurrence can cause, especially in a novice WordPress website owner/admin.

In this post, I’ll walk you through various solutions that will banish the database connection error in WordPress. Before we start, check to ensure that you have backups of your site and database, just to be on the safe side.

Don’t worry, I’ll make sure you can keep up, coding knowledge (or lack of) notwithstanding.

All problem-solving tactics begin with:

1- UnderstandingRepair Database

What does it mean and why do you receive this error in the first place?

The error is exactly what it says on the tin. Your WordPress database (written in MySQL) is essentially a collection of tables which store every bit of data (information) you have on your website. This includes content, comments, user profiles, and such, along with ‘invisible’ information about all that like metadata.

The database connection error on WordPress is generally caused by:

  • Incorrect database login credentials, server name, port number, or database name
  • User permission
  • Server downtime or error
  • Corrupted database

There are any number of factors which can cause any of the above situations, resulting in database connection error and a mess for you to deal with. That’s alright. Now that you have a better idea of what went wrong, you can start attempting repairs.


2- Possible Repairs


– – Repair Database function

Note: This solution works if you are receiving a different error message while trying to access your admin screen than the one displayed on front-end. If the error message on your site’s front-end and admin is the same, this won’t work.

This solution also works in the event when you’re logged out of your WordPress account.

Go to your WordPress installation directory and open wp-config.php. Near the end of this file, just above *That’s all, stop editing!, add this:

define(‘WP_ALLOW_REPAIR’, true);

Save changes and visit: http://www.domain-name.com/wp-admin/maint/repair.php. You’ll see the following message:

Kindly place “Repair Database” image here

Click on ‘Repair Database’, wait a while, and try again.

Keep in mind that you’ll have to remove the define(‘WP_ALLOW_REPAIR’, true); from your wp-config.php once the problem is solved.

 

 – – wp-config.php

This is the file which contains the information regarding how WordPress connects to the database (and for which purposes). The file also contains information about your database credentials, so direct yourself to check those first. You’ll find it written like this:

define(‘DB_NAME’, ‘your_database_name’);

define(‘DB_USER’, ‘your_database_username’);

define(‘DB_PASSWORD’, ‘database_password’);

define(‘DB_HOST’, ‘localhost’);

The ‘DB_HOST’ value (localhost) will need to be replaced. Depending upon your web host, it could be your IP, or anything else. WPBeginner compiled a list here.

Now, try to login again with the database information you found here. If the problem persists, it’s time to take the repairs to the server-side.

 

3– Web Host

Server downtime can be caused by unexpected traffic surges, which in turn can be the cause of database connection error. This means that the server is unable to keep up with the bandwidth requirement and so many simultaneous requests.

The first thing you need to do is contact your web host and enquire about your server. You can also check this yourself with a bit of canniness. If you know the URLs of other sites hosted on the same server as yours (shared hosting), try visiting them. If you’re on a dedicated server or VPS, take a different approach. Go to your cPanel and open phpMyAdmin.

If you receive ‘Access Denied’, then you can be sure that either your server is at fault or the user doesn’t have database permission.

If you can connect, test that the login credential you’ve been using has required permission to access the database anyway. Make a new file and name it testconnection.php. Add this:

<?php

$link = mysql_connect(‘localhost’, ‘root’, ‘password’);

if (!$link) {

die(‘Unable to Connect: ‘ . mysql_error());

echo ‘Connected’;

mysql_close($link);

?>

Save and recheck by visiting your website. If you still can’t connect, get on the phone with your web host and ask them to restore your database permissions.

 

Endnote

Sure, it’s a frustrating error, but you can work through it with a little perseverance and lots of help from the rest of the community and resources like these.

Author Bio: Tracey Jones is an exuberant professional and expert in the field of web development. Presently, she is working for HireWPGeeks Ltd., where she assists her clients to hire WordPress developer to meet all your WordPress development needs at cost-effective rates. Instead of all these things she is very interested in research on various web technologies.

Need our help to grow your company in the internet?

We can develop the perfect solution for your business, generating more revenue through the internet. It's easy, fast and cheap!