Fixing The Error Establishing a Database Connection WordPress: A Comprehensive Guide

by

7-layers of Security for Your WordPress Site

Your website needs the most comprehensive security to protect it from the constant attacks it faces everyday.

error establishing a database connection wordpress

You visit your website, expecting to see everything in place, but you’re faced with a blank screen. The content and posts you worked hard on aren’t there. Instead, there’s an unexpected message: Error establishing a database connection.

You’re probably wondering what went wrong and how to get your site back to normal. But don’t worry—this guide will help you understand the problem, explain why it happened, and, most importantly, walk you through the steps to fix it.

TL;DR: The error occurs because your server isn’t connecting to your database. A reliable backup solution, such as BlogVault, can be useful in times like this. It creates automatic, daily backups of your entire site, allowing you to restore the latest version when needed.

What is the error establishing a database connection

Your WordPress database is like the brain of your site, holding key information such as posts, pages, media, and plugin settings. For everything to run seamlessly, your server must connect to this database. 

If it fails to do so, you’ll see the dreaded Error Establishing Database Connection. Let’s talk about what triggers this error and the solutions to fix it.

error establishing a database connection message

What triggers this error?

1. Web host problems: Sometimes, the issue is with the web host, and it’s not something you can fix on your end. You’ll need to contact their support team for help.

2. Wrong database credentials: WordPress needs specific details to connect to your database, like the database name, server, username, and password. If it’s incorrect, update by editing your wp-config file.

3. Faulty plugin or theme: A problematic plugin or theme might disrupt the connection. To resolve this, disable any plugin or theme that’s causing trouble.

4. Corrupt WordPress files: Corrupted WordPress files could be the cause, possibly due to a hack or malware. You’ll need to reinstall a fresh set of WordPress files.

5. Corrupt database files: A corrupt database can also lead to this error, though it’s less common. If corrupt, you can repair your WordPress database through your cPanel.

How to fix the error establishing a database connection issue

Check the server connection with your host  

Before jumping into deeper troubleshooting of this issue, reach out to your hosting provider. Explain the error you’re facing and see if they can fix it. If not, proceed with the following steps: 

Verify database credentials  

Often, incorrect database credentials cause connection errors. These details include your database name, server, username, and password, which are located in your site’s wp-config file. To verify and update these details, use FTP.

1. Find your database credentials on your hosting account.

2. Connect to your server using an FTP client.

Connect site to FTP

3. Navigate to the root folder often public_html or www and open the wp-config.php file.

Public_html > wp-config.php file

4. Compare the credentials in the file with those on your hosting account and update if necessary.

WordPress database credentials

5. Save and upload the updated wp-config.php file back to the server.

Upload file to FTP client

Disable plugins and themes  

Faulty plugins or themes may be disrupting the database connection. Here’s how to check:

1. Use your FTP client to access the wp-content folder within the root folder which is the public_html or www.

wp-content folder

2. Rename the plugins folder to deactivate all plugins, then check if the site works. 

wp-content folder

3. If it does, rename and activate plugins individually to identify the problematic one.

deactivate plugin

4. Repeat the process for themes if needed.

Note: We strongly recommend deleting unused plugins. Simply deactivating them and leaving them on your site adds unnecessary clutter and can lead to a “database disk full” error.

Replace corrupted files  

Corrupted WordPress files can also cause issues. To address this:

1. Download a fresh copy of WordPress from wordpress.org.

2. Use the FTP client to upload new files, replacing everything except the wp-content folder and wp-config.php file.

Repair the database  

If your database itself is corrupted, you can fix it through your cPanel or wp-config file:

Via cPanel

1. Navigate to cPanel and then find the Databases section. Within this section, click on phpMyAdmin.

phpMyAdmin via cPanel

2. Next, in the Database tab, look for and select the database you need. Follow it by selecting the tables that need repairing, and scrolling down to the dropdown menu. 

Databases

