Although WordPress is a quite stable and mature software but still it’s not error-free like all other softwares. In WordPress database connection errors are the most notorious to fix specially the one Error establishing a database connection.

Sometimes we can’t even say it a “WordPress database error” because in 90% cases the connection error comes out because of mis-configuring WordPress with wrong values.

Feeding wrong values to database results in such errors.

Saying that let’s see what is this error establishing a database connection in WordPress? This message comes whenever WordPress lost connection to MySQL server.

To dig a little deeper, a “complete” WordPress is a combination of files and a database.

WordPress makes pages and posts in run-time. It means whenever a user requests any page or a post of your blog by writing a URL in browser, WordPress instantly connects to database, collects data from different tables, makes a page and presents it to user. see what is database in WordPress for more information.

Now keeping the above scenario in mind, the WordPress database connection problem arises when WordPress tries to connect to database and unable to connect because of certain reasons which we are going to discuss in this guide.

Possible Reasons

  • You recently change/update your database username and password.
  • Database server is down and not responding at the moment.
  • Database got corrupted.
  • Hacking attacks.

Backup First

Database is the most important part of your blog. In case of disaster, WordPress files can be resumed but database once gone will be gone forever.

Before making any changes to database directly, taking a database backup is advisable. see our article how to backup WordPress

Database User Details (wp-config.php)

wp-config.php is the file which is responsible for connecting WordPress with database. Consider it a bridge between the two. On your hosting server, it is located in the root directory normally public_html where you installed WordPress.

wordpress wp-config file

The file holds all the database information like database name, database username, password and host. All this information should be correct otherwise WordPress will not be able to connect to database.

We need to confirm the database user is the correct one we are feeding to our wp-config file. We are using Hostgator throughout this guide. You can take similar steps if you are using another web hosting provider.

  • Login to CPanel and Manage Databases.
cpanel manage databases
  • You will see your database name and the assigned username appears here.
cpanel database list
  • Make a note of database name, username and user password. Otherwise you can create a new user and assign it to your database.

Make sure the user should have all privileges.

cpanel database user

see our article how to create a new database user here

  • From your CPanel, go to File Manager.
  • Click www or public_html folder or where you installed WordPress.
  • Open the wp-config.php file and check for following values:

 

define('DB_NAME', 'database-name');

define('DB_USER', 'database-username');

define('DB_PASSWORD', 'database-password');

define('DB_HOST', 'localhost');

Replace the values exactly what you have noted before from database settings. If you created new database user, add it here.

DB_HOST is localhost which is true for most of the well-known hosting providers like Hostgator, BlueHost and GoDaddy so leave it as it is. Check with your hosting provider otherwise.

Also try replacing localhost with 127.0.0.1. It worked for some people.

Woopoo's Tip

Database siteurl and home Settings

If the steps above didn’t solve your problem, don’t worry we still have more cards to play.

Another important step is to check the two fields in your database: siteurl and home columns. You will find these fields in wp_options table, see more details what is a database in WordPress.

  • From your CPanel > Manage Databases, go to phpMyAdmin.
phpmyadmin cpanel

Take extra care while working in phpMyAdmin since you are interacting with your database directly.

Woopoo's Caution

  • Click your database name on the left side and then wp_options table. Look for the two columns siteurl and home.
wordpress database wp_options

The values should be your website URL like http://www.yourwebsite.com.

  • If the values are other than your website name, click Edit on the far left and change the value feeding your website URL.

Most of the time this type of fix will be needed after moving your blog or change domain name.

Check your website again in browser. Problem solved? No? let’s move forward.

Testing Database Connection

This is not a solution but more of a debug process. If both of the above steps are good on your side then there are major signs that MySQL database server is down. We need to fire a test to check it out.

  • Make a text file and copy paste the following code:
<?php
$dblink = mysql_connect('localhost', 'root', 'password');
if (!$dblink) {
die('Could not connect: ' . mysql_error());
}
echo 'Database connected';
mysql_close($dblink);
?>
  • On the second line, change root and password of your own database username and password.
  • Save the file as dbtest.php and upload it to your root directory where you installed WordPress.
  • Access the file by typing yourwebsite.com/dbtest.php

If the database connected successfully, it means there is something else what is causing problem.

WordPress Repair Database

If none of the above techniques worked then it means your database many need a repair. This is also true when you get a message like one or more database tables are unavailable. The database may need to be repaired while trying accessing wp-admin page.

Lets try to repair this WordPress database error.

  • Add the following code to wp-config.php file just before /* That’s all, stop editing! Happy blogging. */
define( 'WP_ALLOW_REPAIR', true );
  • Next, visit the page http://www.yourwebsite.com/wp-admin/maint/repair.php
database repair
  • Click Repair Database. You can choose the other option Repair and Optimize Database which will take a bit more time.
  • Try and see if it fixes the error by visiting your website URL from browser.

 

Very Important! Once done, remove the code from wp-config.php file you added earlier.

Woopoo's Caution

r

Hacking Attacks

It could be another reason if you get the database error without any reason. Servers go un-responded in case of too many hacking attempts to your website (ddos attacks).

  • Always use a security plugin like Wordfence to save your blog from possible attacks.
  • Protect your wp-admin page by adding another password layer, see our article how to protect wp-admin from hackers.

You also need to consult your web hosting provider for additional help to solve this problem. In many cases you many need a fresh WordPress install if you are not getting any clues to solve this database error in WordPress.

Please share your experience with us, what type of database connection errors you get and how you solved them? We will add them in our next round.

If you find this article helpful, don’t forget to share and help us spread the word.

Gimme a Share pleeeaase!

Pin It on Pinterest

Share This