/  

Blog

Don’t leave yourself behind. Stay up to date with the latest design trends, our new products,
updates, WordPress news, and special programs right on the blog!

How to Fix the Error Establishing Database Connection in WordPress

One day, your website welcomes you with this message: Error establishing a database connection.

You get muddled and a bit scared. You understand that the longer the website is down, the greater number of customers you will lose.

But you don’t know what has happened to your website and how to fix the error.

If you want to find out an appropriate solution, just take your lumps and sift through the post with us.

What is Database Connection Error?

WordPress is built mainly with PHP and MySQL. WordPress core files are written using PHP while the content of website is stored in a MySQL database. The information like posts, pages, layouts and color settings is kept in this database. When a visitor reaches a page on your site, PHP code will be used to connect to MySQL database and get necessary information to display the page.

Sometimes, WordPress cannot use PHP commands to connect to MySQL Database. As a result, the information cannot be retrieved and displayed; the page is left blank with these words only: “Error Establishing a Database Connection”.

Why Does the Error Happen?

Knowing what causes the error, then we will know how to fix it. Below are some common reasons for the error establishing a database connection in WordPress.

  • The login credentials for your database are incorrect. Your WordPress site needs a username and password to access the database. If they have been changed for any reason, your website will not be able to get the database with the old credentials.
  • The database is corrupted. Your database might be corrupted due to some reasons like theme failing, hackers, or a plugin upload.
  • The WordPress files are corrupted. There are many reasons for corrupted WordPress core files like theme and plugin updates.
  • The database server is down. Your database server might be down because of some errors on the hosting provider’s end or getting huge amount of traffic concurrently, leading to the error establishing a database connection.

How to Fix the Error?

  • Check whether your database is corrupted

After the error message comes out on the front-end, try reaching the back-end by going to yourdomain/wp-admin.

If the error appears the same as the front-end, you can move to step 2 below.

If other kinds of messages show up, particularly “One or more database tables are unavailable. The database may need to be repaired”, no doubt your database has got into some trouble and needs repairing.

But there’s nothing to worry about. WordPress comes with an automatic repair mode. However, you have to enable it.  

The process to initiate the repair mode requires no more steps as below.

  • Log into your cPanel
  • Choose File Manager
  • Navigate wp-config.php file ( the file contains your WordPress installation settings and configuration)
  • Open wp-config.php file and add the following code at the bottom: define( ‘WP_ALLOW_REPAIR’, true );

Subsequently,  go to this location on your WordPress website: www.yourdomain.com/wp-admin/maint/repair.php

Now there comes 2 options for you: repair or repair and optimize your database.  One small notice: the latter will take a little longer.

Once you finish the repair of your database, make sure the line of code that has been added to the wp-config.php file is removed. Since the repair page can be accessed by anyone without having to log in, the removal of the code will return the wp-config.php file to its previous state. Otherwise, your database would be put at risk.

Now you can go to your site to check whether the error is still there.

If it disappears, cheer yourself up!

If the error remains before your eyes, don’t hesitate to move to the next step.

  • Check your Database Login Credentials

Contrary to your expectation, the first step does not produce any positive results. The error establishing a database connection keeps occurring. If so, it is time to take a look at login settings in wp-config.php.

Login credentials are used to connect to your database and normally stored in wp-config.php. In some cases, especially when you switch hosting providers or make any change for user or password, the file hasn’t been updated with the new information, the error just happens.

The wp-config.php file holds 4 pieces of important information to successfully connect to your database. Spend some time checking all of them to make sure the credentials are correct.

Reopen the wp-config.php file and look for these lines of code.

  • Database name

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

/** The name of the database for WordPress */

define( ‘DB_NAME’, ‘database_name_here);

  • Login username

/** MySQL database username */

define( ‘DB_USER’, ‘username_here’ );

  • Login password

/** MySQL database password */

define( ‘DB_PASSWORD’, ‘password_here’ );

  • Database host

/** MySQL hostname */

define( ‘DB_HOST’, ‘localhost’ );

Check database name

