5 Simple Ways to Fix “Error Establishing Database Connection” in WordPress

The "Error Establishing a Database Connection" message in WordPress is a serious issue that can lock you out of your site. Our article breaks down this common issue into five simple, actionable steps to get your site back online. 
calendar_today
Published: December 28, 2023
Technical Writer
AI Tools For WordPress Creators
Code, learn, troubleshoot, and secure WordPress sites with the CodeWP platform and our custom AI.
Start for Free
Contents

Have you ever been greeted with the dreaded “Error Establishing Database Connection” message on your WordPress site?

This error can be quite alarming as it locks you out of your website and disrupts your business operation. And the thought of tackling the backend is a bit daunting, especially if you don’t have a lot of technical know-how.

But don’t fret. We have compiled five simple methods to help you troubleshoot and resolve this common WordPress error. Hopefully, by the end, you can get your site back up and running smoothly.

Why You’re Getting “Error Establishing a Database Connection” in WordPress

WordPress database error

The “Error Establishing a Database Connection” message might show up when you try to log in to your dashboard, and WordPress fails to connect to its database.

Now, before you panic, it’s important to understand why this error occurs in the first place. Here are the common culprits:

1) Incorrect Username and Password

Remember when you tried to install WordPress on your web host (or local host) the first time? You were required to enter a username and password to create a MySQL database, where all your site’s content (except the media files) is saved.

But after using WordPress for a while, it’s easy to forget these details. When the error occurs, some users might mix up their WordPress admin login with their database login. 

Also, if you or your hosting provider changed these database credentials but didn't update them in the 'wp-config.php' file, WordPress won't be able to establish a connection.

2) Corrupted Database 

A corrupted database can compromise an entire website.

In WordPress, there are several reasons why this might happen. Sometimes, it's due to a server crash, where the database is abruptly stopped and data gets jumbled. Other times, a faulty plugin or theme update can interfere with the database's normal operations. 

Even more concerning, hackers might target your database directly, especially if it's not properly secured. They might drop tables, alter the schema, or even delete the database entirely. They can also inject harmful code into the database which can modify or delete sensitive data, disrupt the structure of the database, or insert spammy links and content. 

Now, when WordPress tries to access this corrupted database, it struggles to find and organize the necessary information. This issue is a serious one, as it directly affects your site's ability to retrieve and display its content. 

3) Server Issues

Server issues can happen for several reasons. One, if it's overloaded due to sudden spikes in traffic. As explained by Yetunde Salami in her article, this issue is more pronounced in shared hosting due to the limited resources and the number of websites competing for those resources. When the server can't keep up with the demand, your site's performance takes a hit, leading to potential database connection problems.

Another possible cause is that the server might be down or experiencing technical glitches. Just like any other technology, servers need regular maintenance. However, things could go wrong sometimes despite your best efforts - software bugs, hardware failures, or network problems.

These technical issues can prevent the server from functioning optimally and cause connection errors with your WordPress database.

Fixing the “Error Establishing a Database Connection” Problem

It’s not always easy to pinpoint the exact cause of database error connection in WordPress. Below, we have prepared some of the common ways to fix this issue. Give each one a shot and see if it clears up the issue.

Before you start, make sure to back up your site. You can do so by manually downloading your files and database, using your hosting provider’s built-in backup solutions, or using a backup plugin (like UpdraftPlus, BackupBuddy, and Jetpack).  

Now, let's get down to business with these solutions:

1) Check Database Login Credentials

The 'wp-config.php' file in WordPress contains some important configurations of your site, including your database credentials – the username, password, database name, and server information. These details are used to connect and interact with the database where your website's content is stored. 

To check and correct your database credentials, you’ll need to access the 'wp-config.php' file from your WordPress directory. There are many ways to do this, but we highly recommend using an FTP application because it’s the easiest.

For our purposes, we’re going to use FileZilla to connect to the server (make sure you have your FTP account set up). If you’re not sure how to use FileZilla, you can check out this tutorial on how to connect to FTP using FileZilla from SiteGround. 

Once you’ve connected to your remote server, navigate to your website’s root directory and look for the 'wp-config.php' file.

access wp-config.php in FileZilla

Next, open 'wp-config.php' in a text editor (you can use Notepad). Look for the database settings section, which contains the database name, username, password, and host. These are the credentials WordPress uses to access your database.

open wp-config.php in VSCode

Then, log in to your hosting panel and locate the database settings. Ensure that the database name, username, password, and host in the hosting panel match the information in your 'wp-config.php' file.

check database credentials in cpanel

If not, then you’ll need to download 'wp-config.php', edit it to correct the credentials, and then re-upload it to your server using FileZilla.

That’s it!

2) Repair a Corrupted Database

Repairing a corrupted database in WordPress can be done in a few steps. 

First, you need to enable the WordPress database repair feature. To do this, access your WordPress core files through an FTP software or through your hosting file manager. 

For this tutorial, once again, we’ll use FileZilla.

Now, locate the 'wp-config.php' file at the root of your WordPress installation. Open this file in a text editor and add the following line of code just before the comment line that says 'That's all, stop editing! Happy publishing.'

JavaScript
define('WP_ALLOW_REPAIR', true);
add repair in wp-config.php

Save the changes and close the file.

Next, run the repair process. Open your web browser and type the following URL (remember to replace 'yourwebsite.com' with your actual domain name):

