5 Easy Hacks To Fix Establishing A Database Connection Error In WordPress

By Mohit Surati WordPress March 20, 2019 11 min read 1036 Views

Are you someone who has been working as a WordPress Developer? Then, you must be knowing about the Most Common WordPress Errors.

One of the errors that are giving sleepless nights to the WordPress developers as well as the WordPress website owners is Establishing A Database Connection Error.”

What Does Establishing Database Connection Error Mean?

To understand the Error Establishing A Database Connection in WordPress, you need to break down the ecosystem of WordPress in two major parts.

WordPress is built using PHP & MySQL. PHP is in charge of all the functions on your WordPress website, while MySQL is in charge of all the things related to the data storage.

For example, when you type any URL in the address bar and hit enter, the role of PHP comes into play where it will fetch all the contents of your homepage (index.php) from the MySQL database and media files are stored in the wp-content folder.

MySQL database contains all the essential information which PHP needs to create your website. Now, when PHP can’t access your MySQL database for some reason, then it will display “Error establishing a database connection” as shown below.


Root Causes Of Establishing A Database Connection Error

In the previous section, you’ve understood the basics of Establishing A Database Connection Error. However, the question that remains is, Why Do I Get A Database Connection Error In WordPress? Here’s the list of common reasons for this type of error:

  • Wrong Login Credentials
  • Corruption Of Database
  • Database Server Is Down
  • Corrupted WordPress Files

Ways To Fix Database Connection Error In WordPress

1. Check If Your Database Is Corrupted or Not

If you search on the web about How To Fix Error Establishing A Database Connection, then this is one of the solutions that you’ll find on most of the websites. Now, you may be wondering that how to check for the corrupt database isn’t it?

There are some signs which can tell you that there is a corrupt database. They are as follows:

  • You see your website regularly, but when you’re trying to access wp-admin, you’re getting the error.
  • Your website is down, but you’re trying log in to your site you’re getting an error – “One or more database tables are unavailable. The database may need to be repaired.”

When these types of issues occur, then you need to repair your database. Now, the WordPress ecosystem provides you with an inbuilt feature to restore your database. However, to use the feature, you need to enable it.

For that purpose, you need to move your wp-config.php file which contains WordPress configuration & installation settings. To work with this file, you need to be aware of the Configuration Tricks Of WordPress.

You can find the wp-config.php file in your WordPress root directory. For accessing that, you need to login into your cPanel & then, move to the File Manager as shown in the screenshot.

Once you reach there, then navigate to the WordPress root folder as shown in the screenshot.


Now, move to the public.html folder where you will be able to find the wp-config.php file. Once you’ve access to the wp-config.php file, then right click on that and choose the code editor which will open the wp-config.php file in a new tab.

After that, add the following code to the wp-config.php file.

define ( 'WP_ALLOW_REPAIR' , true);

Once you enter this line, it will allow you to optimize and repair your database. For that purpose, move to http://www.yourwebsite.com/wp-admin/maint/repair.php. Don’t forget to replace your website with your actual URL.

Once you type this URL in your address bar, you will see a whole new window as shown in the screenshot below.

wordpress-database-repairHere, you will see two options – Repair Database & Repair and Optimize Database. You can choose an option. However, remember that optimization will take a little bit more time.

One thing that you should keep in mind is that database repair is not secure. Anyone will be able to access the repair database URL without even log in to your website.

Therefore, once set up the database repair, be sure to remove the line of code that you’ve added to the wp-config.php file to enable the database repair. Once you remove that code, it will prevent the users from accessing the repair page.

2. Check Your Database Login Settings

If the above solution didn’t solve your issue, then you should look at your database login settings in the wp-config.php file. You may be wondering how the database credentials have stopped working suddenly.

The reason behind that could be switching hosting provider or changing anything related to their username & password and not updating that details in the wp-config.php file. So, correct this issue, again move back to the wp-config.php file by logging into cPanel.

Once you open that file for editing, you will see something like this as shown below in the coding snippet.

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

As you can see, there are four pieces of information that WordPress needs to access the database. They are as listed below:

  • The name of the database (“DB_NAME”)
  • The login username (“DB_USER”)
  • The login password (“DB_PASSWORD”)
  • The database host (“DB_HOST”)

Now, if any of these values are incorrect, then WordPress won’t be able to access the database. One of the most popular ways to check your database outside of WordPress is by using the phpMyAdmin, a tool for accessing & editing the MySQL databases.

For accessing phpMyAdmin, you need to move the cPanel of your WordPress website. In the cPanel, move to database tools where you will find phpMyAdmin as shown in the screenshot.


Once you click on the phpMyAdmin, you will a login screen. Enter the appropriate credentials and click on the ‘Go’ button which will redirect you a whole new window as shown in the screenshot below.


Now, click on the Databases link which will open a whole new window as shown in the screenshot below.


Here, check if you can find a database name that appears after the DB_NAME in your wp-config.php file.

If you can find that, then you’re pretty sure that there is no issue in your database name and if you can’t see that, then you need to contact your WordPress Hosting Provider, as the problem may be lying in your server configuration.

However, if your database doesn’t exist, then you need to restore your database from the backup, or you need to create a whole new database.

3. Check Your Database Username & Password

Let us assume that, you’ve found your database in phpMyAdmin and you also have the correct database name set in the wp-config.php file.

If these conditions apply, then you need to check the database username and password, as the issue of Establishing A Database Connection Error In WordPress may lie in the credentials.

For that purpose, you need to create a .php file and store it in your WordPress root folder. So, create a file and name it as per your comfort. Here, we have named this file as check.php.

Once you create the file, it will appear in your root folder. Right click on it and choose Code Edit which will lead you to a blank page. Here, you should add the following code.

