How to Fix the Error Establishing a Database Connection in WordPress

The more powerful and feature rich CMS you are using is, the more problems you are going to encounter along the way. It’s inevitable. And WordPress is not an exception in this case.

Although it is well secured and reliable in its performance, it still sometimes causes some small and major problems. Error establishing a database connection in WordPress is one of the frustrating errors any WordPress user and website owner may from time to time experience.

Why this error establishing a database connection in WordPress is so annoying is that it does not come with a proper explanation about what had happened or what you need to do to get rid of that.

In addition, whenever your site is not accessible and is down because of this error, you are going to lose more leads and traffic, more customers and prospects, which is, of course, not what you want.

But however scary error establishing a database connection in WordPress may seem, there is nothing to dramatize, as, in the majority of cases, this error is not difficult to solve. But before we delve into the practical solutions, let’s see what this error is in essence.

What is error establishing a database connection in WordPress?

Well, you are trying to access to your WordPress hosted website but instead of a homepage, you are taken to some blank page containing a frightening message about error establishing a database connection.

Actually, WordPress is written in PHP and MySQL and all the information that makes up your site is stored in MySQL database, while PHP is used to store and retrieve this information from the database.

The PHP code is used to access MySQL database to get all the information needed to create the page. Whether it is the name of the publisher or the author of the post, date or some other information, it is retrieved by the help of PHU queries.

Now, of you see error message “Error establishing a database connection” in WordPress, it means, that this or that reason the PHP code could not connect to the database for the information your website needs for the creation of a webpage.

Why do you get error establishing a database connection in WordPress?
There are 3 main reasons causing this error:
Your database login credentials (login and password) are wrong or have been changed.
Your database is not working properly or is corrupted.
The server hosting your database is not responding or is down.

How to fix error establishing a database connection in WordPress?

Before trying to understand where the root of the evil is hidden, make sure you have the backup of your site at a safe place.

Check whether the same error occur on the backend

First of all, you need to check whether this error is present on frontend and backend of your WP website (wp-admin). The error can be the same for both.

In another case, you can get different errors, like “One or more database tables are unavailable. The database may need to be repaired. ”

It means that your site’s database is corrupted and you need to fix it by accessing wp-config.php file in the root WP file directory. You will need to add the following line of code to that wp-confg.php file before “That’s all, stop editing! Happy blogging”:

define(‘WP_ALLOW_REPAIR’, true);

Once this code is added, it will let you repair or optimize the database from Here you can choose to only find the problems and repair your database or both repair and optimize, in which case WordPress will try to boost your database performance.

As soon as you are finished with this fixing process, be sure to remove the last added code (define(‘WP_ALLOW_REPAIR’, true);) from your wp-config.php file.

This should fix the error establishing a database connection in WordPress. If it doesn’t, we’ll continue troubleshooting.

Related Post: How to fix File Permissions Error in WordPress?

Check your database credentials in Wp-Config file

Whenever you are moving your WordPress site to one hosting to another one or making certain changes in the name or user information of your database, your wp-config file needs to be updated accordingly. Otherwise, it may cause the database connection error we are trying to solve.

First of all, you need to go to your directory in cPanel where you have WordPress installed and open wp-config.php file. Here you will see your login credentials, something like this:

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

Make sure all the 4 values here are correct (the name of the database ‘DB_NAME’, the login username ‘DB_USER’, the login password ‘DB_PASSWORD’ and the database host ‘DB_HOST’). Depending on the host you use, the values may differ.

Some of the checked ways is to replace localhost with the IP:
define(‘DB_HOST’, ‘’). Depending on the web hosting, the IP may also vary.

If everything is ok with this file, there is probably some issue with the server.

Check your MySQL Server

You may have encountered this error establishing a database connection in WordPress when you receive flows of traffic or welcome lots of site visitors.

This is because your site is not able to handle such traffic and becomes too slow for some users and showcases this error to other users trying to browse your site.

What you can do in such case is to test another site on the same server to understand whether the problem has to do with server or not. If the results are the same for both sites, then the problem is with MySQL server.

Try to connect your database via phpMyAdmin in your cPanel. If you succeed with the connection, then check whether your database has sufficient permission. Simply create a new file testconnection.php and add the following code to it:


$link = mysql_connect(‘localhost’, ‘root’, ‘password’);

if (!$link) {

die(‘Could not connect: ‘ . mysql_error());


echo ‘Connected successfully’;



Do not forget to change the username and password. If connected successfully, it means that the user has sufficient permission and you need to look for something else.

If you are not able to connect to your database, it means there is something wrong with your server. Either MySQL is down or the user has not sufficient permission. In this case, you need to contact your hosting company for technical assistance.

About Sonnal S Sinha

Sonnal S SinhaSonnal S Sinha shares exciting WordPress themes, plugins and other WordPress related news for our viewers. He also posts selected WordPress developers interviews from time to time.