error-establishing-database-connectionMySQL service crashing a lot on your new VPS box? Unwilling to pay even more for an appropriate amount of memory that’s needed to support WHM’s [mostly] default configuration? The guide below might just be the solution you’re looking for. I hope it is and that you found it on your first Google search.

I wish that had been the case for us, but my pain is [hopefully] your gain.

Optimizing Memory Usage on a WHM/Cpanel VPS Server

WARNING: The following tweaks require a sound understanding of your VPS server and WHM/Cpanel. Some of the tweaks below WILL cause problems if your environment and/or website is dependent on them. Proceed at your own risk with extreme care and take a freakin backup.

Environment We Ran These On:
CentOS 6.7
WHM 11.50.2
2.4 Ghz Dual Core Processor
1 Gig of RAM
1 website being hosted – A high traffic WordPress based site

  1. Optimize MySQL: By far the most impactful of the configuration tweaks we implemented (300mb+ reduction in idle server memory usage)
    1. For MySQL 5.6.6 +, turn off performance_schema . This is turned on by default in 5.6.6+ and seems to eat RAM for breakfast (more info on this)
      To disable performance_schema:

      1. Open my.cnf (typically @ /etc/my.cnf) via SSH or VPS admin panel » add the following line just under [mysqld] » performance_schema=0
      2. Save/close my.cnf and restart mysql service
    2. For VPS server’s with very limited memory, high traffic and/or a lot of MySQL databases convert InnoDB tables to MyISAM. This might impact load times slightly, but it also allows the converted database tables to be accessed with a much lower memory footprint . You can do this on each table in PhpMyAdmin or with scripts. Google it.
  2. Stopping SpamAssassin: This program isn’t overly important to the stopping of spam. To turn it off do the following:
    1. Go to WHM » Server Configuration » Tweak Settings » Disable Spamd
    2. Go to WHM » Service Manager » Disable Spamd
  3. Stopping Mailman: If you or none of your customers are running e-mail accounts through cPanel, it would be a good idea to disable Mailman (this will not affect web-forms being sent out).
    1. Go to WHM » Server Configuration » Tweak Settings » Disable Mailman
  4. Stop ClamAV: Never use ClamAV on a VPS server unless you have memory and cpu cycles to spare (and why are you reading this if that’s the case?).
    To disable ClamAV:

    1. Go to WHM » Home » cPanel » Manage Plugins » Uncheck clamavconnector and save.
  5. Disable Catch All E-Mail Delivery : If you are getting a lot of spam for non-existent e-mail addresses, disable their delivery.
    To disable catchall email delivery:

    1. Go to WHM » Server Configuration » Tweak Settings » Mail » Initial default/catch-all forwarder destination » Change to :fail:
  6. Tweak Apache: Apache needs some tweaking to reduce memory usage.
    Recommended settings updates for this:

    1. WHM » Home » Service Configuration » Apache Configuration » Global Configuration » Change the following settings:
      MinSpareServers 2
      MaxSpareServers 5
      StartServers 3
      Keep-Alive Off
    2. Save the values and rebuild apache (rebuild should start automatically).
  1. Optimize PHP – Main advice here would be to reduce memory_limit in php.ini file. This wasn’t an issue for our server, but might be worth checking if everything before now hasn’t helped enough.
    To reduce memory_limit:

    1. From WHM >> Service Configuration >> PHP Configuration Editor >> Core >> memory_limit

 

After making all the tweaks, reboot your VPS, check memory usage. Hope it has freed up enough to get your server stabilized!

To check memory usage:

From SSH àfree –m
From WHM à system health à server status
I think a dollar for every MB you freed up is reasonable, but am open to cookies, toys, compliments, trolling and comments as well.
Sources:
http://syslint.com/syslint/how-to-reduce-memory-consumption-on-a-cpanel-vps/

http://blog.triantech.com/memory-drain-issues-on-mysql-5-6/

http://dev.mysql.com/doc/refman/5.1/en/miscellaneous-optimization-tips.html

http://www.vpshostingreviews.net/whm-tutorial-tweaks-for-vps-performance-optimization/

https://my.bluehost.com/cgi/help/2521