Every server has a limited amount of memory, also known as RAM. The amount of memory your server has is determined by the type of server you're paying for at your server provider.
When a server runs out of memory, the server must kill processes so the server can keep running. This is the most common cause of MySQL crashing: the server ran out of memory and had to kill MySQL.
The best solution to running out of memory is to upgrade to a server with more memory.
The mount of memory your server uses is related to many factors, including
The operating system and core services use around 150MB of memory.
Each simple app (small codebase, one database, low traffic) uses about 50MB of memory.
So, for example, with 10 apps your server would normally use about 650MB of memory. That is,
150MB (OS memory usage) + 10 (number apps) * 50MB (app memory usage) = 650MB
If you need to lower memory usage, you can do a few things to save memory.
Each new version of PHP includes optimizations that reduce that amount of memory used internally by PHP to execute your scripts.
PHP 7.1 uses less memory than PHP 7.0, which uses less memory than PHP 5.6, which uses less memory than PHP 5.5, which uses less memory than PHP 5.4.
You should use the newest version of PHP that your app supports.
Disable any WordPress plugins that you don't need.
Some WordPress plugins increase memory usage significantly. By disabling plugins you don't need, you can save memory.
If you've added third-party PHP extensions like New Relic, these will increase memory usage.
If you need these extensions, you should not disable them. However, if you've added them and aren't using them anymore, you should disable them.
PHP 5.5, 5.6, 7.0, 7.1, and 7.2 have a built-in opcache that is enabled by default.
The entire purpose of PHP's opcache is to speed up your scripts and save CPU by storing compiled PHP scripts in memory.
If you'd rather to have slower apps and increased CPU usage to save memory, you can disable the opcache.
To disable PHP's opcache, you can SSH in as root and edit the following files:
/etc/php5.5-sp/conf.d/opcache.ini /etc/php5.6-sp/conf.d/opcache.ini /etc/php7.0-sp/conf.d/opcache.ini /etc/php7.1-sp/conf.d/opcache.ini /etc/php7.2-sp/conf.d/opcache.ini
add the following line to the end of each of these files:
Finally, restart each PHP version by running:
sudo service php5.5-fpm-sp restart sudo service php5.6-fpm-sp restart sudo service php7.0-fpm-sp restart sudo service php7.1-fpm-sp restart sudo service php7.2-fpm-sp restart