Error establishing a database connection - recurring (Ubuntu)

November 24, 2013 8.4k views
After a reboot of the droplet, all works fine for a while. I'm in the 512 MB memory plan. I have 2 WP sites installed. A backup plugin is installed but running weekly. In addition, when I get the error, and check on the status of mysql I get this: mysql stop/waiting
14 Answers
Looks like your MySQL server is failing out, chances are you're running out of RAM. Check the logs for reasons as to why MySQL would be stopping.
by Etel Sverdlov
Varnish is an HTTP accelerator and a useful tool for speeding up a server, especially during a times when there is high traffic to a site. It works by redirecting visitors to static pages whenever possible and only drawing on the server itself if there is a need for an active process. This tutorial covers installing wordpress on a LEMP stack (with nginx instead of apache), and then installing varnish.
The logs are empty as far as I can see.
In addition, I've been watching the memory as I do certain things. The error is appearing again and this is what outputs when I "watch" memory

total used free shared buffers cached
Mem: 495 284 210 0 4 51
-/+ buffers/cache: 228 267
Swap: 0 0 0

It appears I have free memory! So could something else be causing the issue?
I am running Apache. Thanks for the suggestion(s) but I'm trying to figure out what is causing the issue and your answer doesn't provide that (maybe it does, but I'm new to VPS so it doesn't for me).

If anyone can expand on Pablo of vDevices answer to explain or has any other advice/answers to offer, I'd greatly appreciate it!
"... but I'm trying to figure out what is causing the issue ..."

Apache consumes a lot of a server's resources. MySQL can consume a lot of resources, particularly if your websites serve a lot of dynamic content. Put the two programs on the same server (particularly one with only 512MB of memory) and you'll be frustrated by timeout issues.

Memcache can lighten the load on MySQL. Swap provides MySQL (and the rest of the programs on your server) with more than the 512MB of memory you currently have available.

"... and your answer doesn't provide that ..."

Read the articles I cited for ya. If you're interested in learning more, check out WordPress Hosting: Apache or Nginx?.
I read both articles as I stated, it doesn't answer my question since when I do a memory check, I'm not using all of it as I posted above. Because of that, I'm not convinced it's a memory issue. It may very well be but if it was a memory issue - when the error appears, shouldn't the memory check show that it's all being used?
There is a specific mysql log file that you may want to look at if you haven't already. It should be under /var/log/mysql/mysqld.log for CentOS, but it can vary depending on distribution and setup. There may be some additional information there. Besides the memory allocation and the mysql logs, you may wish to have the wordpress installs be as clean as possible. I try to keep only a few plugins running and installed. I've seen folks have 20+ plugins and that can lead to problems.
The logs are empty. I have minimal plugins. I upgraded my droplet to the 1GB and I'm STILL having these issues.

So I stand by the fact that this isn't memory related. Again, I do a memory check and I'm NOT at capacity.
Check MySQL's error log:
sudo tail /var/log/mysql.err
I too have the same problem, now my wordpress site is down and I don't know how to access it again, just so I could backup my posts...I really need them all, including a complete mySQL backup...but both "/var/log/mysql.err" and "/var/log/mysql.log" are empty. how to I regain access to my site?
OK, i SSH to my droplet and ran the command: service mysql restart
sudo tail /var/log/mysql.err returns empty, swap is never used, there is no problem with apache and wordpress because mysql is the same as for ngix so no need for ngix and no need for more ram.
did anyone resolved this??? a lot of bad suggestions i have read all over this community and no one doesn't even know the source of the problem...
Thank you @Pablo. The pages you provided about WP caching were incredibly illuminating. I really had no clue how much a beast Wordpress is on a system. I did a swap a while back based on @Kamal Nasser's post, but didn't install a caching system.

Also, if anybody needs to do quick testing, helped. Here's my recent test results for a client
Have another answer? Share your knowledge.