Home WordPress Errors How To Fix Error Establishing A Database Connection

How To Fix Error Establishing A Database Connection

How To Fix Error Establishing A Database Connection

The “error establishing a database connection” is one of the most common and scary errors that WordPress users can face. It’s a close tie with the white screen of death (WSOD). This error means that your website is no longer communicating or has access to your WordPress database. Therefore your entire website goes down. That is not something to be taken lightly, and you should examine and fix this quickly as this can instantly affect your traffic.

But there’s nothing to worry about. In this tutorial, we’ll walk you through some common situations that can cause this error and some easy ways to get your website up and running in no time.

What is the Error Establishing a Database Connection?

All the data on your WordPress site, such as post data, meta information, login information, page data, plugin settings, etc., are stored in your MySQL database. The only data that isn’t put away there is media content, for example, pictures and your plugins/theme/core files, for instance, wp-login.php, index.php, and so on. At whatever point somebody visits your site, PHP executes the code on the page and requests for the data from the database, which at that point displays it to the guest in their browser (Chrome, Firefox, Safari…).

If for some reason, this isn’t working accurately, you’re left with the error establishing a database connection message, as seen below. The entire page is empty because no data can be retrieved to render the page, as the connection isn’t running properly. In addition to the fact that this breaks the frontend of your site, yet it will also keep you from entering your WordPress dashboard.

However, visitors probably won’t see this error on the frontend instantly. That’s because your site is still serving everything from the cache until it ends.

Error establishing a database connection in Chrome

Error establishing a database connection in Chrome

If visitors decide to access your website while this error is happening, it will generate a 500 HTTP status code in your logs. That same status code will appear if there is an “internal server error.” It means that something went wrong on the server, and the requested resources were not delivered. When everything is working well, your site will generate a 200 HTTP status code, which indicates that everything is excellent.

Common Situations That Cause This Error

Why is this happening? Well, there are a few simple reasons below. We will go into each of these more in-depth so you can learn how to fix them. Typically you can fix this error in under 20 minutes.

  • The most common issue is that your database login credentials are incorrect. Your WordPress site handles separate login information to connect to its MySQL database.
  • Your database is corrupted. With so many changing parts with themes, plugins, and users continually deleting and installing them, sometimes databases get damaged. This can be due to a missing or individually damaged table, or possibly some information was deleted by accident.
  • You may have corrupt files in your WordPress installation. This can even appear sometimes due to hackers.
  • Issues with your database server. Numerous things could be wrong on the web hosts end, such as the database being overloaded from a traffic spike or unresponsive from too many coexisting connections. This is actually common with shared hosts as they are using the same resources for a lot of users on the same servers.
SEE ALSO  How To Fix Cannot Modify Header Information In WordPress

How to Fix the Error Establishing a Database Connection?

Before you start fixing the error, we advise you to take a backup. A lot of the recommendations below require manipulating data in your database. Hence you don’t need to make things more critical. You should always backup before attempting to fix something on your WordPress site.

You can use a well-known WordPress backup plugin such as WP Time Capsule or VaultPress to backup your files and your database.

5 Easy Fixes for Error Establishing a Database Connection in WordPress:

  1. Check whether your database login credentials are correct (the most obvious reason for the issue).
  2. Fix a corrupt database with WordPress’ built-in database repair mode: define(‘WP_ALLO W_REPAIR’, true);
  3. Fix corrupt files
  4. Verify with your hosting provider for issues related to your database server
  5. Restore to your latest backup

1. Check Your Database Login Credentials

The very first thing to do is to check that your database login credentials are right. This is by far the most obvious reason why the error establishing a database connection message occurs, mainly when someone transfers to another hosting company. The connection details for your WordPress site are saved in the wp-config.php file, which is usually located at the root of your WordPress site.

It includes four essential pieces of data that all need to be accurate for the connection to take place successfully.

Database Name

// ** MySQL settings ** //
/** The name of the database for WordPress */
define('DB_NAME', 'xxxxxx');

MySQL database username

/** MySQL database username */
define('DB_USER', 'xxxxxx');

MySQL database password

/** MySQL database password */
define('DB_PASSWORD', 'xxxxxxxxx');

MySQL hostname (server)

/** MySQL hostname */
define('DB_HOST', 'localhost');

To reach your wp-config.php file, you need to connect to your site via SFTP, browse to the root of your website. Or, you can use cPanel, then choose “File Manager,” navigate to the root of your site, and right-click to edit the file.

cPanel file manager

cPanel file manager

This is what the file looks like when displayed.

wp-config.php credentials

wp-config.php credentials

You need to quickly check your current values into those on your server to guarantee that they are correct.

Check Database Credentials in cPanel

You need to check the database name. To do that, you will need to login to phpMyAdmin in cPanel under the Databases section.

cpanel phpmyadmin

cPanel phpMyAdmin

On the left-hand side, you should see your database name at the bottom. You can ignore the “information_schema” database as this is something used by the host. You will then want to compare that name against the DB_NAME value in your wp-config.php file. If they match, then this isn’t the problem. If they don’t match, then you need to update your wp-config.php file.

cPanel database name

cPanel database name

You can also confirm that this is the correct database by ensuring that it includes your WordPress site’s URL. To do this, just click on the database, then click into the wp_options table (this might be labeled something slightly different for security purposes, such as wpxx_options). At the top of the table, you will see values for your site’s URL and name. If these match your current website, you can rest assured that you’re in the right place.

