“Error Establishing a Database Connection” – How to Fix it

Error Establishing a Database Connection

Is your website homepage greeting you with a message “Error Establishing a Database Connection”?

You start to freak out!

We totally understand why.

This is one of the most dreaded error because it doesn’t come with details of what went wrong. And to add to the stress, there are several things that can actually lead to this error.

In this post, we will go over each of these things and try to fix error establishing a database connection.

Let’s start by examining what this error actually means:

What Does “Error Establishing a Database Connection” Mean?

WordPress uses two languages – PHP and MySQL. All your website’s information is stored in a MySQL database and PHP is used to store and retrieve information from that database.

In simple words, when you type your URL in the address bar and hit enter, PHP fetches and displays the contents of the homepage from your MySQL database.

Error Establishing a Database Connection

source: Cloudways

Coming back to the error, sometimes PHP can’t access your MySQL database to retrieve the information it needs. When the connection between MySQL-PHP is compromised for some reason, you see the message “Error Establishing a Database Connection”.

Troubleshoot “Error Establishing a Database Connection”

Understanding what this error means is quite simple. But trying to detect the exact reasons for this error is a little complicated.

Here are some of the most common reasons for this error:

1. Incorrect Login Details

2. Database Username and Password Error

3. Corrupt Database

4. Corrupt WordPress Files

5. Problems with The Server that Hosts your Database

Now, let’s try and fix these errors one by one:

Note: Before making any attempt to fix the issue, I advise you to take a backup!

1. Database Login Credentials

Your Database Login Credentials can go haywire if the wp-config.php file is not updated properly by the system. Wp-config.php is the most essential file of WordPress as it contains all the database information.

  • To open this file Launch Filezilla > WordPress install folder > public_html.
  • Right-click on the wp-config.php file and then select download from the menu. Your FTP client will now download the wp-config.php file to your computer.

This is how your login credentials for your database will look:

// ** MySQL settings - You can get this info from your web host ** //

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

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

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

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

Recheck the following information:

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

Make sure all the values are correct otherwise WordPress will not be able to connect to the database.

You can also check your database in PHPMyAdmin. It is a tool used for accessing and editing MySQL databases. Simply log into PHPMyAdmin, and click on the name that matches your wp-config file from the left-hand column.

Find the table named “wp_options” and click on Browse.

Error Establishing a Database Connection

Check here if all the information is correct or not. If you find any mistakes, go ahead and correct them.

Error Establishing a Database Connection

2. Database username and password

You will also have to verify database username and password. You can do this by login into cPanel Dashboard.

2.1 Checking Database Credentials In cPanel

  • To check your Database username and password you will have to create a .php file and store it to your WordPress root folder.
  • It might take a few seconds before the new file appears in your root folder.
  • Once it does, right-click it and choose Code Edit > then Code Editor. You will be lead to a blank page, add this code there:
$test Connection = mysql_connect('localhost', 'root', 'password');
if (!$testConnection) {
die('Error: ' . mysql_error());
echo 'Database connection working!';

Replace ‘localhost’ ‘root’ and ‘password’ with the database host, username and password. Save changes, and navigate to yoursite.com/check.php.

2.3 Create New Database User

For creating new Database User and Password you will have to go back to your cPanel. Go to phpMyAdmin > MySQL Databases. 

Error Establishing a Database Connection

Inside MySQL Databases, scroll down to where you have MySQL Users. Locate Add New User. Enter a new username and password, and click Create User.

Error Establishing a Database Connection

Remember these details for your wp-config.php file.

Next, you will have to assign the new user to your WordPress Database. In the same MySQL Database, scroll down and locate Add User to Database.

Error Establishing a Database Connection

Select the user you just created from the drop-down list. On the next screen, you can also select the privileges you want to assign. Once you complete that, simply go back to wp-config.php and update the file with the new information.

Note: Make sure there are no spaces between the quotes and also use single quotes like – ‘your-username’

Now, access your website. If the issue was with the username, you will be connected successfully.

If you are still getting an error, the problem could be a corrupt database.

3. Corrupt Database 

If the error is a result of a corrupted Database, it could be resolved easily. But you need to be careful while doing so, a little mistake can wipe away your entire site. 

WordPress actually comes with a database repair mode which you can initiate by simply adding the following code to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Enter https://yourdomain.com/wp-admin/maint/repair.php in your web browser. You will see the options to repair your database.

Error Establishing a Database Connection

  • Click on “Repair Database”. Make sure to remove the code once you are done running the repair.
  • Even after all this if you still see the error, there could be another reason, corrupt WordPress files.

4. Corrupt WordPress Files

  • This can occur from transferring some files, or some hack or a problem with your host.
  • You can fix this easily but make sure to take a backup of your site before proceeding.
  • You will have to replace the core version of WordPress. First, download a new copy of WordPress from their official site.

Error Establishing a Database Connection

  • Unzip the package and delete wp-config.php and wp-contents folder. By deleting these you won’t break your existing wp-config.php file.
  • After doing this, upload all your files to WordPress root folder using FTP or the File Manager. If you follow all these steps properly, all the problematic files will be removed.
  • Now try to load your site, if you still see the error, try clearing your browser’s cache.
  • If the problem still persists, you will have to contact your web host.

5. Contact your Hosting Provider

Even after following all the above steps your website is still showing the dreaded message “Error Establishing a Database Connection”?

You will have to check with your hosting provider, there is a chance that the problem is on their end. Give them the details of the error and the troubleshooting actions you’ve taken, they will be able to determine the cause of the error.

These problems are very common with a shared hosting. Since shared host uses all the same resources on servers, there is a high chance that you are facing problems because someone else’s site is compromised.

How To Avoid This Error In Future?

There isn’t any way that will protect your site completely. But there are ways to prevent such errors on your website.

  1. Always have a backup. Install a backup plugin, it will always come in handy if something goes terribly wrong.
  2. Invest in a quality managed WordPress hosting company. Work with people who will provide you a with expert solutions and a secure platform.

We hope this guide helped you to resolve error establishing a database connection. If you still need help, you can always reach out to us!

Latest Magento Tips, Guides, & News

Stay updated with new stuff in the Magento ecosystem including exclusive deals, how-to articles, new plugins, and more. 100% Magento Goodness, a promise!

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top

Talk to a sales representative

USA / Worldwide




We can help you. Right now.

Fast growing merchants choose Breeze for high-performance hosting. Experience counts. Let's get started.

Request demo

Please fill in the details below and we’ll reach out to you with a customized demo of our product!


75% OFF on

Receive the coupon as soon as you
submit the email address. 🚀