banner

banner

How to Fix - Error Establishing a Database Connection in WordPress

 “Error establishing a database connection” is One of the most common and fatal errors that WordPress users face.

This error occurs when WordPress is unable to make a connection to the database. It is a fatal error that makes your WordPress website inaccessible to users. Several things can affect your WordPress database connection which makes it a bit difficult for beginners to troubleshoot.

In this article, we will show you how to easily fix the error by establishing a database connection in WordPress.

Understanding The Problem

As a content management system, WordPress uses a database to store all your content and website data. Then it connects the database each time someone visits your website.

WordPress needs the following information for connecting to the database:

  • Database Server
  • Database Name
  • Database Username
  • Database Password

Common Scenarios That Cause This Error:

  • Incorrect login credentials: WordPress needs a specific login and password to access the database. If those have changed for any reason then WordPress will no longer be able to fetch anything from the database.
  • Corrupted WordPress files: The core WordPress files can get corrupted as a result of a failed update. This can include things like failed updates of the plugins, themes, and even the main WordPress software updates.
  • Corrupted database: This might be a result of a rogue or nulled or cracked Plugin messing up the database internally.
  • The database server is down: In some cases, the webserver  (the server that stores your website files) is run on a different machine and the database server is running on another machine. And that separate database server can simply be down for whatever reason.
  • Too much traffic: Even though this is a good problem to have, it’s still a problem. Specifically, your database might be unresponsive due to a high spike in traffic.

Let’s take a look at how to fix errors establishing database connection issues in WordPress with step-by-step troubleshooting.

The most common reason for database connection errors in WordPress is incorrect database credentials. If you have recently moved your WordPress site to a new host or location, then this could be the most likely reason.

Your WordPress database credentials are stored in the wp-config.php file. This is the WordPress configuration file that contains important WordPress settings including database information.

You need to make sure that the information for the database name, username, password, and database host is correct.

// ** 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' );

You can confirm this information from your WordPress hosting account dashboard or cPanel dashboard. Simply log in to your hosting account and click on MySQL databases under the database section.

Repair WordPress Database

If you are getting an error on the wp-admin, for instance, something like “One or more database tables are unavailable. The database may need to be repaired”, then you need to repair your database.

You can do this by adding the following line in your wp-config.php file. Make sure to add it just before ‘That’s all, stop editing! Happy blogging’ line wp-config.php.

1define('WP_ALLOW_REPAIR', true);

Once you have done that, you can see the settings by visiting this page: 

http://www.yoursite.com/wp-admin/maint/repair.php

Repair Database

Repair Database

Note: the user does not need to be logged in to access the database repair page. Once you are done repairing and optimizing your database, make sure to remove this code from your wp-config.php.

Check If Your Plugin or Theme Files Haven’t Been Corrupted

Your WordPress files might get corrupted as a result of misfortunate events.

For instance, maybe you’ve updated a plugin or a theme, and the whole thing collapsed? Maybe you hooked the site up to an external service? Maybe you installed a nulled or cracked plugin?

If any of the above reasons, you probably have your culprit. If you haven’t done any of that, skip this step.

Here’s what you can do:

First: you need to access your file directory, where all the files are located. You can do this by FTP or you can log in to your hosting dashboard or cPanel dashboard then select file manager. then navigate to the WordPress root folder. It’s usually called “public_html” or “www”.

Second: Then Go into “wp-content” and rename the “plugins” folder to plugins.OLD.

Third: Go back to your site and see if the error is still there.

If not, you now know that it’s been caused by some plugin. Go back to FTP, rename the folder back to “plugins”. Then Go in it, and change the folder names of the individual plugins, one by one. The easiest way to rename  is to add an underscore at the end of the name: “_”

Keep coming back to your site to see when the error gets vanishes. Once you’ve narrowed it down to one specific plugin, you have found the troublemaker. Now you can delete that specific plugin.



  • If the error is still there even after renaming the “plugins” folder, go through the exact same process with the “themes” folder.
  • If that doesn’t help, rename both folders back to “plugins” and “themes”. The error is caused by something else.

How To Avoid This Error In the Future?

There is no way that you can 100% secure your website for any errors, but to be 90% safe, like an insurance policy, you must have two main elements in this puzzle.

  • Quality Hosting: Only work with hosting providers who have optimized their platform to work on WordPress. That way, you can always count on knowledgeable support and fast help and avoid many common WordPress problems.
  • Take a backup. You should always have a working WordPress backup that you can restore if all other options fail. For example, Updraft Plus is a WordPress backup plugin that works automatically.

We hope this article helped you fix the error establishing a database connection in WordPress. And feel free to reach out to us (or leave a comment) if you’re having a tough time with an error establishing a database connection.


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.