Categories: Troubleshooting

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

Under-the-hood, your WordPress website is powered by a MySQL database. Most of the time, this software will run quietly in the background, but occasionally you may encounter the following warning: “Error establishing a database connection”. 

Fortunately, there are several ways to fix this common WordPress error. By taking quick, decisive action to get your database back on track, you can remove this warning before it’s noticed by your site visitors – or even worse, the search engines. 

In this post, we’ll look at the main causes of this WordPress error. We’ll then share four tried-and-tested ways to fix it. Let’s get started!

The Database Connection Error (And Why It’s Bad News for Your WordPress Website)

As a Content Management System (CMS), WordPress stores all of its website data and content in a database. Every time someone visits your site, WordPress will connect to that database and retrieve the requested information. 

When you attempt to connect to your website, you may occasionally come across the following message: “Error establishing a database connection”. There are a few different reasons why you may encounter this error.

In order to connect to your database successfully, WordPress requires the database name and server, as well as a username and password. WordPress stores all of this information in a configuration file. 

If any of these details are incorrect, then WordPress will be unable to establish the connection. There’s also a chance that your database server may be down, or that your database files are corrupt. 

Since there are a few different causes, this problem can be tricky to resolve. However, you should always take this error seriously, as it can prevent visitors from accessing your site. This can be disastrous for your traffic, conversions, and your reputation. If the error persists, your customers may go elsewhere.

A study by Moz found that 500 internal server errors can even cause issues with tracked keywords. If your site remains unavailable for a significant period of time, this could impact your search engine rankings.

With over 53 percent of all web traffic originating from organic search, you won’t want to put your Search Engine Optimization (SEO) at risk. Therefore, it’s important to resolve this issue as quickly as possible. 

How to Fix “Error Establishing a Database Connection” in WordPress (4 Ways)

This is one of the most common WordPress errors, but it isn’t always the easiest to diagnose. That’s why we’ve created this complete guide to getting to the root of the problem, and fixing it. Here are four ways to resolve this database connection issue.

1. Check Your WordPress Database Credentials

There are several factors that can contribute to a database connection error, but incorrect credentials are the most common culprit. If you’ve recently migrated your site to a new host, then you may be at increased risk of mismatched credentials.

WordPress stores all of your database information in the wp-config.php file. You can examine this file via a File Transfer Protocol (FTP) client such as FileZilla.

Once you’ve downloaded FileZilla, open the program and navigate to File > Site Manager:

If you’re connecting to your server for the first time, then select New Site. You can then assign a name to this connection, and enter your FTP username and password. When you’re done, click on Connect:

If you don’t know your FTP credentials, you can find this information in your cPanel account or the admin tool supplied by your hosting provider. Assuming that the connection is a success, all of your WordPress files should appear in FileZilla’s right-hand window. You’ll find the wp-config.php file in your root-install directory:

You can then Control-click this file and select Open. This launches the wp-config.php file in your default text editor:

In this file, you’ll find the database credentials in the following section:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */define( 'DB_NAME', 'database_name_here' );
/** MySQL database username */define( 'DB_USER', 'username_here' );
/** MySQL database password */define( 'DB_PASSWORD', 'password_here' );
/** MySQL hostname */define( 'DB_HOST', 'localhost' );

You can verify that this information is correct by comparing it to the credentials in your WordPress hosting account dashboard. Every hosting provider’s dashboard is unique, but you’ll typically find this information by looking for any options labelled as database or MySQL database

If the information in this file is incorrect, you can update it to match the details in your hosting account. When you’re ready, you can try visiting your website again to check if the issue has been resolved.

2. Repair the WordPress Database

Over time, your database can become corrupted. Fortunately, WordPress has a built-in tool that promises to repair a misbehaving database with the click of a button.

To enable this repair tool, open your site’s wp-config.php file and add the following line:

define('WP_ALLOW_REPAIR', true);

Save and close your wp-config.php file. Next, navigate to the following URL, making sure to replace mywebsite.com with your site’s domain name: 

https://mywebsite.com/wp-admin/maint/repair.php