$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
echo 'Connected successfully';

Be sure to replace the ‘localhost,’ ‘root’ and ‘password’ with your database host, username and password in wp-config.php file respectively. Now, move to yoursitename.com/check.php.


Don’t forget to replace yoursitename with your site URL. If you get a message – “Connected Successfully,” then your login details are correct.

However, if you get “Could not connect: Access denied” or some other error, there is something wrong with your host, username or password.

In this type of scenario, you need to contact a developer. So, Hire WordPress Developer from a reputed organization. He/She will provide you with a world-class solution.

4.Creating New Database User & Password

If you can’t solve the database connection error using any of the above methods, then you should focus on building a new database user and password.

To solve Error Establishing A Database Connection from cPanel, move to database tools and find phpMyAdmin tab as shown in the screenshot below.


Now, click on the MySQL Databases and navigate down to MySQL Users where you will notice the Add New User section as shown in the screenshot below.


Now, enter a new username & password as per your need and click on the ‘Create User’ button. Don’t forget this details for your wp-config.php file.

Once you create the user, you need to assign the user to your WordPress database. It is the database which you see after DB_NAME in your wp-config.php file.

For that purpose, go to MySQL Databases screen once again and then, scroll down to Add User to Database section as shown in the screenshot below.


Select the user as well as the database that you’ve just created. Click on ‘Add’ button which will redirect you to a whole new window as shown in the screenshot.


Here, you will be asked to choose the user privileges. So, select the opportunities as per your needs and save all the changes. Move back to the wp-config.php file and update this file with all the new information related to the database.

While updating these changes, you need to keep few things in mind. Firstly, there shouldn’t be any space between the quotes. For example, ‘ your-username’ is bad, while ‘your-username’ is right. In addition to these, use single quotes instead of double quotes.

After making all the changes, save the file. Now, try to access your WordPress website. If you’re still getting the error, then that could be an issue of corrupted WordPress files which we will analyze in the next section.

Read also: Top 10 WordPress Comment Plugins For Increasing User Engagement Rate

5.Fixing Corrupted WordPress Files

If the above four ways haven’t solved your problem, then you should verify if there are corrupt WordPress files or not. The reason for that is, many times it was found out as a reason for establishing a database connection error in WordPress.

To solve this error, you need to restore the WordPress files. However, while doing this, you have to be careful. The reason behind that is, a tiny mistake here can wipe out your entire WordPress site. Firstly, you need to back up your entire WordPress website before proceeding.

Download a fresh copy of WordPress from WordPress.org. Unzip the entire package on your PC and then, delete the wp-config.php file as well as the wp-content folder.

By doing this, you’re ensuring that your wp-config.php file won’t break up. It will also provide that you don’t the themes, plugins, and media stored in the wp-content folder.

Once you’ve all the above aspects covered, upload the remaining files to WordPress root folder using FTP or cPanel which will remove all the problematic files. After doing this, try reloading your WordPress website.

Most probably your error will resolve, however, if it’s doesn’t happen, then clear your browser’s cache and try reloading your site again which will solve the issue.


‘Establishing A Database Connection Error’ is one of the most common WordPress errors. In spite of that, WordPress Developer and Custom WordPress Development company are not able to solve this error by themselves. In fact, this error is giving them sleepless nights.

Taking this scenario into consideration, here we have tried to provide you with a list of 5 ways through which you can fix the establishing a database connection error in WordPress.

Which of these ways are you following to solve the database connection error? Do let us know in our comment section. We’ll try to respond to each of your queries. Thank You.!

If you’re looking for an IT company who can guide you in any of the WordPress errors and provide you with robust solutions, then feel free to connect with us. Hire Our Accomplished WordPress Developers and avail a high-quality WordPress Development Services which can grow your business.


Disclaimer: We at eSparkBiz Technologies have created this blog with all the consideration and utmost care. We always strive for excellence in each of our blog posts and for that purpose, we ensure that all the information written in the blog is complete, correct, comprehensible, accurate and up-to-date. However, we can’t always guarantee that the information written in the blog correct, accurate or up-to-date. Therefore, we always advise our valuable readers not to take any kind of decisions based on the information as well as the views shared by our authors. The readers should always conduct an in-depth research before making the final decision. In addition to these, all the logos, 3rd part trademarks and screenshots of websites & mobile apps are the property of the individual owners. We’re not associated with any of them.

Mohit Surati

Mohit Surati

A post-graduate in Computer Engineering. Passionate about innovative writing & Content Marketing. A rich vein of experience in writing articles related to WordPress right from plugins, themes, customization, security, WooCommerce & Gutenberg. Pens down his thoughts for eSparkBiz @Custom WordPress Development Company where you can Hire WordPress Developer as per your requirements.


Follow us

Subscribe Newsletter

Call us Today


Got any Questions?
call us Today!

+1 408 850 1488 [email protected]

Our Guides

Related Post

Top 8 Cryptocurrency & Bitcoin Plugins For WordPress

Top 8 Cryptocurrency & Bitcoin Plugins For WordPress

“BITCOIN is a remarkable cryptographic achievement and the ability to create something that is not duplicable in the digital world has enormous value.” - Eric…

How To Make Millions By Starting A Travel Blog In WordPress – 2020?

How To Make Millions By Starting A Travel Blog In WordPress – 2020?

It clearly shows that you should also opt for WordPress for starting your travel blog. Taking this into consideration, today we’re going to provide you…

11 Easy Hacks To Make Your WordPress Website Accessible

11 Easy Hacks To Make Your WordPress Website Accessible

Web Accessibility Guidelines Web Accessibility means everyone who visits your website should be able to access everything. Even if the person has a condition that…

Share via
Copy link