Question

Error establishing a database connection

When I log in to edit my site through WordPress, after doing some work on the site, it starts giving an “Error establishing a database connection” error, and the CPU usage also goes up to 100%. How can I fix this?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
April 3, 2024

Hey,

The error might be due to your database struggling under load, which can also cause high CPU usage. Sometimes, an unexpected surge in traffic, particularly of a malicious nature, can cause your resources to max out. You should review your access logs to identify any unusual activity. Here is a step-by-step tutorial on summarizing your Nginx and Apache access logs, which can help you spot and address any anomalies: Summarize Access Logs.

Also, optimizing your MySQL or MariaDB configuration can significantly improve performance. Here is a guide that walks you through tweaking your database configuration for better performance and stability. You can check it out here: Optimize MySQL/MariaDB.

You can also set up Cloudflare as it can help offload some of the traffic and provide additional caching benefits, which might reduce the load on your server. It can also provide added security features that might prevent malicious traffic from reaching your site.

If you consistently hit high CPU usage and your site’s traffic is growing, it might be time to scale up your resources. Adding more CPU and RAM can help accommodate your site’s demands, especially during peak traffic times.

Lastly, ensure that your WordPress and all plugins/themes are up to date. Sometimes, a simple update can resolve performance issues and patch security vulnerabilities. Also, remove any unnecessary plugins and themes that you are not using.

Hope that this helps!

Best,

Bobby

alexdo
Site Moderator
Site Moderator badge
March 30, 2024

Heya, @salamexpert

What I could suggest is following the steps from this tutorial here on how to tweak your MySQL configuration for better reliability:

https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

Additionally, I would recommend checking your MySQL error log so that you could get some more information on what might be going wrong. It sounds like the server might be running out of RAM.

If this is the case, I would suggest considering adding a bit of extra RAM and possibly a SWAP file:

https://www.digitalocean.com/community/tutorials/how-to-add-swap-space-on-ubuntu-22-04

Let me know how it goes!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel