Are you facing an error establishing a database connection? Or are you wondering how do I resolve the error by establishing a database connection issue?

Let’s take a look at how WordPress displays your website first, and you’ll see why this issue is so dangerous.

PHP and MySQL are used to create the WordPress software. So why are there two languages? Because your website’s information is saved in a MySQL database. And WordPress uses PHP to store and retrieve information from that database.

Whenever a page on your website is loaded, it is usually constructed on the fly. PHP code is used to connect to your MySQL database and retrieve all the data required to build the page. There’s a PHP query for getting the title of the post. Another for getting the author’s name. Another for getting the publishing date, and so on.

“Error establishing a database connection,” according to your error message. This signifies that the PHP code was unable to connect to your MySQL database. And, also unable to retrieve the information required to generate that page for some reason.

That’s why, on an otherwise blank page, this error is frequently the only thing you see. There’s no way to know what to display if you can’t connect to your database. No information about your website is available.

What is the WordPress Error Establishing a Database Connection?

In this section, we will discuss what does Error establishing a database mean.

MySQL database stores all the information on the WordPress site. Like Post data, login information, meta information, page data, plugin settings, etc.

Media content such as images and theme/plugin/core files doesn’t get stored like index.php, wp-login.php, etc.

When a visitor comes to your website, PHP runs the code on the page and queries the database for information, which is then displayed in the visitor’s browser.

If this doesn’t work, you’ll get the error establishing a database connection WordPress notice, which looks like this:

The entire page is blank since the connection is down. No data can be retrieved to render the page. This not only breaks your site’s frontend. But it also prevents you from accessing your WordPress dashboard.

Visitors may not see the problem on the front end right away. Because your site is probably continuing to serve from cache till it expires, this is the case. 

If you have a site that doesn’t update frequently, this can actually improve the performance of your site because it won’t have to download new files as frequently when the cache expires. And, in most circumstances, the front end of the site would be up a lot longer unless you have a script or component of your site that breaks the cache.

When people try to access your site when this problem is occurring, your logs will show a 500 HTTP status code. When there is an “internal server error,” the same status code shows.

 It basically signifies that something went wrong on the server and the requested resource did not arrive. When everything is running properly, your site will return a 200 HTTP status code, indicating that all is well.

Also Read :- How to Fix a 500 Internal Server Error in WordPress

What Causes Error Establishing a Database Connection in WordPress?

Now that we know what the problem means. We need to figure out what is causing it when we try to open our WordPress site. It could be for a variety of reasons, as database connectivity issues might have a variety of underlying causes.

Incorrect Login Credentials

It could be due to the administrator panel being accessed with the incorrect login account and password if your login credentials have recently changed. You will be unable to access your database using the previous credentials. As a result, the first thing you should do is double-check that the login credentials you’re using are correct.

You forgot to update your wp-config.php file when you changed the database password

It’s a good idea to update a database password to make your database more secure. The ideal kind of password is one that is strong. Remember that whenever you change a database password in your hosting control panel. You must also change the password in your wp-config.php file.

The good news is that this is a simple problem to solve. Simply enter your wp-config.php file and replace the old password with the new one.

Open a text editor and edit the following line in your wp-config.php file:

Save the wp-config.php file and upload it over FTP with your new password. Check to see whether your site is working again by refreshing it. If that’s the case, you’re done.

Note: The single quotations around the password should be left alone. There should be no spaces between the single quotes and your password.

In essence, you’re ensuring that your database connection information has been changed to reflect the new database password.

This step isn’t applicable to you if you’re on a host like Flywheel, so you can safely continue on!

You entered your database information incorrectly during the famous WordPress “5-minute install.”

When working online, typos are the scourge of our existence. One small error may cause a website to crash. A wrong underscore or forgetting a letter or number from the database name, username, or password can cause the same crash.

WordPress installations now are done by a wizard in your hosting control panel. Or Managed WordPress takes care of everything for you. But, some people prefer to install manually.

If you choose to install WordPress the old-fashioned method. By manually building a database and manually uploading the WordPress software over FTP. Ensure to save the database user information when building your database. Copy and paste are really useful in this situation.

Make sure that you only copy the content that you need while copying this information. Did you know that adding a blank space after the database name, username, or password is treated as a character? When the installation attempts to connect to your database, the extra blank character will cause issues.

Also, during installation, double-check that you’re putting the information in the correct field. I had a bit of a meltdown when I discovered I was typing the database name into the incorrect field. The installation went off without a hitch once the problem was solved!

When moving a WordPress website to a new host, DB_HOST may be something unique to that host

Even if you altered your wp-config.php file to reflect your new hosting company’s new database connection details, you may need to alter your DB_HOST Field.

This is a less common issue, but it does occur when defining the DB Host field in wp-config. Some hosting firms use their own unique values, although this is not always the case.

Other hosts specify a URL for the MySQL database server, which must be mirrored in the DB HOST variable. You can also try utilizing your hosting I.P. address instead of a MySQL URL or localhost.