Check site URL in phpMyAdmin

Check site URL in phpMyAdmin

If your database name was already correct and you are still seeing the error establishing a database connection message, then you will also require to check your username and password. To do this, you will need to create a new PHP file in the root directory of your WordPress site and insert the following code. You can name it whatever you want, such as checkdb.php. Just change the values of db_user and db_password with those that are in your wp-config.php file.

$test = mysqli_connect('localhost', 'db_user', 'db_password');
if (!$test) {
die('MySQL Error: ' . mysqli_error());
echo 'Database connection is working properly!';

Then go to the file on your WordPress site: https://yourdomain.com/checkdb.php. If you get a “MySQL Error: Access denied,” then you know your username or password is wrong, and you will need to proceed to the next step to reset your credentials.

Access denied mySQL

Access denied MySQL

Here is the message you need to see, “Database connection is working properly.” But of course, if it was, then you wouldn’t be here. Make sure to delete/remove this file after you are done testing.

Database connection working properly

Database connection working properly

Next, you need to reset your username and password. In cPanel, click on MySQL Databases under the Databases section.

cPanel MySQL databases

cPanel MySQL databases

Scroll down and create a new MySQL user. Choose a unique username and password so that they can’t simply be guessed. The password generator tool they provide works excellent. Then click on “Create User.” You could alternatively also change the password on this screen for the current database user that already exists.

Create new MySQL user

Create new MySQL user

Next, scroll down and add your new user to your database. The next screen will require which privileges you want to assign, select “All Privileges.”

Add user to database in cPanel

Add the user to the database in cPanel.

Then take those new credentials and update your wp-config.php file. You will want to update the DB_USER and DB_PASSWORD values. You could also rerun the test file from earlier. This should then fix your credentials issue. If not, it could be that you still have the wrong hostname (DB_HOST). Some hosts use various values, see a list of some common DB host values. Typically this will simply be localhost. But you can always go back to your hosting provider or view their documentation if you aren’t sure. Some might also use instead of localhost.

SEE ALSO  How To Fix Cannot Modify Header Information In WordPress

2. Repairing Corrupt Database

In some situations, it could be that your database has become corrupt. This can hardly happen (although not very often) as, over time, hundreds of tables are continually added/removed by new plugins and themes. If you try to login to your WordPress site’s dashboard and are receiving the following error, it means your database is corrupt: “One or more database tables are unavailable. The database may need to be fixed.” It is necessary to note that you might only see this error on the back-end, whereas you see the error establishing a database connection message on the frontend.

WordPress has a database repair mode which you can initiate. Simply add the following to the bottom of your wp-config.php file.

define('WP_ALLOW_REPAIR', true);

WordPress repair mode

WordPress repair mode

Then browse to the following location on your WordPress site: https://yourdomain.com/wp-admin/maint/repair.php. You will then have the option to repair the database or repair and optimize the database. Since you are apparently troubleshooting an interruption on your site at the moment, we advise going with the repair database option as it is faster.

WordPress repair database

WordPress repair database

After running the repair of the database above, ensure that you delete the line of code you added to your wp-config.php file. Otherwise, anyone could run the repair. If you are running cPanel, you can also run a repair from within the MySQL database screen.

cPanel repair database

cPanel repair database

Or you could run a repair from within phpMyAdmin. Simply log in to phpMyAdmin, click on your database, and select all the tables. Then from the dropdown, click on “Repair table.” This is essentially just running the REPAIR TABLE command.

Repair tables in phpMyAdmin

Repair tables in phpMyAdmin

Finally, your other option would be to run the repair using WP-CLI with the following command:

wp db repair

See more documentation on usage in the WordPress developer resources.

SEE ALSO  How To Fix A Website With Blocked Mixed Content In WordPress

3. Fixing Corrupt Files

The next possible reason you might be seeing the error establishing a database connection message is that your files have become corrupt. Whether this started from an issue with transferring files via FTP, a hacker obtaining access to your website or a problem with your host, you can quickly fix this. But, again we recommend taking a backup of your site before trying this.

You are going to replace the core version of WordPress on your site. You aren’t touching your plugins, themes, or media, just the WordPress installation itself. To do this, you will need to download a recent copy of WordPress from WordPress.org.

Download WordPress

Download WordPress

Extract this file on your computer. Inside, you will need to delete the wp-content folder, as well as the wp-config-sample.php file.

Delete wp-content folder

Delete wp-content folder

Then upload the remaining files via SFTP to your site, overwriting your existing files. This will replace all the uncertain files and guarantee you have fresh ones that are clean and not damaged. It is recommended to clear your browser cache after doing this. Then check your WordPress site to see if the error still occurs.

4. Issues With Your Database Server

If nothing of the above has helped resolve your issue, then we highly recommend checking with your hosting provider support team as it could be a problem with your database server. For example, if there are too many concurrent connections to your database at once, it could cause the error. This is because a lot of hosts have limits on their servers on how many connections are allowed at once. Using a caching plugin can help minimize the database interactions on your site.

5. Restore Latest Backup

And last but not least, you can constantly resort to a backup if wanted. In some situations, this might be a faster way to resolve the issue if you aren’t concerned about losing any data between when your last backup was taken. Many hosts have their backup restore process. Remember that you might need to restore both your database and your files.