You can now choose whether to repair your database, or repair and optimize your database. We recommend opting for the latter, since it never hurts to fine-tune your database: 

After a few moments, you’ll see a page of code. Scroll to the bottom of this screen and you should spot the following message: “Please remove the following line from wp-config.php to prevent this page from being used by unauthorized users”.

Warning: It’s possible for people to access the database repair tool without being logged into your WordPress website. This is a security risk, so it’s important to open your wp-config.php file and remove the WP_ALLOW_REPAIR line immediately after repairing your database. 

3. Check Whether Your Database Server Is Offline

Occasionally, your database server may go down, particularly during heavy traffic. If your MySQL server is offline, then WordPress will be unable to connect and you’ll encounter the database error message.

If you’re running multiple websites on the same server, then it’s worth trying to visit these sites in your web browser. If they all display the same error, then it’s likely that your MySQL server is offline. 

Meanwhile, if you have another site on the same hosting account, you can also try to connect to the database via the phpMyAdmin tool. If you cannot access MySQL, then chances are an unresponsive server is to blame. 

When your server is down, it’s a good idea to contact your hosting provider. There’s a chance they may be unaware of the problem. They may also be able to provide a timescale for when they expect to bring your server back online. You can then pass this information onto your customers, co-workers, or anyone else who is waiting to access your website. 

If you have a dedicated server, local server, or Virtual Private Server (VPS), then you can try rebooting your server manually. This may be enough to bring your database back online. 

The process for restarting your server will vary, so you may need to check your hosting provider’s documentation for instructions. Alternatively, if you’re not sure how to perform a restart, you can contact your web host for assistance. 

4. Update Your Site’s URL

Some members of the WordPress community have reported getting positive results after updating their WordPress site URL. You can make this change via the phpMyAdmin tool, which is typically included in your hosting account dashboard. 

To make this change, you’ll need to enter your table name. By default, this is wp_options. However, it is possible to change the wp_ prefix. If you’re unsure, then you can check your table name by logging into your dashboard and accessing the phpMyAdmin tool. You’ll find the table name in the left-hand column:

After logging into your dashboard, launch phpMyAdmin and select the database that’s causing you problems. Next, click on the SQL tab:

You can then enter the following MySQL query:

UPDATE wp_options SET option_value='MY_URL' WHERE option_name='siteurl'

Make sure to replace MY_URL with the URL of your WordPress website. You’ll also need to change wp_options to reflect your table name.

After running this query, try re-accessing your WordPress website. This may have resolved your database connection issue.

Conclusion

The “Error establishing a database connection” issue prevents users from accessing your site and may also hurt your search engine rankings. Therefore, if you ever encounter this warning, you’ll want to resolve it as quickly as possible. 

Let’s recap four ways to fix this common WordPress error:

  1. Check your WordPress database credentials, using an FTP client like FileZilla.
  2. Repair the WordPress database through your site’s wp-config.php file.
  3. Check whether your database server is offline by contacting your hosting provider.
  4. Update your site’s URL via the phpMyAdmin tool.

Do you have questions about how to reconnect your website to your database? Let us know in the comments section below!

Will Morris

Recent Posts

How to Reset WordPress: Step-by-Step Guide for Beginners

Are you looking to reset a WordPress site and start fresh? Reset a WordPress Site…

5 days ago

WordPress vs. Wix: Which Platform Is Right for You?

Ready to build your online presence? Choosing the right platform between Wix vs. WordPress is a crucial first…

5 days ago

How to Change Favicon in WordPress: A Step-by-Step Guide

Changing your favicon in WordPress is a small yet powerful way to improve your website’s…

5 days ago

How to Embed a PDF in WordPress

Have you been searching for the easiest way to share PDF files directly on your…

1 week ago

How to Add Social Media Icons to WordPress Header: A Complete Step-by-Step Guide

Ever notice how professional websites seamlessly display social media icons in their headers? Adding social…

1 week ago

How to Clear Cache on WordPress: A Step-by-Step Guide

WordPress caching is an incredible tool for boosting your website’s performance. It helps deliver faster…

1 week ago