http://yourwebsite.com/wp-admin/maint/repair.php

Here, you’ll see a page with two options: 'Repair Database' and 'Repair and Optimize Database'. Click on 'Repair Database' if you want a quick repair. Choose 'Repair and Optimize Database' for a more thorough check (note that this option might take longer).

repair WordPress database

The repair process will start, and WordPress will attempt to fix any issues found in the database. Once the process is complete, you'll see a report of what was fixed.

After the repair is finished, you’ll need to disable the repair function for security reasons. Go back to the 'wp-config.php' file and comment out or delete this code (the one you added earlier): 

define('WP_ALLOW_REPAIR', true);

Save the changes and re-upload the file to your server via FTP.

upload wp-config.php using FileZilla

Finally, open your WordPress site and check if the database connection error is resolved.

3) Check Your Server

Sometimes, your database server might temporarily go offline due to sudden spikes in traffic, limited resources, technical issues, or incorrect server configurations.

To resolve these server issues, your first step should be to contact your hosting provider. They can inform you about any ongoing server issues, maintenance, or outages. In this case, your only option is to wait until your hosting provider resolves the problem.

Monitoring your site's traffic can help you understand if traffic spikes are causing the problem. If this is a consistent issue, consider upgrading your hosting plan to a VPS or dedicated hosting for more resources and stability. 

4) Try Deactivating A Plugin or Theme

A corrupted plugin or theme can occasionally conflict with your database, especially after updates or if they're not well-maintained.

To check if a plugin is causing the issue, you can try to deactivate it using FTP software. Connect to your site using FTP, go to the 'wp-content' folder, and find the 'plugins' folder. Rename this folder (this could be something like 'plugins_deactivated') to deactivate all plugins at once. 

rename WordPress plugin folder

After doing this, check your site. If the error is gone, it's likely one of the plugins was the issue. You can then check each plugin by reactivating them one by one.

The same approach applies to your WordPress theme. If deactivating the plugins doesn’t solve the issue, try renaming your 'themes' folder via FTP to deactivate it. If this resolves the error, your theme might be the problem.

If the error persists, then you can be fairly certain that the problem lies elsewhere. In this case, you can revert the names of the 'plugins' and 'themes' folders to their original state and move on with other troubleshooting steps. 

5) Create a New Database

Creating a new database should only be considered a last resort if nothing else seems to work. You must approach this method with caution since creating a new database means you’re starting from scratch.

To create a new database, you’ll need to log in first to your cpanel account (note that we’re using Namecheap for this tutorial).

From the left side menu of your dashboard, select ‘Tools’, scroll down to the ‘Databases’ section, and click on ‘MySQL Database Wizard.’

access MySQL database on cpanel

Start the wizard and enter a name for your new database. Click ‘Next Step.’ Remember, this database will store your WordPress site’s data.

create WordPress database on cpanel

Now, create a user for the database. Choose a username and a strong password. Click ‘Create User.’

create WordPress database user

On the next screen, assign this user to your newly created database. Grant all privileges for full access and management capabilities. Click ‘Next Step’ to finalize the settings.

add WordPress database user

The wizard will confirm that your database and user are created. Note down the database name, username, and password – you’ll need them for configuring WordPress.

complete database setup

Finally, update your WordPress configuration with the new database details. Edit the ‘wp-config.php’ file in your WordPress installation and replace the DB_NAME, DB_USER, and DB_PASSWORD with your new database information.

Note: If you have access to your phpMyAdmin account, you can also create your new database here. Follow this tutorial Creating a MySQL Database for WordPress from Melapress to learn how.

Error Establishing a Database Connection - FAQs

What does it mean when it says a database error occurred?

In WordPress, a database error means the website can't access or save data in its database. This is commonly caused by incorrect database credentials, corrupted database files, or technical issues with the server.

How do I fix error establishing database connection?

To fix the database connection error in WordPress, you can start by checking your database login credentials in the 'wp-config.php' file. If these steps don't resolve the issue, consider the other methods we’ve mentioned in this article, such as checking for server issues, deactivating plugins and themes to rule out conflicts, or creating a new database.

How do I connect my database?

To connect your database in WordPress, simply edit the 'wp-config.php' file and fill in your database details – the database name, username, and password. Make sure that these credentials match with the ones you’ve used during the WordPress installation process.

To Sum Up

The “Error Establishing a Database Connection” message is a common WordPress error. Thankfully, it can be easily fixed with the right steps. But if you don’t like handling this issue yourself, consider reaching out to your hosting provider for quick assistance.

We hope this guide has been helpful.  

Want to get even better at managing your WordPress site? Take a look at our other tutorials:

About The Author
Christy Cañete
Technical Writer
More By Christy Cañete
Christy is a front-end developer and technical writer with expertise in JavaScript, React, Node.js and other web technologies. As a writer for CodeWP, she breaks down complex AI and WordPress concepts into easy-to-understand tutorials so anyone can leverage the platform's advanced capabilities. Christy's experience spanning web development, IoT, and embedded systems enables her to clearly explain technical topics to empower WordPress creators of all levels. Her passion is making the latest innovations accessible through beginner-friendly writing.
More
Tags
WordPress WordPress database error

More Reading

Make WordPress Easy With AI
© WPAI, Inc. 2024 | d.b.a CodeWP