Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

Faster WordPress Hosting: An Experiment for the Best Load Time (79ms). SEE HOW

How to Fix Error Establishing a Database Connection in WordPress

Updated on March 13, 2019

6 Min Read
wordpress error establishing a database connection
Reading Time: 6 minutes

One fine morning, you wake up, brew some coffee, and sit at your workstation. You fine-tune your laptop, open up your browser, and enter your own website’s URL. Shockingly, you encounter a blank white screen page! No matter how many times you refresh, you’re unable to access the website. You’re only furnished with the following curt message: WordPress Error Establishing a Database Connection.

For each minute that your website is down, unfortunately, you’re losing the chance to generate leads, have conversions, and enjoy sales. The gravity of the situation is serious, but calm your nerves down and don’t panic! Keep reading this guide to better understand and learn some practical approach on how to fix the error establishing a database connection in WordPress.

  • Why WordPress Database Error Occurs?
  • Determine Error Replication
  • Check Database Login Credentials in wp-config
  • Reset Username and Password
  • Database Host Value

Why WordPress Database Error Occurs?

First, let’s examine what this error message actually means.

Your database uses a language called MySQL. Your website is reliant on a server-side language called PHP. Whenever your website URL is entered into the browser, WordPress uses PHP to retrieve all the information pertaining to the page from the database and sends it to the visitor’s browser.

MySQL-WebServer

The error establishing a database connection you tend to experience is because this MySQL-PHP connection is severed for some reason, which we must investigate.

Any of the following scenarios may cause this error:

  • Your login credentials (your username and password) may have been changed. Even if you didn’t make the change yourself, this could have happened somehow.
  • If you install a poorly coded plugin, your database may corrupt.
  • Your website may be experiencing a heavy traffic spike, which is beyond your server’s capacity.
  • Your web hosting company may be at fault and the database is not functioning due to some problem at their end.

Before any attempt to fix the issue, I advise you to take a backup! You have the following options to make a backup:

  • Download all web files from FTP (SFTP). To do this, you will have to log into your hosting account, such as Cloudways Platform.
  • Export MySQL database through the Export function.
  • Alternatively, you can use UpdraftPlus to backup your data.

If you are a Cloudways user and using our WordPress hosting platform, we make things easy and allow you to take manual as well as automated backups via the cloud console. The following image shows how Cloudways allows easy and periodical backups.

Cloud Backup Frequency

Now, let’s narrow down your problem.

Determine Error Replication

Are you encountering the same error when you try to enter the website URL (front-end), and when you attempt to login to /wp-admin/ (back-end)? If the error is the same, i.e., error establishing a database connection, move onto the next step.

However, if you encounter the following error when trying to login to /wp-admin/, your database has been corrupted:

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

In your root WordPress file directory, you must locate your wp-config.php file. You must do this to enable WordPress’ built-in repair feature.

Launch Filezilla —–> (WordPress install folder) —–> public_html

Right-click the wp-config.php file and select a code editor, such as Sublime Text Editor to open the file.

Edit wp-config

After you’ve opened the wp-config.php file, add the following line at the bottom of the file:

define('WP_ALLOW_REPAIR', true);

Now navigate to:

www.yourwebsite.com/wp-admin/maint/repair.php

(Replace “yourwebsite.com” with your actual URL)

Repair WordPress Database

You will see the above-mentioned options. We recommend you to choose the “Repair and Optimize Database” option. Although it takes more time, it is a more comprehensive attempt to fix the issue. This repair page can be accessed without the need to log in, so it creates a security issue. Immediately after the repair and optimization have finished, remove the line of code you added to the wp-config.php file.

Did this method solve your problem? If so, congratulations! If not, carry on reading this guide to find another fix regarding the issue.

Check Database Login Credentials in wp-config

If the procedure outlined in the preceding paragraphs did not work for you, then the possible cause of your problem is your database settings. As mentioned earlier, your database login settings may go haywire if you change other details of your user information or database. This happens if the wp-config.php file is not updated properly by the system.

wp-config.php is the most essential file of WordPress. It contains all the database information.

To open this file go to:

Launch Filezilla —–>(WordPress install folder) —–> public_html

Right-click the wp-config.php file and select a code editor such as Sublime to open the file.

Once you’ve opened the wp-config.php file, you will see four essential variables defined in the code, which is required to get connected to the database. These are database name, database username, database password, and database host that is localhost in most of the cases (there may also be an IP address in place of localhost).

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

Note down these values. Now, login to your Cloudways platform, and select the “Applications” tab. Get into your WordPress application, from the “Access Details” tab, click “Launch Database Manager”. Click the table named wp_options. It should reveal your website name, URL and other settings. This reveals whether you have the correct information in your wp-config.php file or not.

Edit wp-options WordPress Table

If the ‘siteurl’, and ‘home’ field information is different, then edit wp-config.php file and insert the same values as in the wp_options table.

Reset Username and Password

You must ensure that the username and password in your wp-config.php file match the values in the “MySQL Access” panel of your website’s application page on the Cloudways platform.

MySQL Access

If the username and password do not match, open your wp-config.php file and edit it to include the values from the “MySQL Access” panel. That’s it! If you were attempting to do this via cPanel, you’d have to put in a lot more effort.

Database Host Value

The fourth important value in WordPress wp-config.php file is DB_HOST. For most web hosting providers, this value needs to be localhost, which is the value in the file by default.

If you’ve carried out all the aforementioned steps, and you are still facing the problem, then check this official WordPress list of DB_HOST values for various web hosting providers. Otherwise, you can contact your web hosting providers and ask them to provide you the DB_HOST value you need to input into the wp-config.php file.

Managed WordPress Hosting on Optimized Cloud Servers

We hope you find this guide helpful and that you were able to solve your WordPress “error establishing a database connection” problem. However, WordPress experts recommend using managed WordPress hosting to reduce the stress by handing over the technical challenges to professionals.

Did you figure out any other way to get rid of this problem? Let us know in the comments section below and mention what worked and what didn’t work for you. It will benefit others who are facing the same problem as you.

Q. What is the database connection error?

Well, for short, this error occurs because WordPress is not able to establish a connection to a database. And the reason for this disability can vary. This could be because:

  1. Your login and credentials are false or have been changed.
  2. Your database server is not responding.
  3. Your database has been corrupted.

Most of the time this error occurs because of some server error, but there may be other factors as well.

Share your opinion in the comment section. COMMENT NOW

Mustaasam Saleem

Mustaasam is the WordPress Community Manager at Cloudways - A Managed WordPress Hosting Platform, where he actively works and loves sharing his knowledge with the WordPress Community. When he is not working, you can find him playing squash with his friends, or defending in Football, and listening to music. You can email him at mustaasam.saleem@cloudways.com

Start Growing with Cloudways Today!

We never compromise on performance, security, and support.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

THERE’S MORE TO READ.