WordPress: “Error Establishing a Database Connection”

This post originally appeared on https://tomaltman.com/wordpress-error-establishing-a-database-connection/

Web hosts always blame WordPress – I want to create a faux WP site sometime and have a non-WP error so when I contact them and they tell me it is WordPress I can call BS. Is that wrong?

I consider myself a fairly well versed user of the WordPress – I’ve been a web developer for over 10 years now and a bit of a technologist my whole life. But there is something about a rash of emails from your website monitoring service to get your head spinning.

Yesterday – this happened to me. So, I do what every level headed web dude would do. I create a support ticket with my host and say “Hey – what did you guys do to my site!”. I knew it wasn’t them and I knew they were going to blame it on WordPress. They always do.

Also Read:

WordPress Database Connection Error

Anyway…back to fixing the issue. So – about 4-5 times out of 10 when I refreshed my site I was getting the “Error Establishing a Database Connection” messages. Here’s how those look on the backend vs frontend. If you see this…you have “issues”.

WordPress "Error Establishing a Database Connection" - when viewing the site
WordPress “Error Establishing a Database Connection” – when viewing the site
WordPress "Error Establishing a Database Connection" - when logging into admin.
WordPress “Error Establishing a Database Connection” – when logging into admin.

How to fix it?

  1. The first “step” is admitting your site has a problem. That took me a little while.
  2. Step two – the real problem is you are using too many connections from the WordPress code to the database. It makes it sound like you have a database issue – but in reality…a part of the php code is poorly written.
  3. Its usually a plugin, but can be a theme. So – disable all the plugins. Its painful – I know. But trust me when I say – its the easiest was to figure it out.Here is the mini menu on the admin screen for plugins.Here is the mini menu on the admin screen for plugins.The menu WordPress provides for plugins is very good. There are two things you need to focus on – Active plugins (red box above) and Recently Active (blue box above). Go to your “Active” plugins and disable them all. This will put them all into “Recently Active”.
  4. Check the site. With no plugins…refresh the site 10-20 times. Is the “Error Establishing a Database Connection” gone? Don’t forget to get the admin and the homepage. Also – click around the site a bit. If you look at 10-20 pages and you don’t see the WordPress issue…you probably have it.Still see it with the plugins disabled? – change themes to a default WordPress version. Back to rechecking…did that work?If it didn’t – you may have a bigger issue and I would consider looking into a company like https://fixmywp.com – these guys can help if you find your self in a real pickle with no options.
  5. Plugin Problem:
    Now you have to go through the work of turning on plugins. I usually do 2 at a time, this will cut the time to test down quite a bit. But you basically do the same thing as in #4 above. Browse the site and see if the connection issue comes back. If it doesn’t after 10-20 refreshes…activate more plugins.
    Theme Problem:
    I’m not sure if it is easier or not – but you’ll need to find a new or updated theme. Themes use the WordPress framework to “get things done”. I know WP changes things from time to time and it may be your theme was not updated.

If you have SSH access to the server – you can also run this command:

Also Read:

mytop -u {wordpress-database-username} -d {wordpress-database-name} -p '{wordpress-database-password}'

This will give you a list of all the connections to the WordPress database – you want to see 1 or 2 – a list is bad.

That is really what you need to do to figure things out – it can be a single plugin, and combination of two/three plugins or even a bad theme. This is when you can see the value of paying for plugins and themes. The premium market has a reputation of putting out better themes and plugins.

Please feel free to contact me if you have questions – I’ll do my best to answer or help when I can.

Also Read:

Leave a Reply

Your email address will not be published. Required fields are marked *