How to Fix “Error Establishing A Database Connection” in WordPress
access_time
hourglass_empty
person_outline

How to Fix “Error Establishing A Database Connection” in WordPress

Is your WordPress website experiencing the “Error Establishing a Database Connection” error? Don’t panic just yet — it is actually pretty common.

Simply put, this error message indicates that your site failed to retrieve the information needed to render the page.

WordPress uses PHP as the main language and MySQL as the database. When someone visits your site, certain PHP commands will fetch your site’s information from the database. If it fails to do so, your site cannot load, and the WordPress error message will appear.

A database is software used to easily organize, store, and retrieve data into other software. Since WordPress is a CMS, it uses a database to store all of the contents of your WordPress site. Then, when a user connects to your website, it connected to the database.

As a result of an error, not only does your site become inaccessible and loses traffic, but it can also create a negative reputation.

Read on to find out how to solve this WordPress error, what it means,  and what the possible causes are.

Get up to 82% OFF for WordPress hosting packages.

Get a Discount for WordPress Hosting

How to Fix the WordPress “Error Establishing a Database Connection”?

Here are the five most common solutions you can use to try and fix the WordPress error establishing a database connection error. But before you try them, it’s recommended to download a back up of your data. You can find more information about how to do so here.

Quick Methods to Fix the Error

  1. Check the Login Credentials
  2. Repair the WordPress Database
  3. Fix the Corrupted Files
  4. Check Your Database Server
  5. Create a New Database

Method 1: Check the Login Credentials

One of the most common reasons why the error establishing a database connection messages occurs is due to the login credentials being wrong. This tends to happen after a website owner migrates to a different hosting provider. If both the frontend and backend of your site show the same error establishing a database connection error warning, this method might solve the problem.

First things first, you need to check if the database login information matches what your site stores in the wp-config.php file.

To access the wp-config file, use an FTP client like FileZilla or the WordPress File Manager in your hosting control panel.

If you use the File Manager, navigate to the /public_html/ folder, right-click on the wp-config.php file, and select Edit.edit wp-config file in file manager

Then, find and take note of:

  • DB_NAME – database name
  • DB_USER – username
  • DB_PASSWORD – password
  • DB_HOST – the host of the database

Finding the DB_NAME, DB_PASSWORD, and DB_HOST lines within the wp-config.php file on the hPanel's file manager

After that, navigate to MySQL Databases and look for your current database name under the List of Current MySQL Databases And Users section. There, you’ll find the database name, db user, and host.

List of Current MySQL Databases and Users

If the login data in your wp-config.php file is different from the MySQL database settings, match them by modifying the wp-config.php file.

Edit these lines:

define('DB_NAME', 'fill_this_with_your_current_database_name'); 
define('DB_USER', 'fill_this_with_your_current_username'); 
define('DB_HOST', 'fill_this_with_your_current_localhost');

Once you’ve edited the file, refresh your WordPress site to see whether or not the error establishing a database connection is successfully resolved.

If the error still persists, it’s possible that the value of DB_PASSWORD is still false. To fix this issue, you need to reset your MySQL password.

Simply head over to the MySQL Databases menu, click on your current database, and select Change Password. After entering the new password, hit the Update button.

If your database name, username, and host are all correct, you may want to check your database host information. With WordPress, web hosting companies tend to use localhost as your database host. Whereas managed WordPress hosting companies might use separate servers. 

If this is the case, you will need to contact your web hosting company to confirm your database host info.

Note: If you have recently switched hosts, be sure to double-check the DB_HOST details. In most cases, localhost won’t change, but some web hosts use an IP Address or different URL.

Get up to 82% OFF for WordPress hosting packages.

Get a Discount for WordPress Hosting

Method 2: Repair the WordPress Database

Your database might be corrupted if the WordPress admin dashboard shows an error message along the lines of “One or more database tables are unavailable. The database may need to be repaired.” This error will only appear on the back-end, whereas on the front-end you will see the error establishing a database connection message instead.

