How to Fix “Error Establishing Database Connection.” in WordPress

Whether you are WordPress beginner or expert with a WordPress site, chances are you have encountered the “error establishing database” (EEDC) error. It may be frustrating but let’s address what causes this error and what we can do to prevent and fix it

The article covers what causes error establishing a database connection, how to troubleshoot and resolve the error.

Overview

WordPress uses two main technologies; PHP which is the primary programming language and MySQL that serves as the database where all the information on the WordPress site is stored.
PHP commands connect to MySQL database to access information requested and displays that on the screen on the site.

The error occurs for whatever reason PHP code or commands are unable to read information stored in the database.

What then causes the error?

A variety of factors can cause the errors such as;

 

  • Wrong database credentials –Your database uses different credentials from what you use to log in. Wrong credentials may lead to a database connection failure.
  • Corrupt database –Your database can be corrupt for some reasons; a wrong plugin or script or even a hacker sending SQL injections to your database. A server failure due to power fluctuation may cause your database to be corrupt and thus not accessible.
  • The database server is down – The server hosting your database may be inaccessible and thus no connection to the database.

How to Fix “Error Establishing Database Connection.” in WordPress

You need an FTP client to access your website from the back end. Check to ensure that you are getting the same error on the front end side as well as the backend side of the site. Remember to start with the first solution and follow the next step until the issue is resolved.

Troubleshooting

Log in to yoursite.com/wp-admin (the backend) and make sure you substitute yoursite.com with your sites WordPress domain name, if you get EEDC, your database may need to be repaired.

Check with other customers to find out if they are experiencing the same issues if using shared hosting. You may be able to narrow down to the issue, or the issue is local to your WordPress site.

Do you have other sites hosted on the same server or with the same hosting provider? Are they having issues? If yes, it may indicate that you need to contact your hosting provider to resolve the issue.

Call your Hosting Provider

Your hosting plan includes a support contract and contact that you can use to help identify and isolate the issue with your database. Sometimes the hosting company may be aware of the issue and may explain why you experience the issue and even how to resolve. The issue could be due to too much traffic to your website, and your hosting server is unable to serve the requests in a timely fashion and thus ends up not accessible.

Depending on the hosting provider advice and or feedback, you may go ahead and resolve the issue and investigate further if the issue does not lie with the provider

Confirm Database Credentials

Access your wp-config.php  file and confirm that the credentials defined are the correct ones as given by the hosting company or as per your records. Look out for this details;

define('DB_NAME', 'some_db'); /* this is the name of your WordPress database */define('DB_USER', 'some_username');
define('DB_PASSWORD', 'some_pass');
define('DB_HOST', 'localhost'); /* this is the web address of your database server */Update the credentials if not correct then upload the file using FTP.

Check Database Host Server

The next solution involves checking your host or server. Maybe your server cannot handle lots of traffic. If you are on shared hosting, this error is more likely to occur. You might have to call your hosting providers and ask them if your MySQL server is responding. If you need to check it out, you might check other sites hosted on the same server; if you get the same error, there is something wrong with your MySQL server. Also, you might opt to try to access phpMyAdmin and connect to the database. If you get an unsuccessful connection, make sure that you have sufficient permissions. To confirm, create a new file called connectiontest.php and paste the following;

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

Under the root and password replace with your username and password respectively. Then save and upload. If it connects successfully, you have sufficient permissions.

Repair WordPress Database

To edit your wp-config file, you need to download it through the FTP client. Once you log in to your FTP client (I’m using WinSCP), you can download the file. Just search for the wp-config file located almost at the bottom of the list.

Right-click the wp-config file and hit the download button. After download, use any text editor to edit the modified file. Use visual studio code to edit the file (keep it open until we confirm the WordPress is working).

Add the below code at the bottom of your file;

define('WP_ALLOW_REPAIR', true);

The above code allows WordPress to try and repair the database. After the modification is complete, upload the file to the server.

Confirm the overwrite, Once you add that and it has finished uploading successfully, go to

http://www.yoursite.com/wp-admin/maint/repair.php

Remember to replace the yoursite.com with your WordPress domain name. The link takes you to a window that looks like the following; an attempt to repair the database.

Click repair and optimize the database for the appropriate maintenance. The database repair page is not secure as anyone can access the URL without using login credentials. Therefore, once the repair is complete, go back to the file and edit the line we added to wp-config.

Disable access to the repair page and prevent unauthorized access to your database.

If you still see the error, go back to the wp-config file and make sure the database section is okay.

If everything is okay, upload the file and try to reload your website.
If you still see the error, try to change the DB_host from localhost to the actual IP address.

define('DB_HOST', '127.0.0.1:8889');

Restore WordPress default files from a backup

A plugin or a hacking attempt may modify the WordPress files or the database, and this may require a restore from the default WordPress files. You can get this files by downloading them from WordPress.org.Unpack the files and upload them to the WordPress root folder on your site using the FTP tool that you use. Use this as a last resort.

Conclusion

With the above steps, you can identify and resolve any database connection error

(s). WordPress has tried to maintain itself, so this kind of error is less likely to affect you as long as you keep your database ‘healthy’.

 

Check out these top 3 WordPress hosting services:

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
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
Hostens
$0.90 /mo
Starting price
Visit Hostens
Rating based on expert review
  • User Friendly
    4.5
  • Support
    3.8
  • Features
    4.3
  • Reliability
    4.5
  • Pricing
    4.8

How to Install MySQL on a Windows Web Server Running Apache

This tutorial will show you how to install the MySQL database on a Windows serve
3 min read
Michael Levanduski
Michael Levanduski
Expert Hosting Writer & Tester

How to Solve a “Warning: Error While Sending QUERY Packet” Error

This is one of the rarest errors people come across. The article will cover all
3 min read
Arvind Singh
Arvind Singh
Hosting Expert

How to Solve a “Warning: Error While Sending QUERY Packet” Error

This is one of the rarest errors people come across. The article will cover all
4 min read
Arvind Singh
Arvind Singh
Hosting Expert

How To Install and Configure MySQL for PHP Applications on Windows IIS 7

When hosting PHP apps on IIS 7 it’s highly recommended you use the Miscrosoft
3 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.