Firstly, make sure you have the right database name. Follow these steps.

  • Log in phpMyAdmin in cPanel dashboard.
  • On the left-hand side is a list of databases on your server. Continue clicking on the database with the name as the value defined in the code: “database_name_here” above.
  • The table names of the chosen database will occupy the main screen. You can check whether this is the correct database by clicking on the wp-options table, and choose Browse option next to it. Here you should see your website’s name, URL, and other general settings.

Now it’s clear whether or not your database name in your wp-config file is correct. If it’s not correct, fix it in wp-config.

Check login username & password

The database name is correct, but the error establishing a database connection remains there. Come on, we should go checking the login username and password.

There are usually 2 simple steps to do this. Actually, you can skip the first step and move to the second one immediately.

  • Create a new simple .php file to test your current login credentials

Create a .php file in the root directory of your WordPress site with any name. Then paste the following code:

<?php
$test Connection = mysql_connect('localhost', 'root', 'password');

if (!$testConnection) {

die('Error: ' . mysql_error());

}

echo 'Database connection Working!';

mysql_close($testConnection);

?>

Browse to the file on your WordPress site: https://yourdomain.com/yourfilename.php

If there is a message saying that you have been successfully connected, congratulation! There’s nothing wrong with your current credentials.

Otherwise, a MySQL Error: Access denied message appears. What you need to do next is to reset your login credentials.

Reminder: Be sure to remove the file when you’re done testing.

  • Create a new database user and password

You can create a new database user and password with MySQL Database in cPanel.

In cPanel, click on MySQL Databases under the Databases section.

Scroll down to MySQL Users: Add New User. Choose a new username and password and click Create User.

Continue scrolling down to Add User To Database, choose your newly created username and your WordPress database, click Add.

Finally, remember to update the wp-config.php file with your new username and password.

Check your database host

To ensure the information in the wp-config.php file is correct, don’t skip the step to check your database hostname in DB_Host. The values often come as “localhost”. However, some hosts might use different values. To be certain, contact your hosting company to see what value should be used.

  • Check if theme/ plugin files have been corrupted

There is also likelihood that the corrupted theme/ plugin files cause the error establishing a database connection. The files can be corrupted for some reasons, such as plugin update or file editing.

  • To start checking, first access your root  WordPress directory (via File Manager or FTP Client).
  • Navigate to wp-content and rename the plugins folder to others of your choice.
  • Then come back to your site to see if the error goes away.

If it does, the error should have been caused by some plugins. It will take some more steps to identify which plugin is a real troublemaker.

  • Change the plugins folder back to its original name.
  • Click on the plugins folder to access its inside files.
  • Change the folder name of each plugin, then go to check when the error vanishes.

If you catch the problematic plugin, you can choose to either delete it or go online to seek solutions.

If not, try to check the theme files with the same process.

Even trying to rename both plugins and theme files does not do any help; other steps should be taken into account to handle the issue.

  • Restore the Default WordPress Files

Reminder: You should take a backup of your site before proceeding with this step.

Getting exhausted because of such a long journey you’ve gone through and seeing no results, here is the last resort: Restore the default WordPress files. This method will help replace the corrupted files on your site with the original WordPress core files.

The process to restore the default WordPress files is as simple as follows.

  • Download the newest copy of WordPress from WordPress.org.
  • Unzip the downloaded package.
  • Open the folder and delete the wp-content file.
  • Access your WordPress directory and upload the remaining files of the downloaded folder.

When uploading the new WordPress files, all the previous files will be overwritten. Now you can come back to your site to see the result. If nothing changes, you have no other choice than coming to the final part below.

  • Check your Database Server

No more frustrating. You have already followed all the recommended steps, but the error establishing a database connection has not been resolved. You should wait no longer. The error might be in connection with your database server. So contact your hosting provider to ask for help. You can provide them with all the needed information and leave them the rest.

Let’s Fix the Error!

Now you have some good solutions under your belt, hope that the error establishing a database connection on your WordPress site will no longer disturb you.


            

Leave a Reply

What people say?

lmoenat

This is a great and wonderfull theme, i was worried because the design look so professional but the manual make everything easy. Thanks Guys! you make a wonderfull job, KOPA Themes is my favorite!

armisha

Great theme and very professional support team that is very quick to help in anything concerning the template.

Subcribes Newsletter