BEST FREE WORDPRESS RESOURCES FOR ALL USERS

Error Establishing a Database Connection is one of the common and fatal WordPress errors that you might see on your site one day. This can really affect your site’s traffic and drop your leads, sales, and conversions.

Usually, this error occurs when your site is unable to connect to the specified database and it can be due to several reasons, and today I’ll explain the reasons that can cause this error.

Also, we will see that How you can easily fix the ‘Error Establishing a Database Connection’ in WordPress.

error-establishing-a-database-connection


First, Let’s see that Why the “Error establishing database connection” occurs?

What causes Error Establishing a Database Connection in WordPress?

The error is self-explanatory in that it is an error that is caused due to database connection. In simple words, connection with your database is not successful.

WordPress uses two technologies:;

1- PHP: As we all are aware that it is a programming language and the core WordPress files are written in this language.

2- MySQL: It is a database technology that WordPress uses to store all of your website’s content like posts, pages, the layout of the widgets, colors, and etc.

WordPress uses PHP instructions to connect to the MySQL database, then it will display all the required information on the screen.

When WordPress is unable to connect to the MySQL database using the PHP commands, then it displays the Error Establishing a Database Connection.

Reasons of Error Establishing a Database Connection

There are several reasons due to which this error occurs. Some of the common ones are

  • Login credentials are incorrect: To access the database, WordPress requires a unique username and password. If those have changed for any reason, WordPress will be unable to retrieve data from the database.
  • WordPress files that have been corrupted: A botched update, for example, can cause the core WordPress files to become corrupted. This can include things like plugin, theme, and even core WordPress software updates that have failed.
  • The database has been corrupted: This could be caused by a rogue plugin mucking up the database, a hacker attack, a theme failure, and many other things.
  • The database server is unavailable: The web server, which saves your website files, is sometimes run on a different system than the database. And that secondary database server could be unavailable for any number of reasons.
  • There is an excessive amount of traffic: Despite the fact that this is a nice problem to have, it remains a problem. Due to a large increase in traffic, your database may become unresponsive. Perhaps one of your postings went viral, and now everyone is trying to get on your site? As a result, the server is unable to handle a percentage of those requests.

Now that we know some of the most commons reasons for this type of error. Now, let’s see How you can fix the error by establishing a database connection in WordPress?

Fixing the ‘Error Establishing a Database Connection’

Let’s look at how to troubleshoot the error establishing a database connection issue in WordPress step by step.

1- Check Your WordPress Database Credentials

It is one of the most common reasons for Error Establishing a Database Connection. If you recently migrated to a new WordPress Hosting then probably this is the reason for the error establishing a database connection.

wp-config.php is the file in which all of your WordPress Database Credentials are stored. In that file, you will be looking at these files in the wp-config file

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

Make sure that the database name, username, password, and database host are correct.

To confirm this, log in to your WordPress Hosting account dashboard, click on the hosting tab, and then Database » MySQL Database.

check database credentials


And on the next screen, you will see the database details and user privileges. From here, you can view the database name and can confirm that the Database credentials are the same as in the wp-config file.

credentials


After that, check your website to see if the database connection error has been resolved.

If the message is still visible, it suggests that something else is incorrect. For troubleshooting tips, keep reading.

2- Check You Database Host Information

If you’re sure that your database name, username, and password are accurate, you should double-check that you’re using the correct database host information.

Localhost is the database host used by the majority of WordPress hosting businesses. Some managed WordPress hosting companies, host databases on different servers. Your database host information will not be localhost in that case.

To confirm your database host details, contact your WordPress hosting company.

3- Repair WordPress Database

In some cases, your database could be corrupted because hundreds of tables are added to your database whenever you install/remove a theme or a plugin.

And you could see the following error in WordPress Dashboard

“One or more database tables are unavailable. The database may need to be repaired”

Please keep in mind that this error will be visible on the backend while the error establishing a database connection is visible on the frontend.

In the above case, you need to repair your database and you can do this by adding the following code of line at the end of the wpconfig.php file

define('WP_ALLOW_REPAIR', true);

After adding the above code of line, visit the https://yourdomain.com/wp-admin/maint/repair.php address by adding your site address instead of yourdomain.com

And you will the following page with some repair settings

wordpress repair database


Note: To access the database repair page, the user does not need to be logged in. Remove this code from your wp-config.php once you’ve finished repairing and optimizing your database.