If you use Flywheel, this is taken care of for you automatically.

Your database gets corrupted

This issue isn’t quite the same as “Error establishing a database connection,” but it’s close. On occasion, your website’s front end functions great. But the backend displays an error. Such as “One or more database tables are unavailable, the database may need to be repaired.” This shows that the database should be repaired.

The solution to this problem is as follows:

Go to the part of your wp-config.php file that defines your database name, username, password, and host. Define the following under Host:

define(‘WP_ALLOW_REPAIR’, true);

Then save your wp-config.php file. Upload it to your server through FTP, and go to http://www.yoursite.com/wp-admin/maint/repair.php.

This will take you to a page with two choices. The first choice is to click the “Repair Database” button. The second option is to click the “Repair and Optimize Database” button.

I can’t stress enough how important it is to back up your database before proceeding with this step. If you can’t connect, that doesn’t mean you shouldn’t have a backup.

When the repair procedure is complete, open a new tab and browse to your URL to see if the “Error establishing a database connection” warning has vanished and your site has returned to normal.

If this remedy and the other ones don’t work, there’s one more thing you should be aware of.

Most managed hosts will back up your data on a daily basis for you, So if something goes wrong, it’s often just as easy to restore from yesterday’s backup and start moving on.

The MySQL database server is down.

This is common among people who push their shared hosting to its limits. If your website receives a lot of traffic. The database server may become overwhelmed. Congratulations on having a website with so much traffic. But if you’re still seeing the “Error establishing a database connection” error after trying all the other options. You may need to upgrade.

Hundreds of websites can be jammed onto the same server on a shared host if one site receives a lot of traffic or is simply a noisy neighbor in general. It might pose major issues for the other sites. Each site gets its own server on managed servers like Flywheel. Thus this problem is easily solved.

But, before proceeding with a hosting upgrade. Contact your hosting company and request that they examine the database server’s status. Maybe there’s a technical problem. Or maybe your hosting business sent you an email about scheduled server maintenance. But you didn’t receive it. While it’s unlikely that you’ll get an “Error establishing a database connection” warning as a result of server maintenance. It’s worth calling to find out.

Explain to your hosting business that you’ve tried all these fixes and that you’d like them to look into it. If they tell you that your site is putting a lot of strain on the server and that you should improve your hosting, you’re excellent! Keep in mind that these upgrades show that your site is performing well.

How to Fix Error Establishing a Database Connection in WordPress

Because we respect your time, we’ll start with the path that’s most likely to solve the problem promptly for you. Then we’ll delve further and teach you how to perform all the fixes manually.

Step 1: Get In Touch With Your Web Host Provider

It’s safe to assume that if your site was operating just a minute (or a day) ago and isn’t now, the problem is most likely with your web server.

These days, all good hosts provide some type of fast-response help, usually via live chat.

Start by contacting that live chat and explaining that you’re having an “error establishing a database connection”, and ask:

  •  if the database server is up and running without any issues
  •  if there isn’t any current surge in activity or traffic on the server.

Most of the time, if you tell them what kind of issue you have, They’ll either tell you what’s causing it or even fix it for you.

Most importantly, they’ll tell you if your database is in good shape. Or if there’s any unusual activity going on on the server.

If the problem isn’t on their end (the server is up and running, and there are no traffic spikes), and they won’t help you, move on to step 2:

Also Read :- The Truth About Website Hosting Costs

Step 2: Check If Your Plugin or Theme Files Haven’t Been Corrupted.

A couple of unfortunate occurrences may cause your WordPress files to become corrupted.

For example, what if you changed a plugin or theme and the entire site crashed? Maybe you connected the site to a third-party service? Perhaps you modified some of the files by hand?

If any of the above sounds familiar, you’ve probably found the perpetrator. If you haven’t already done so, you can skip this step.

Here’s what you can do:

  • First: 
  1. FTP access is needed to connect to your host. You can do this with FileZilla, a free program.
  2. You’ll need your FTP account’s login and password to connect. If you don’t know what/where they are. Find them in your web host’s initial communications when you joined up. Or you can request those details from live chat support professional.
  3. Connect to the server and navigate to the WordPress root folder after you have these. It’s commonly called “public_html” or “www,” and you may already be in it once you’ve established an FTP connection.

The thumb rule is that if you can see subfolders like “wp-content” and “wp-admin,” then you’re in the WordPress root. 

  • Second: Go into “wp-content” and rename the “plugins” folder to something else (whichever other names, as long as it’s not “plugins”).
  • Third: Go back to your site and see if the error is still there.

If not, you now know that the problem is due to a plugin. Return to FTP and rename the folder to “plugins.” Change the folder names of the different plugins one by one in there. Adding an underscore at the end is the simplest way to do this: “. “

