Fixed a bug: WordPress database connection error

Fixed a bug: WordPress database connection error

The “Database Connection Error” is probably one of the most common and frightening errors that WordPress users can encounter. It is certainly closely related to the “White Screen of Death” (WSOD). This error means that your website can no longer communicate with or access the WordPress database, causing the entire website to crash. This should not be taken lightly, and you should try to resolve this problem immediately, as it can directly affect your sales, traffic, and analytics.

But don’t worry, today we’re going to discuss some common scenarios that caused this error, as well as some easy ways to get your site up and running in no time.

 

What causes a database connection error in WordPress?

What causes a database connection error in WordPress?

The “Database Connection Error” problem can be caused by incorrect database information in your WordPress settings, a corrupt database, or an unresponsive database server. A database is a software that allows you to easily store, organize, and retrieve data in other software.

As a content management system, WordPress uses a database to store all of your content and other website data. It then connects to the database every time someone visits your site.

The following information is required to connect to the WordPress database:

  • Database name
  • Database user name
  • Database password
  • Database Server

This information is stored in your WordPress configuration file, called wp-config.php.

If any of these items are incorrect, WordPress won’t be able to connect to your database server and you’ll get a “Database Connection Error” error message.

This is one of the most common WordPress errors. In addition to incorrect login information, this error can also occur if the database server is down or the database files are corrupted.

Let’s look at how to fix a WordPress database connection error with a step-by-step troubleshooting guide.

 

How to fix a WordPress database connection error

How to fix a WordPress database connection error

Since we value your time, we’ll first recommend a method that will likely fix the problem quickly. Then we’ll go ahead and show you how to do all the fixes manually.

Contact your hosting provider

If your site was up a minute (or a day) ago and no longer works, it may be your web host’s fault. These days, all quality hosts offer some form of quick help, usually via live chat.

Start by contacting that live chat, say you have a “database connection error” (insert sentence), and ask :

  • if the database server is running with no problems, and
  • if there are no bursts of activity or traffic on the server at the moment.

In most cases, if you tell them what your error is, they will either tell you exactly what caused it or fix it for you.

More importantly, they will tell you if your database is in good shape and if there is suspicious activity on the server.

Make sure your plugin or theme files aren’t corrupted

Your WordPress files can be corrupted by a series of unfortunate events.

For example, you may have updated a plugin or theme and it went away? Maybe you connected the site to an external service? Maybe you changed some folders manually?

If any of the above conditions apply, you are probably the culprit. If you didn’t do any of these things, skip this step.

Here’s how to do it:

1.) connect to your host via FTP

To connect, you’ll need a username and password for your FTP account. If you don’t know where they are, the easiest way to find them is in the emails you received from your host when you registered. You can also ask a support representative in the chat room for these details.

Once you get them, log in to the server and go to the WordPress root directory. This is usually called “public_html” or “www,” or you’ll already be there once you establish an FTP connection. Usually, if you see subfolders like “wp-content” and “wp-admin,” then you are in the WordPress root.

2.) Go to “wp-content” and rename the “plugins” folder to something else (any name, as long as it’s not “plugins”):
3.) Go back to your site and see if there is still a bug.

If not, you now know it was caused by a plugin. Go back to FTP, rename the folder to “plugins.” Enter and change the folder names of each plugin one by one.

The easiest way is to add an underscore at the end: “_”. Keep visiting your page to see when the error occurs or disappears. Once you’ve narrowed it down to a particular plugin, you’ve found the troublemaker. Either delete it or go online to see if the fix has been documented.

  • If the bug still exists after renaming the Plugins folder, do the same with the Themes folder.
  • If that doesn’t help, rename both folders to “plugins” and “themes”. The error is caused by something else.
Make sure your database isn’t corrupted

The WordPress database is a pretty complicated thing, and there are a lot of little cogs that have to work together perfectly to avoid problems. Sometimes things get out of hand, and that’s when you see an error when connecting to the database.

  • The first thing to check is to go to your WordPress dashboard – YOURSITE.com/wp-admin.