Thankfully, you can easily repair the database by inserting the following function into the wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Make sure to put the function right before the section “That’s all, stop editing! Happy blogging.”

inserting the following function into the wp-config.php file to try to fix the wordpress error establishing a database connection error

Once you’ve saved the modification, go to http://www.yoursite.com/wp-admin/maint/repair.php. Here you will have two options to repair your database.

Next, select the Repair Database or the Repair and Optimize Database option. Choose the one that fits you best.

After the database is repaired, don’t forget to go back to your wp-config.php file and remove the function immediately. This is important to prevent any website abuse as anyone can access the repair page URL without being logged in.

Method 3: Fix the Corrupted Files

Another possible reason for the Error Establishing a Database Connection issue is a corrupted theme or plugin.

Your theme might be the problem if the error happens after you’ve just activated a new one. To resolve the issue, simply navigate to Appearance -> Theme on your Admin Dashboard and then activate another theme from the available options.

Alternatively, you can disable all of your plugins if changing the theme doesn’t resolve the problem. After that, enable each plugin one by one to find the culprit.

If you cannot access your WordPress dashboard, deactivate your WordPress theme or WordPress plugins by renaming the corresponding folders from your hosting control panel’s file manager.

Go to the File Manager and open the wp_content folder located in the public_html directory. Then, rename the currently used theme or the plugins folder. This will disable your theme or all of your plugins at once.

rename the currently used theme or the plugins folder to disable your theme or all of your plugins at once

Keep in mind not to disable WordPress themes and WordPress plugins at the same time in order to find out the root of the problem.

For example, rename the plugins folder first and then refresh your site. If the error disappears and you can access your WordPress dashboard, then you know for sure that one of the plugins is the issue.

But how do you know the exact WordPress plugin that causes the issue?

Well, after disabling all of them, restore the plugins folder to its original name. This way, all WordPress plugins will still be inactive, but you can adjust the settings from the admin area.

activate plugins one by one

After that, activate the WordPress plugins one by one, refresh your site, and keep doing that until you find the one causing the error establishing a database connection.

Pro Tip: Don’t forget to delete the WordPress theme or WordPress plugin that causes the error.

If that doesn’t work, you can also try to re-upload the core WordPress files. To do this, download a fresh version of WordPress and unzip the package on your computer. Inside the folder, delete the config.php file and the wp-content folder. After they are deleted, open an FTP client and upload the remaining files onto your WordPress root folder.

This should now safely overwrite all of these files on your live server.

Method 4: Check Your Database Server

If you have tried the methods above and WordPress still has an error establishing a database connection, then it is possible that your MySQL server is down. 

There are many reasons why this could happen, but one is due to heavy traffic on a server. This is when your host server cannot handle the number of users trying to access it, thus establishing a connection error. 

If this is the case, your site will become slow and may output the WordPress error establishing a database connection message. This tends to happen when your host has limits on their servers. If this happens, contact your web hosting provider and have them check if your MySQL server is still responsive. 

If you have more than one site using the same server, you can double-check and see if your SQL server is down on those as well. 

If you only have one site, navigate to your WordPress dashboard and access phpMyAdmin, and connect to the database. 

phpMyAdmin in Hostinger to check your database status

If it allows you to establish a connection, we can check to see if your WordPress database user has sufficient permission. 

Create a new file called testconnection.php (or something that you will remember with the extension being .php) and paste this code in it:

<?php 
$link = mysqli_connect('localhost', 'username', 'password'); 
if (!$link) { 
die('Could not connect: ' . mysqli_error()); 
} 
echo 'Connected successfully'; 
mysqli_close($link); 
?>

Be sure to replace the username and password with your information. Then upload this file to your website and access it through the web browser.

If it successfully establishes a database connection, it means your user has enough permission, and there is something else causing the error establishing a database connection.