3. From the dropdown menu, click on the Repair table option. You will then be taken to a page that confirms the repair is complete. 

phpMyAdmin repair table

Check if your site is back online. If it isn’t, you’ll need to restore your site from a backup, a reliable method to recover your site.

Via wp-config file

1. Add the following line of code to your wp-config.php file towards the end right above the line that says, “That’s all, stop editing! Happy blogging.”

define(‘WP_ALLOW_REPAIR’, true);

repair table in wp-config file

2. Run the repair by pasting the following URL in your browser. Make sure to replace “yourdomain.com” with your website’s URL.

“https://yourdomain.com/wp-admin/maint/repair.php”

Restore your site  

If all else fails, restoring your site from a backup is your last resort. It’s best to regularly back up your site to simplify restoration. Tools like BlogVault can help automate and schedule backups, allowing easy restoration with just a few clicks even without accessing the admin panel. Select the correct backup version to ensure the most recent data is restored.

MalCare backups

Best practices to prevent this issue from reoccurring

Here’s how you can ensure your website stays up and running smoothly:

Backups

Backups are your ultimate safety net. Every time there’s a change, like updates or new posts, a backup is essential. With a backup solution like BlogVault, you can set automated backups to run regularly. This way, you’re always prepared for any unexpected issues.

Security

Strong security measures are important if you want to keep your site safe. Malware and hackers are always on the prowl, but with BlogVault’s security features, you’re protected. It offers automated scans that alert you to any threats, and with just one click, you can remove any malware. Plus, its real-time firewall blocks potential attacks, keeping your site safe and secure.

Good web hosting

Choosing a reliable web host is foundational for a hassle-free website experience. A good host provides not only robust performance but also a supportive community. This ensures that when you encounter issues beyond your control, expert help is just a call away.

Parting thoughts

Fixing the “Error establishing database connection” can be a bit tricky, but you’ve got options. While these solutions might take a little effort, the best way to be ready for any future hiccups is to have a solid backup plan in place. BlogVault can help with that.

With BlogVault, you can set up automatic backups easily. You can schedule your backups when traffic is low, ensuring minimal disruption. BlogVault also provides real-time backups, securely storing them on their servers, so you don’t have to worry about storage. 

FAQs

How to fix the “Error Establishing a Database Connection”?  

Start by checking your wp-config.php file for correct database name, username, and password. If these details are correct, contact your hosting provider to check for server issues, or try disabling any recently updated plugins or themes to troubleshoot further.

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

It means your website is having trouble communicating with its database, preventing it from retrieving necessary information.

Where is the wp-config.php file?  

You can find the wp-config.php file in the root directory of your WordPress installation. It is often named public_html or www. This can be accessed via an FTP client or your hosting control panel.

Why do I keep getting the “Error Establishing Database Connection” message on WordPress?

This error often occurs due to incorrect database credentials, hosting provider issues, or conflicts with plugins or themes. Ensure your database details are accurate and review recent changes or updates that might be causing the issue.

Category:

You may also like


How to Whitelist an IP Address in WordPress
How to Whitelist an IP Address in WordPress

Whitelisting IP addresses is a manual way to ensure that certain IPs have access to your WordPress website. Ideally, you’d want your WordPress firewall to take care of that hassle…

5 Best WordPress Firewalls to Block Attacks
5 Best WordPress Firewalls to Block Attacks

To keep your WordPress site secure from hackers, prevention is key. Fixing a hacked site can take a lot of time, effort, and money. Hacks can also cause your site…

How can we help you?

If you’re worried that your website has been hacked, MalCare can help you quickly fix the issue and secure your site to prevent future hacks.

My site is hacked – Help me clean it

Clean your site with MalCare’s AntiVirus solution within minutes. It will remove all malware from your complete site. Guaranteed.

Secure my WordPress Site from hackers

MalCare’s 7-Layer Security Offers Complete Protection for Your Website. 300,000+ Websites Trust MalCare for Total Defence from Attacks.