How To Fix “Error Establishing A Database Connection” in WordPress

Posted on July 8, 2021 by in WordPress |

How To Fix “Error Establishing A Database Connection” in WordPress

It’s unsettling when you go to your website expecting to see your amazing design only to be faced with a blank screen and a message that says “error establishing database connection.” This error occurs when WordPress can’t connect to that database. Several things can cause this connection error. In this article, we’ll explore what causes the error establishing database connection message and see how to fix it.

What is a WordPress Database?

A database is a table that stores content such as your posts, pages, widgets, colors, titles, etc. This data can be accessed, organized, and retrieved by WordPress to display to your visitors.

It works great. Until it doesn’t.

If the database can’t be accessed, you’ll see a message that says “error establishing database connection” and a blank screen. It’s blank because it has no data to display anything else.

What Causes the Error Establishing Database Connection Error?

First, don’t panic. This is a common error, and it doesn’t mean that your content is lost. It is fixable.

Several things can cause this error. The most common include:

  • Incorrect information in your WordPress settings.
  • A corrupt database.
  • The database server not responding.
  • Corrupted WordPress files.
  • Too much traffic.

Let’s look at the steps to troubleshoot and fix the error establishing database connection error. Once you’ve found the problem, there no need to do the rest of the steps.

1. Check Your Database Credentials

This is the most common cause of the error establishing database connection error. Incorrect database credentials don’t change on their own, but this could be the problem if someone has been making changes to the files, hosting, etc.

WordPress stores the data in a database management system called MySQL. This database is stored on a server. To access the database on this server, WordPress needs some information:

  • Database name
  • Username
  • Password
  • Database server

These are the database credentials. All this information is stored in the wp-config.php file. If any of this information in the file is incorrect, WordPress cannot establish a connection and you’re given the connection error.

Check the wp-config File

Check the wp-config File

To check your WordPress database credentials, you’ll need access to your server’s files. In your host’s cPanel, select File Manager and then select the folder for your website.

Check the wp-config File

Your files might look different from mine. I have one main site and a lot of test sites under that one. Because of this, I have a public_html folder that has the files for my main site and individual folders for all the other sites on the server. Open the folder for the site you want to check.

Check the wp-config File

Look for the file named wp-config.php and right-click to open the options. Download a backup before making changes, and then select Edit.

Check the wp-config File

Scroll down to find a section labeled MySQL settings. Look for the information with the database name, username, database password, and hostname. Next, you’ll need to verify that this information is correct.

Confirming the Database Credentials

Confirming the Database Credentials

In your host’s cPanel, select MySQL Databases.

Confirming the Database Credentials

Next, look at your current databases to confirm your database name and username.

Confirming the Database Credentials

Then, scroll to the section with current users. Here, you can see the usernames and change the password.

Make Sure They Match

If the database name, username, and password doesn’t match, make the proper changes and try reloading the website.

2. Check Your WordPress Dashboard

Try to open your WordPress dashboard by adding /wp-admin to the end of your URL like this:

examplewebsite.com/wp-admin

If you see the error “establishing database connection” message, move on to the next step. If you see a different message, follow the information it provides. This could lead to the solution.

If you can enter the dashboard, then try to deactivate plugins, themes, or code until the problem is solved.

3. Repair Your Database

Try to log into your website as an admin and see a message that says something like “One or more database tables are unavailable. The database may need to be repaired”.

In this case, you can repair the database by adding a line of code to your wp_config.php file.

Repair Your Database

Open the file and scroll to the section that says ‘That’s all, stop editing! Happy blogging’. Just above this line, add this code:

define('WP_ALLOW_REPAIR', true);

Repair Your Database

Next, go to the repair page at your website: examplewebsite.com/wp-admin/maint/repair.php

This gives you two options:

  1. Repair Database – WordPress will look for the common problems and repair them.
  2. Repair and Optimize Database – WordPress will repair and look for things it can optimize to improve the performance.

Both can take a while to complete. Anyone can access this page. Once you’ve completed the repair, remove the code from your config file.

4. Check if Your Server is Down

We don’t expect our servers to go down, but it’s difficult for any server to have 100% uptime. This is more likely to happen if you’re on a single server. If you’re on a cloud-based plan, then your site will simply pull from another server if one goes down.

If you’re on shared hosting, your server could go down due to heavy traffic. When this happens, your site can give this error if the server is slow enough.

First, if you have multiple websites on the server, make sure they’re working.

Check if Your Server is Down