If your website is on a shared server and your traffic begins to increase, you may continue to overload your server and experience the error establishing a database connection. Caching plugins like, W3 Total Cache, WP Rocket, or WP Super Cache, will be a good tool to help prevent further issues.

Method 5: Create A New Database

If all of the methods above still yield no result, your database might be completely corrupted and cannot be used anymore.

In that case, you need to create a new MySQL database and user. To do so, head over the MySQL Databases top page and fill in the form.

head over the MySQL Databases and create a new database to fix the database connection error

Once a new MySQL database is created, import your database backup so you don’t have to start everything from scratch.

Bonus Methods

If none of the methods listed above work, we can try some steps that have worked for other users to fix the WordPress error establishing a database connection. 

  1. Update your WordPress – Navigate to your dashboard and access phpMyAdmin > WordPress database. Click on the SQL database menu and paste in this code:

    UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'
  2. Ask for help – There is nothing wrong with asking for help. Contact your web hosting company or their customer support team. They will be able to further troubleshoot the connection problem you are having. They might be able to fix it right away or show you how to fix it.
  3. Reboot the web server – Users on dedicated servers, local server, and VPS (virtual private servers) can also try to reboot their servers. This will restart your web and database server which could fix some errors.
  4. Restore your latest backup – This should only be done if you don’t mind possible losing some data between when you last did a backup. Check to see if your web host has its own automatic restore process to see when your last backup was. You might have to restore both your database and your files

What Causes This Error?

There are a few possible reasons that can cause the “Error Establishing a Database Connection” WordPress error message, namely:

  • Incorrect database credentials — if there is any change to your database’s username and password, the old login credentials stored in your WordPress files will be invalid.
  • Corrupted database — can be a result of a hacking attempt, plugin failure, or an incompatible theme.
  • Traffic spike — a sudden surge in demand can make your database unresponsive.
  • Database server is down – sometimes the webserver that stores your website files might be down for whatever reason.

Before making any attempts to fix the error, check whether or not the error establishing a database connection message appears on your WordPress admin dashboard as well.

Here’s how your WordPress admin area will look like:

error establishing a database connection that appears on the admin dashboard

If that’s the case, the issue lies with invalid login credentials. However, if a different error message appears, then the cause might be a corrupted database or a traffic spike.

Conclusion

Getting the “Error Establishing a Database Connection” WordPress error can be frustrating. Your website won’t load, and there’s a risk of losing traffic and customers.

The error occurs when your site fails to retrieve the information from a database. It might be caused by different reasons, such as incorrect login credentials in the wp-config.php file, WordPress theme or WordPress plugin conflicts, or traffic overload.

Try these three methods to solve the error:

  • Fix the Login Credentials — make sure to match the login credentials in your wp-config.php file with the ones in your MySQL Database.
  • Repair Database — if your database is corrupted, repair it by inserting the define(‘WP_ALLOW_REPAIR’, true); function into the wp-config.php file.
  • Fix the Corrupted Files — deactivate the theme or plugin via your WordPress dashboard, or rename the folders from your control panel.
  • Create a New Database — start over with a new MySQL database and import a backup.

Turns out the WordPress error establishing a database connection is not as scary as you may have initially thought, isn’t it? Simply follow the tutorial, and your site will be up and running in no time!

The Author

Author

Linas L. / @linas

Linas started as a customer success agent and is now a full-stack PHP developer at Hostinger. He is passionate about making a positive impact on people by utilizing user-driven development techniques. As much as he enjoys coding and changing the world, he secretly dreams of becoming a rock star.

Related tutorials

Author

Janet Reply

March 13 2018

Thank you for the detailed article,was able to fix the database connection error

    Author

    Domantas G.

    Replied on March 23 2018

    Hey Janet, Glad we were able to help you!

Leave a reply

Comment*

Name*

Email*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Become a part of Hostinger now!