If you see the same basic error, “Database connection error,” skip this step.

  • Second, if you see something different, pay close attention to what WordPress suggests you do. WordPress has expanded its troubleshooting service in new versions, so you may find some good tips there.

Another possibility is if you see a message that starts with “One or more database tables are unavailable […].” This means that you are the one who has to “repair” the database.

This is actually quite simple. First of all, reconnect to your site via FTP. Go to your WordPress root directory and download the “wp-config.php” file to your desktop.

Edit this file in Notepad, add an extra line at the end (just before “Like this, stop editing”):

define(`WP_ALLOW_REPAIR`, true) ;

Now upload the file to your site (via FTP), overwriting the old version.

Go to YOURSITE.com/wp-admin/maint/repair.php

Click on one of the two buttons. When the automatic repair is complete, go back to your site and see if there is an error.

If there are no more errors, so much the better! But you’ll still need to return your “wp-config.php” file to its previous state. Delete the line you just added and download the file again.

Check your database login credentials

As mentioned earlier, WordPress uses a special username and password to connect to the database. Normally, these credentials don’t change on their own. However, strange scenarios can arise after you make changes to your site files, hosting environment, or other things. In other words, even if it’s not supposed to happen, sometimes links change. And with the wrong credentials, a “database connection error” occurs!

The fix is made in the file “wp-config.php”.

We are talking here specifically about these strings:

Define(`DB_NAME`, `some_db`) ;
Define(`DB_USER`, `some_username`) ;
Define(`DB_PASSWORD`, `some_password`)
Define(`DB_HOST`, `localhost`) ;

These contain all the information WordPress needs to connect to the database. If any of this data is incorrect, the link will not be established.

Note that there is a manual way to solve this problem, but honestly, it requires several steps and can be very confusing depending on the host and the interface used. In other words, whatever we say will only apply to a small group of users using the same host as us anyway, so we have a much more universal solution for you:

Take these four lines from the file “wp-config. php”, just copy them to the clipboard, contact your host’s support again, paste those lines into the chat window, and ask if they’re correct so you can connect to your database.

The support person will check everything and then give you feedback. Most of the time, he will let you know the exact details if anything has changed.

All you have to do is take the new credentials and put them in your `wp-config.php` file where the old ones were.

define(`DB_NAME`, `some_db`); /* 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 the database server */

Finally, download the file again via FTP. At this point, you can go back to your site and see if the error is fixed.

Restoring the default WordPress files

As mentioned earlier, core WordPress files can be corrupted for a number of reasons. Maybe there was an attempt to hack your site, maybe you accidentally changed some files, maybe a malicious plugin made the change.

In any case, you can always restore the default WordPress source files. The easiest way is to download a clean version of WordPress from WordPress.org and copy it over the one you currently have.

Download the latest version to your desktop. Unzip. Go through the unzipped archive and delete the “wp-content” folder.

Then log in to your site via FTP, go to the main WordPress directory and download the new WordPress files. Overwrite the previous files. When the upload is complete, go back to your site and see the effect. This is the last resort to fix the problem.

If that doesn’t work for you, you have no choice but to contact your web host’s support team again, tell them everything you’ve done, and ask for help. At this point, you’ve pretty much-exhausted everything you could have done to solve the problem yourself, and now it’s the host’s turn.

 

Frequently Asked Questions

  1. Contact your hosting provider.
  2. Make sure your plugin or theme files are not corrupted.
  3. Make sure your database is not corrupted.
  4. Check your database links.
  5. Restore WordPress default files.

A "database connection error" is probably one of the most common and scariest errors that WordPress users can encounter. This error means that your website can no longer communicate with or access the WordPress database, causing the entire website to crash.

With phpMyAdmin, you can also easily import your WordPress database. Simply run phpMyAdmin and select your WordPress database. Then you need to click on the "Import" link on the top menu. On the next screen, click "Select File" and then select the backup file of your database that you downloaded earlier.

The "Database Connection Error" problem may be caused by incorrect database information in your WordPress settings, a corrupt database, or a non-responding database server. A database is a software that allows you to easily store, organize, and retrieve data in other software.

Leave a Reply

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