Second, if you have a VPS, dedicated, or local server, go to your hosting account and look for a notification about your server’s current status. If your server is not running, you might need to reboot it.

5. Reboot Your Server

Reboot Your Server

Even if your server is running, rebooting it might help. In your hosting account, look for an icon about restarting your server and click it. This will only be available for VPS, local, and dedicated servers. Shared hosting won’t have this option.

6. Update WordPress or Remove Corrupted Plugins and Themes

WordPress files may be corrupt due to an out-of-date plugin or theme, or even the WordPress core. It could have failed during an update or when connecting the website to an external service.

To solve this, you can remove the files via FTP or by accessing the files in the host’s admin panel. The file structure is the same.

Update WordPress or Remove Corrupted Plugins and Themes

To access the files in cPanel, go to the Files section and select File Manager.

Update WordPress or Remove Corrupted Plugins and Themes

If your website is the main site on your server, look for public_html and click it. If it’s another site on your server, look for the folder with that site’s name and click it.

Update WordPress or Remove Corrupted Plugins and Themes

Look for a folder called Content and click it.

Update WordPress or Remove Corrupted Plugins and Themes

Now, look for the folders for your themes and plugins. Rename the folder you suspect has the culprit. If you’re not sure, think about what was updating or needed updating. Consider the quality of your themes and plugins. I find that plugins are usually the problem if something is corrupting my files.

Update WordPress or Remove Corrupted Plugins and Themes

If this solves the problem, then rename to folder back to its original name and enter the folder. Next, rename one of the plugins or themes and test the site until the problem goes away. If it does go away, then delete the plugin or theme that’s causing the error establishing database connection error.

If you’ve renamed the plugins and themes folder and the problem persists, plugins and themes are not the problems.

7. Update Your Site URL

Update Your Site URL

Another possible problem could be the site URL for the database. This one is less likely to be the problem, but if nothing else works then it’s worth trying. In the host’s cPanel, go to Databases and select phpMyAdmin.

Update Your Site URL

This opens a query editor allowing you to run SQL on your database. First, choose your database from the list. Next, select SQL. Then, add this line of SQL with your URL:

UPDATE wp_options SET option_value='examplesite.com' WHERE option_name='siteurl'

Finally, click Go. This will update your URL for the database. Once it’s complete, load your website again to see if the problem is solved.

8. Replace Corrupt WordPress Files

Replace Corrupt WordPress Files

If your WordPress files are corrupt, download a new copy of WordPress and unzip it.

Replace Corrupt WordPress Files

Delete the wp-content folder and the file called wp-config-sample.php.

Replace Corrupt WordPress Files

Finally, upload the files to the WordPress root folder. This will overwrite all files except for the two you deleted in the step above. Now, try your site to see if it works.

9. Contact Your Host

If nothing else works, it’s time to contact your host for help. They can provide you with further steps to take. Many hosts will step you through troubleshooting the problem. It’s also possible that they can fix it for you.

Ending Thoughts on the Error Establishing Database Connection Error

That’s our look at troubleshooting and fixing the error establishing database connection message on your WordPress website. These steps are not difficult but it can take time to trace down the problem.

Always use quality themes, plugins, and hosting. Also, be sure to have a backup available just in case there are no other solutions. Before deleting important files, contact your host for help.

We want to hear from you. Have you had the error establishing database connection error? What did you do to fix it? Let us know in the comments.

Featured Image via fedrunovan / shutterstock.com

Premade Layouts

Check Out These Related Posts

How to Use the WordPress Calendar Block

How to Use the WordPress Calendar Block

Posted on September 16, 2021 by in WordPress

Keeping your content organized is an important aspect of running a website. Fortunately, the WordPress Calendar block can help you do that. This element enables you to display a calendar on your site that links to your posts based on their publication dates. As such, your readers won’t need...

View Full Post
How to Use the WordPress Audio Block

How to Use the WordPress Audio Block

Posted on September 15, 2021 by in WordPress

The WordPress Audio Block lets you provide playable audio files within your pages and posts. The audio files can be added from your media library or a URL. Users can play the audio from their browsers or download them to listen offline. This is a great way to provide music, podcasts, or other types...

View Full Post
How to Use the WordPress Cover Block

How to Use the WordPress Cover Block

Posted on September 14, 2021 by in WordPress

The WordPress Cover Block is interesting. It lets you place an image, video, or solid color in the background and add content blocks to the foreground. This means you can place blocks over other blocks. It has more options than most blocks. Knowing how to use the Cover Block can greatly enhance the...

View Full Post
Join To Download Today

Pin It on Pinterest