5 Ways to Solve ‘Error Establishing a Database Connection’ on WordPress

5 Ways to Solve ‘Error Establishing a Database Connection’ on WordPress

I’m going to make a wild guess: right now, your WordPress blog is down, and there’s a plain white page with big black text saying Error establishing a database connection. I’m right, aren’t I? I’m a little bit psychic. Anyway, what’s happening, and how can you fix it? The short version is that there’s been a breakdown between your frontend and database. The long version? Well, let’s break it down into five headings:

  1. Incorrect Login Credentials
  2. Database Corruption
  3. Server Issues
  4. The Professional
  5. The Nuke

1. Your Login Credentials Have Changed or are Incorrect

You need correct MySQL login details to access your database—if these have changed or you’re inputting them incorrectly, you cannot access any of the necessary information. Navigate to wp-config.php and look for the following:

[code]

define(‘DB_NAME’,
‘X’);

define(‘DB_USER’, ‘X’);

define(‘DB_PASSWORD’, ‘X’);

define(‘DB_HOST’,
‘localhost’);

[/code]

The Xs are variables. The host can also change, but it’s usually going to be localhost, or possibly your IP. You’re here for DB_USER and DB_PASSWORD, but check the other two just in case. Make sure all these details are correct for your MySQL login, and fix them if they’re not. How to fix them will vary depending on your setup (for me: cPanel → phpMyAdmin. I like the classics) but it’s usually a fairly straightforward process. From personal experience, this accounts for a solid 75% of all Error establishing a database connection incidents and should be the first thing you check every time. 

2. Database Corruption

Sometimes because of malicious code or a bad plugin, sometimes just plain bad luck, but something has gone fatally wrong at the database level. A table is stuck in the gears somewhere, and WordPress couldn’t unstick it without an extra push.  

The simplest solution is to use WordPress’ built-in repair code.  Navigate to wp-config and add the following line:

[code]define (‘WP_ALLOW_REPAIR’, true);[/code]

Then hop into your browser and go to:

[code]yoursite.com/wp-admin/maint/repair.php[/code]

You’ll get a WordPress repair screen with a 1-button database fix. This will only cover common issues, but common issues are, well … common. They happen a lot, and I wouldn’t be surprised if you’re dealing with one right now. After login credentials, this is your #2 port of call. Remember to remove the REPAIR line when you’re done.

If you’re still having issues, the thing to try is—assuming you’ve been making regular backups—to roll back to the most recent working version of the database. If that would result in unacceptable loss of data/work, then you’re going to need to go in manually and figure out what’s causing problems. If that didn’t work, you’re going to need to dive into your FTP and go hunting.

Make a list of all recent plugins/addons/general changes, and locate their associated folders. You’re going to need to go through folder-by-folder. Find one that seems likely and rename it (recommended method: add an underscore after the name, myfolder → myfolder_) and see if that fixes the site. If it does, you’ve located your issue. If it doesn’t, change the name back and go onto the next folder. Common culprits are plugins and themes, especially free ones.

3. Server/Hosting Issues

Sometimes, a massive traffic spike (or DDOS attack) can throw up this error. Check your hosting and metrics and see if anything has changed—if some content went mega-viral and the server couldn’t handle it, then you’re probably going to want to upscale your server. Most modern VPS hosts will have a way of doing this—you can see how to do it with DigitalOcean here. Some hosting services will even resize automatically based on traffic, though they tend to be more expensive and can be difficult to use properly. Since we’re running WordPress, your host is going to vary, as is the method of upsizing your VPS. If you want to know about my favourite server hosts, check out this comparison of DigitalOcean vs Vultr.

If it’s a hosting issue that’s not that, then it’s really out of your hands. File a support ticket with your server host and wait for them to get back to you.

4. The Professional Option  

Has everything else failed? Well, maybe it’s time to call in the cavalry. WordPress development companies aren’t an option for everybody, but if you really absolutely need something fixed, you’re going to need to get out your wallet. They can do other work while they’re in there—suggest tweaks for optimization and security and the like. A lot of developers do WordPress full-time and they know their way around these systems like a sherpa knows Everest.

Another popular option is to hire freelancers from a trusted source like Upwork or Toptal. Freelancers tend to have higher hourly rates that companies, but are better for retaining in short bursts for a single job. If you think the issue is unlikely to recur then you probably just want a freelancer for a few days. If it turns out the issues go deeper, you might want to talk to a company.

5. The Nuke

Don’t want to bring on a professional, but can’t figure out any other fix? You can reset WordPress without totally destroying the site. This will let you keep a lot of critical data (your URL, DNS configuration etc) but will remove the site’s actual content. Use cPanel’s DROP function to delete the database, then start building again.

Once that’s sorted, run the WP installation script again and let it fill in the cracks. This is not a pretty solution, but I’ve had to do it more than once when it turned out the database corruption went so deep and far back it was easier to just burn the whole thing down. If you skipped down to the last one because it sounded the most interesting, please try the others first. And good luck! WordPress databases errors can be the bane of your existence, but hopefully you’ve picked up a few tools to help you keep them short and painless.

Comments

0 comments

James Williams
James is our Lead Content Publisher here at Feeds Portal. He has worked with many top websites over the years, including BuzzFeed.

Log In

Forgot password?

Forgot password?

Enter your account data and we will send you a link to reset your password.

Your password reset link appears to be invalid or expired.

Log in

Privacy Policy

Add to Collection

No Collections

Here you'll find all collections you've created before.