How To Fix "Establishing A Database Connection" Error in WordPress

How To Fix "Establishing A Database Connection" Error in WordPress

This error is the most common one on the internet. We our-self have faced it a couple of this. The most annoying thing about this error is that it is occurs even though you’ve not changed anything. But no worries. We have come up with a solution in this article.

Cause of this error

The cause of this error is the failure of WordPress in establishing a database connection. The reasons for this could be that your database has been corrupted, your server is unresponsive, your database login details are incorrect and many more.

Does /wp-admin/ also face the problem?

You need to check if you are getting the same error message on both sides, the front-end, and the back-end. The message will read, “Error establishing a database connection”. But if you get a different error on the back-end you need to repair your database.

To repair database add the following code to your wp-config.php file:

define('WP_ALLOW_REPAIR', true);

Now visit http://www.yoursiteurl.com/wp-admin/maint/repair.phpand you will see the setting page.

remove this from your wp-config.php after repairing and optimizing your database.

In case this does not help, try the following method.

Checking the WP-Config file

The file named WP-Config.php is where you specify the details for WordPress to connect your database. Make sure if everything’s the same in your wp-config.php.

define('DB_NAME', 'your-database-name');
define('DB_USER', 'your-database-username');
define('DB_PASSWORD', 'your-database-password');
define('DB_HOST', 'localhost:3006');

The value of your DB_Host might be different depending on your host.

Check web host (MySQL Server)

If your site gets swarmed with a lot of traffic then your site will face this error and you are on a shared hosting. So when your host can’t handle the load it will slow down the site and sometimes even show the error. Now contact your hosting provider.

If other sites on the same server and if they are having the same issue. If this is the case, then something is wrong with your MySQL server. Go to cPanel in case you don’t have any other site on the same hosting account. Then try to access phpMyAdmin and connect the database. Check if your database user has sufficient permission. Create a new file and name it testdb.php and paste this code:

<?php
$link = mysql_connect('localhost', 'root', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

Replace username and password.

If the connection is successful, then your user has sufficient permission. Now you need to go back and re-scan for typos.

If you are using your own VPS then open ssh  and try

$: service mysql status

And check the status of your MySQL  if it’s showing stop then use the following command

$: service mysql restart

And check your MySQL status,  if your MySQL fails to restart then check MySQL error log.

Mainly myself stop due to fewer resources and try adding swap space.

 

Check out the top 3 WordPress hosting services:

A2 Hosting
$2.99 /mo
Starting price
Visit A2 Hosting
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.8
  • Pricing
    4.0
HostArmada
$2.49 /mo
Starting price
Visit HostArmada
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.0
Hostinger
$2.99 /mo
Starting price
Visit Hostinger
Rating based on expert review
  • User Friendly
    4.7
  • Support
    4.7
  • Features
    4.8
  • Reliability
    4.8
  • Pricing
    4.7

How to Install PhpMyAdmin on a Ubuntu 18.04 VPS or Dedicated Server

This article will describe the process of installing phpMyAdmin on an Ubuntu 18.
3 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Install phpMyAdmin on a CentOS 7 VPS or Dedicated Server

Learn how to setup phpMyAdmin on your CentOs 7 VPS server and manage your MySQL/
2 min read
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Backup Your MySQL Database on an Ubuntu 18.04 VPS or Dedicated Server

This article shows you how to back up a MySQL database on a Linux Ubuntu 18.04 s
3 min read
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert

How to Disable MySQL 5 "Strict Mode" on an Ubuntu 18.04 VPS or Dedicated Server

How to Disable MySQL Strict Mode on MySQL 5.6 Running on an Ubuntu 18.04 VPS.
2 min read
Idan Cohen
Idan Cohen
Marketing Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top