Introducing | Kaon — Creative Website Builder for WordPress & WooCommerce

Home How to Fix the Error Establishing a Database Connection in WordPress

How to Fix the Error Establishing a Database Connection in WordPress

Learn how to fix the Error Establishing a Database Connection issue in WordPress with these easy steps to follow.
Error reconnecting to the database
Learn how to fix the Error Establishing a Database Connection issue in WordPress with these easy steps to follow.

How to Fix the Error Establishing a Database Connection in WordPress

Error establishing a database connection is another ambitious WordPress error that leaves you wondering as to what shall you do next. Where do I start to troubleshoot this WordPress error. 

When encountering the error establishing a database connection issue this means that your website is no longer communicating or has access to your WordPress database and is down.

This is not good for no website owner, especially online stores as it will affect sales, traffic, and analytics. There are a number of things that can be causing the disconnection between your website and the database, and we know it might sound taunting for WordPress beginners to be left up to such tasks. 

That is why in this article we will lay down all the reasons as to why is this error establishing a database connection issue is occurring and what are the best and common approaches to fix this problem. 

What is the Error Establishing Database Connection Issue

To understand the error establishing database connection better let’s break down how WordPress works. All the files of your website such as post data, meta information, plugin settings, login information are stored on your MySQL database. On the other hand all the media content like images, your theme/plugin/core files are stored on PHP files like index.php, wp-login.php, and others. 

When users visit your website, the PHP will implement the code on the page and will require to connect to the database in order to display your website to the user. And when this connection does not carry out then you will encounter the error establishing database connection issue. 

WordPress will need the database name, username, password, server to connect to your database. All this information will be store in your wp-config.php file. The reason why there might be trouble connecting your website to your database includes incorrect credentials from the ones listed above, the database server could be down, or if the database files are corrupted. 

Let’s go through the options one by one to see how you can resolve the error establishing database connection and help you follow the best practices for this issue. This is the WordPress configuration file that contains all the crucial WordPress settings for your website. 

To edit this file you will need to connect your website to your FTP client. The wp-config.php file is usually located in the root folder of your website with other folders like /wp-content/.  Click on the file and it will start downloading, you can open it and edit it using a plain text editor like Notepad. 

You will need to look out for the lines below 

How to Fix the Error Establishing Database Connection in WordPress

Error Establishing a Database Connection Neuron Themes 1

Each of these practices will require you to check out what the issue is and then start attacking the problem from its root. We will display all the most common practices to fix the error establishing database connection as a WordPress error in a few steps will require you to make edits and changes in your core WordPress files. For such a case, it is required to take a full backup of your website before you start fixing the problems.  

Check the Database Credentials

The most common reason as to why you might see the error establishing database connection is when the WordPress database credentials are wrong, especially if you have migrated your website to a new hosting provider, this might be a likely cause. 

As mentioned, all your credentials are stored in the wp-config.php file. This is the configuration file that contains all the crucial WordPress settings including your credentials. You will need access to this file in order to edit and make the changes necessary. 

First connect your website to your FTP client. Once done with that, locate the wp-config.php file that is usually stored in the root folder of your website with other folders like /wp-content/. Next, click on the file and the FTP client will start the download process on your computer. Open it using a plain editor tool like Notepad and look for lines like. 

// ** 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 will need to make sure that the information for the database name, username, password, and database hot is all correct and the same. If these values are not correct then WordPress can’t connect to your database thus showing the error.

file manager cPanel 1

Locate the wp-config.php file and it should look something like this

wp config php file root file

Now that you found your credentials you must confirm them.

To confirm your credentials, you can either do that from your hosting provider account dashboard or login to your cPanel and in both cases, you should locate the phpMyAdmin under the Databases. This will take you to the database management dashboard and from here you can find all the needed information and credentials. 

cPanel myphpadmin

Now that you have the credentials from the wp-config.php file and the credentials from the database you will have to compare the two and see if there are any changes and check if the credentials on your wp-config.php file are correct. If not then you will need to take the new credentials from the database and paste them in your “wp-config.php” file where the old ones used to be. 

Go back to your website and try access it. In case the credentials are all correct then we suggest you move onto the other approaches until you find out what cased the error establishing database connection. 

Check if you have a corrupt database

If you have checked your credentials and they seem to be correct then it might be the case that your database has become corrupt. To know if this is the case for you, it will show a slightly different form. This WordPress error will appear in the following arrangement “One or more database tables are unavailable. The database may need to be repaired.”. 

Note that this message will appear only in the backend, whereas the error establishing a database connection will appear on the front end. In such case WordPress comes with a database repair mode that you can initiate. To start the process simply copy the following code and add it at the bottom of your wp-config.php file, which we showed how to locate above. 

    define('WP_ALLOW_REPAIR', true);

Once you’ve added the code on your wp-config.php file, the next set would be to go to this page http://www.yoursite.com/wp-admin/maint/repair.php. From there you will have the option to repair the database or prepare and optimize your database. The repair database option will take less time to finish, so if you are in shortage of time then go with that option. 

wordpress repair database Neuron Themes

Once you are done with these steps make sure you delete the line of code you added on your wp-config.php file as it will leave your website vulnerable for everyone to repeat the database. 

Check to see if you have corrupted files

If none of the fixes for far haven’t worked, there is a good chance that your files have gone corrupt, whether that be while transferring files via FTP or there was a problem with your host, this is a problem that can be fixed quickly. This will mean interference with the core WordPress files, and in such cases, a full website backup is most needed. 

We are going to install a fresh WordPress installation and replacing the core version of WordPress on your website. We will not delete r replace any of the files like themes or plugins, only the WordPress installation. 

  • Download a fresh copy of WordPress from wordpress.org  
  • Next, unzip the file you downloaded from your computer. Make sure to delete the wp-content folder and the wp-config file because in this way we won’t have them overwriting your existing wp-config.php file. 
  • After that, you will need to upload the remaining files by connecting to your FTP client or via the File Manager. This will have all the corrupted files replaced and ensure that all your files are safe and not corrupted. 
  • Before you check your website make sure to clear your browser cache. 
Reinstall WordPress to fix the error establishing database connection issue

Check if your database server has issues

If you’ve tried all the methods above and the error establishing database connection is still apparent then, it would be smart to see if your database server is down. This might happen due to heavy traffic on your website, more than your hosting plan can handle. 

This is because many hosting providers offer a limit to how many connections are allowed on their server. To try and fix this you can use a caching plugin to minimize the interactivity of your database. 

Another case scenario would be to contact your hosting provider and check if your database server is down. They will be able to assist you and let you know if your server is down, also help you locate the problem and fix it in most cases. 

Restore to the latest WordPress Backup

If all fails down there is another route you might fix the error establishing database connection issue on your WordPress website, that is to restore t your latest backup. This might be a faster way of resolving the issue, but always if you have no data to lose. Note that you will need to restore your database and your files. You could do that with the help of a plugin and ease the process of doing it manually. Check the full guide on how to restore your WordPress website

By Snowy Smith

Director of product design, Freehand

Introducing Kaon WordPress Theme