Return to your site frequently to see if the problem is triggered or disappears. You’ve located the troublemaker once you’ve narrowed it down to one unique plugin. Delete it or check the internet to see whether the solution has been recorded.

  • If the error persists even after renaming the “plugins” folder, repeat the procedure with the “themes” folder.
  • If it doesn’t work, rename both files to “plugins” and “themes” accordingly. Something else is to blame for the error.

Step 3: Check If Your Database Hasn’t Been Corrupted.

The WordPress database is a complicated system. With a lot of small cogs that must all operate together flawlessly to avoid problems. Something may go wrong from time to time. Resulting in a problem establishing a database connection.

  • Firstly, go to your WordPress dashboard – YOURSITE.com/wp-admin.

Skip this step if you get the same major error: “problem creating a database connection.”

  • Secondly, If you notice something else, pay close attention to what WordPress recommends you do. WordPress has increased its problem-solving options in recent versions. So you might find some useful tips there.

Another possibility is that you’ll receive an error notice that begins with “One or more database tables are unavailable […].” This means you’ll be the one to “fix” the database.

This is actually fairly straightforward. Reconnect to your site using FTP for the first time. Download the “wp-config.php” file from the WordPress root to your desktop.

Add one more line to the end of this file in notepad (just before “That’s all, finish editing” ):

define(‘WP ALLOW REPAIR’, true); define(‘WP ALLOW REPAIR’, true); define(‘WP AL

Replace the old version with the new version and upload the file to your site (through FTP).

Go to YOURSITE.COM/wp-admin/maint/repair.php to repair your site.

Either of the buttons can be pressed. Return to your site when the automatic repair has been completed to see if the error has been resolved.

If there aren’t any errors, that’s fantastic! But, you must restore the previous state of your “wp-config.php” file. Re-upload the file after removing the new line you just added.

If the problem persists, the next step is to:

Step 4: Check Your Database Connection Credentials

WordPress connects to the database using a unique username and password, as shown before. Those access credentials don’t usually change on their own. But, after making any modifications to your site files. The hosting environment, or anything else, you may encounter some strange scenarios. In other words, even though it shouldn’t, access credentials change from time to time. And when you have bad access credentials, you’ll get a “problem establishing a database connection” message!

The fix is done in the “wp-config.php” file. We’re talking about these lines specifically:

define('DB_NAME', 'some_db');

define('DB_USER', 'some_username');

define('DB_PASSWORD', 'some_pass');

define('DB_HOST', 'localhost');

They contain all the information WordPress needs to connect to the database. No link will be made if any of these are incorrect.

There is a manual approach to remedy this problem. But it involves many steps and can be confusing depending on the webserver and the interface. To put it another way, everything we say there would only apply to a tiny group of users who happen to be on the same host as us. So we’ve come up with a lot more general solution for you:

Take those four lines from the “wp-config.php” file and copy them to your clipboard. Contact your web host support again. Paste those four lines into the chat window, and ask if the details are correct for you to connect to your database

The representative will review everything and contact you with his or her findings. If anything has changed, they will usually supply you with the updated information.

All you have to do now is copy the new credentials and paste them where the old ones were in your “wp-config.php” file.

define('DB_NAME', 'some_db'); /* this is the name of your WordPress database */

define('DB_USER', 'some_username');

define('DB_PASSWORD', 'some_pass');

define('DB_HOST', 'localhost'); /* this is the web address of your database server */

Finally, re-upload the file via FTP.

You can now return to your site to see if the error has been resolved.

If not, there is one last step for you to take:

Step 5: Restore The Default WordPress Files

The core WordPress files can become corrupted for a variety of reasons. Perhaps there was a hacking attempt on your site. Or you accidentally modified some files, or a rogue plugin caused the modification.

Whatever the case may be, you can still restore WordPress’s default, native files. The simplest approach to do this is to just download a fresh copy of WordPress from WordPress.org and replace the one you already have.

Then, using FTP, login to your site, navigate to the WordPress root and upload your new WordPress files. All prior files will be overwritten.

Return to your site after the upload is complete to see the result.

This is a last-ditch solution. If this doesn’t work, you’ll have no option but to call your web host’s support line and explain what you’ve done and ask for help. At this point, you’ve done everything you can to resolve the problem on your own, and it’s up to the host to do it.

How To Avoid This Error In the Future?

While there is no foolproof technique to safeguard your site against database connection errors, there are two essential pieces you should have in your insurance puzzle to be at least 90% safe:

  • Quality hosting. Only cooperate with hosts who have made their platforms WordPress-friendly. As a result, you can always rely on skilled and prompt help. Not to mention, you’ll avoid a lot of frequent WordPress issues. Our WordPress hosting recommendations are shown below.
  • Have backups. Install a basic backup plugin like UpdraftPlus (it works automatically). And you’ll always have a working WordPress backup to restore if everything else fails.

Conclusion 

Most of the time, this error occurs because of some server error, but there may be other factors as well.

If your website is still down after following the troubleshooting procedures. It could be a problem on your host’s end. Contact your web hosting company’s support team. They might be able to assist you to figure out what’s causing it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here