1. Home
  2. WordPress Tutorials
  3. How to Solve an Error Establishing a Database Connection in WordPress
Experience Faster, Secure and Eco-Friendly Web Hosting for your Website or Blog. Learn More.


How to Solve an Error Establishing a Database Connection in WordPress

Are you getting an “Error Establishing a Database Connection” when you go to your website? This is actually one of the many common WordPress errors that happen when you are running a website. Like all errors, this one can be very frustrating if you are unaware of why this is happening and if you don’t know how to fix the error to get your website running again.

This error message can mean one of two things. The database may be corrupted and needs to be repaired or your WordPress credentials like username and password have been inputted incorrectly. Keep in mind that if you have a recent backup of your website you can restore your website to that earlier version. Today, I will demonstrate how to fix the error establishing a database connection in WordPress.

Why Does the Establishing a Database Connection Error in WordPress Occur

The first two reasons are very easy to understand. You could have inputted your login credentials incorrectly. It happens to everyone and is quite a simple fix. Of course, if you have all of your login information memorized in your webs browser this is unlikely. It is possible that you changed your login credentials. It is good practice to change your password on a regular basis. You may have forgotten to update your web browser with the new credentials and this can cause the issue. If neither of these is likely and you are absolutely sure it is not related to logging in incorrectly, then the process gets a bit more complicated.

The other reason this error can occur is because there is something wrong with the database. The most common issue is if your database is corrupted. This is quite easy to tell by checking if the messages on the front and back ends are the same. If they are not the same, then most likely your database is corrupted. If it is, then repairing it or using an old backup will fix it. The other issue that can occur is that the database server is unresponsive. Thankfully, fixing the error establishing a database connection issue in WordPress is not terribly difficult.

How to Solve an Error Establishing a Database Connection in WordPress

Today, I will demonstrate how to fix the error establishing a database connection in WordPress. Keep in mind that since there are many different causes there are also multiple solutions and it is possible that none of these will work, but that is very unlikely. Remember, the easiest way to fix these errors is to revert to an old backup and that will most likely fix the error without any hassle.

You will need access to the cPanel of your website. The login information is given to you by your web host when you are creating an account with them. Now, the first solution we will go over is when you are getting different error messages on the front and back ends of your website. This signifies a database corruption and you will need to repair the database, which is quite easy. Login to the cPanel and click on the File Manager option. If you are getting the same error messages feel free to skip the next few steps.

You now need to locate the wp-config.php file. Click on the public_html directory. Right-click on the wp-config.php file and select the “Edit” option.

Right-click on the wp-config.php file and select the "Edit" option.

A new tab will open containing all of the file’s code. Scroll down to the bottom of the page and paste the following code above the “/* That’s all, stop editing! Happy blogging. */” line:

define(‘WP_ALLOW_REPAIR’, true);

Paste code here.

Now you can begin the repair process by visiting:

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

Replace myURL with your websites URL. On this page, you will have two buttons to press. The “Repair Database” button will repair the common problems found in databases. The “Repair and Optimize Database” button will do the same and also optimize your databases to be more efficient. I would recommend clicking on the “Repair and Optimize Database” button.

click on the "Repair and Optimize Database" button. 

This will most likely fix your website’s error message. Make sure that you go back into the wp-config.php file and delete the line of code you added. If this did not fix the problem then keep reading for other solutions.

If you are getting the same error, then most likely your sign in information is wrong. This can be confirmed inside of the wp-config.php file. You should see the following lines in the code:

/** MySQL database username */
define(‘DB_USER’, ‘Username’);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘Password’);

/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

If you are sure the Username and Password match, then that is not the problem.

The next most common solution is contacting your web host. It is very common that websites slow down when large amounts of traffic happen. Unfortunately, the slowdown can be so severe that this error message can occur. The web host should be aware and if they are not, you will make them aware. The good news is there is a way to check this by yourself.

On the cPanel click on the “phpMyAdmin” option.

click on the "phpMyAdmin" option.

If you can connect to this page then we need to once again make sure that your user has the correct privileges. We can do this by creating a test file. Create a new file and copy and paste the following code inside of it:

$link = mysql_connect(‘localhost’, ‘root’, ‘password’);
if (!$link) {
die(‘Could not connect: ‘ . mysql_error());
}
echo ‘Connected successfully’;
mysql_close($link);

If this test runs successfully, then it is definitely a problem on the server end and you should contact your web host.

These are the most common solutions and hopefully they worked, but unfortunately, they may not. If this is the case, contacting your web host is one of the few options you have left. They should have experience with this error and will know how to approach the situation. If you were hoping to find a fix that did not involve using a backup and still have one, then I would recommend using the backup and see if that fixes the issue after contacting the web host.

Errors Are Not Fun

Errors are not the highlight of any web developer’s day because not only do they need to rush to fix this mistake, but it will most likely cause the website to lose traffic while the issue is occurring. The downtime can cost many first-time visitors. Backups are a really good way of avoiding extended downtime and there are many great plugins that make it easy to backup your website.

The worst thing you can do is hope that the error message will fix itself and not do anything. Most error messages will not disappear on their own and take some manual intervention to fix.

Which method has worked to get your website back online? Was your web host helpful when attempting to fix this issue?

Author: Chris Racicot

Chris is the Support Manager at GreenGeeks and has been with the company since 2010. He has a passion for gaming, scripting and WordPress. When he’s not enjoying his sleep, he’s working on his guitar skills and fiddling with 3d printing.

Updated on November 30, 2017

Was this article helpful?

Related Articles

Add A Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.