If you’re using cPanel, you can also repair your MySQL databases via the MySQL Databases panel. After selecting the database from the drop-down click on the repair database tab.

cpanel repair database


You can repair your database from phpMyAdmin too. Simply log into your phpMyAdmin and click on your database, select all the tables, and select Repair Tables from the dropdown.

repair database phpmyadmin


4- Fix Corrupted Files

You can see the error establishing a database connection notification is if your files have become corrupted. Before proceeding further with the fix, I’ll suggest you take a complete backup of your site.

Because we are going to replace the core version of WordPress on your site. It is just the WordPress Installation, none of your themes or plugins will be affected by this.

In simple words, we are just replacing the old WordPress files with new ones. To do this, first, you need to download a fresh copy of WordPress from WordPress.org

download wordpress


Unzip the file on your computer and delete the wp-content folder and wpconfig.php file from the extracted files.

Then upload the remaining files to your site via SFTP. This will remove all of the faulty files and replace them with new, clean, and uncorrupted ones. After that, it’s a good idea to delete your browser’s cache. Then check to see if the problem is still present on your WordPress site.

5- Check if Database Server is Down

If everything appears to be in order but WordPress still can’t connect to the database, and you are facing the error establishing a database connection then it is possible that your database server (MySQL server) is down.

In this scenario, you contact the hosting company via email or live chat.

All modern hosting providers like Hostgator, Hostinger, Bluehost, GreenGeeks, and Cloudways provide you with quick support.

Might be, you are seeing this error because of high traffic on the server. In short, your server could not handle much load and this can happen when you are using a shared hosting account.

Check out Hostgator and Hostinger hosting companies as their plans are in the budget and they provide one of the best experiences.

You can start by contacting your hosting company through live chat and most of the issues are resolved during live chat.

You can directly ask them in the live chat that you are facing an error establishing a database connection and ask them that

  • if your MySQL server is responsive
  • if the database server is up and running without errors
  • and there is no current spike in activity or traffic on the server

They will let you know either the issue is from their side or from your side and if the issue is from the hosting side then probably they’ll help you in fixing the error establishing a database connection.

If the issue is not from their side then continue reading this tutorial.

If you have other websites on the same host as your SQL server, you can check those sites to see if your SQL server is down.

If you don’t have any other sites on the same hosting account, go to your hosting dashboard and try to join the database using phpMyAdmin.

If you’re able to connect, we’ll need to see if your database user has enough permissions. Make a new file called connectiontest.php and paste the code below into it:

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

Replace the username and password. Now, upload this file to your site and access the site via a web browser.

If the script connected successfully, that suggests your user has enough permissions and something else is wrong.

Go to your wp-config file and double-check that everything is in order (re-scan for any typos).

6- Restore Latest Backup

Last but not the least, if the above-mentioned ways don’t work for you, then you can go for restoring the latest backups.

If you aren’t concerned about losing any data between now and your previous backup, this may be a faster option to resolve the problem.

Many hosts have their own backup and restoration procedures in place. Keep in mind that both your database and your files may need to be restored.

You can contact your hosting provider and ask them for help to restore the latest backup.

Note: Please keep in mind that some hosting companies and their plans have automatic backup features while some don’t have this feature. Usually, all hosting companies are providing regular backups but it depends on the plan you have.

Additional Steps

You can try the following steps if none of the above-mentioned ways work.

Update WordPress Site URL

Using phpMyAdmin, try changing the URL of your WordPress site. Simply select your WordPress database in phpMyAdmin from your hosting account dashboard.

Then, at the top of the page, select SQL and type the MySQL query below.

UPDATE wp_options SET option_value='YOUR_SITE_URL' WHERE option_name='siteurl'

Remember to give your own site URL and update wp_options to your own table name if the WordPress table prefix has changed.

sqlphpmyadmin


Rebooting Web Server

Rebooting dedicated servers, local servers, and virtual private servers (VPS) is an option for users.

This will restart your web and database servers, which may resolve some of the error’s temporary issues.

I hope this article helped you fix the error establishing a database connection in WordPress. You can also view my guide on How to start a blog and Best WordPress Themes.

If you liked it please subscribe to my YouTube Channel for WordPress video tutorials. You can also find me on TwitterFacebook, and Instagram.

SHARE

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

Leave A Comment

Join The Family

Get fresh content from WPScholor

Popular Guides

Popular Deals