SOLVED – ‘Error establishing a database connection’

If you are a regular reader of the blog, you might have experienced an intermittent error where you are shown the following error:

Error establishing a database connection‘ 

This particularly fickle error has plagued me for about 2 months. Today, I was able to resolve it so I’m putting this information out there to hopefully help someone else out.

I’ll cut right to the chase and tell you that the Rank Math SEO plugin was the source of my woes. Once I removed Rank Math and ran WP-Optimize, the issue went away.

Specifically, my issue was a blown out MYSQL table ( _postmeta ).

Somehow my WordPress postmeta table had ballooned up from 50MB to almost 700MB. This created a TON of issues including the above database connection errors.

I was able to use another plugin called Database Browser to inspect the tables and I found some really weird rows called post_views and post_views_count. The Post Views rows were being appended with visit information (for each post view) and were bloating the database without significantly adding to the rows. Super hard for me to figure out without actually looking at the tables and rows.

After dropping Rank Math, the database compressed back down to around 50MB. Hopefully this is the end of my Database connection issues. In addition to eliminating the source, I also added the follow performance tweaks to the system.

Increased MAX memory to 256M -> WP-CONFIG.PHP
Increased Memory limit to 256M -> WP-CONFIG.PHP
Increased PHP Memory Limit to 256M -> PHP.INI
Increased max_execution_time to 600 -> PHP.INI

I followed some really great information from this blog post.

Thanks for reading this & Happy Troubleshooting!