There are a couple of ways going about it. First however, you’ll need to see why MySQL is crashing in order to be fix the issue rather than just cover it up.
Most probably the issue comes out of High Load caused by something, being either a service like MySQL,Apache or Nginx or just too much traffic. When this happens and your service runs out of memory it kills random services to keep itself from crashing completely. How to be able to pinpoint this?
Firstly, you’ll need to check why the service was killed. To do so, you’ll have to check your /var/log/messages for the kill or oom command from your server. Run the following
grep -i kill /var/log/messages
grep -i oom /var/log/messages
This will show you why and if your server killed a service and why.
If there is no output, most probably the service itself, in this case MySQL crashed for a different reason. We’ll get on to that a bit later.
Now, let’s assume you did get some output stating your server went out of memory and had to kill a service to not crash. In this case, you’ll need to check if your service was taking too much memory and why.
This will most probably be related to traffic. This can be checked in the logs of your Web Service and Mail Service. Depending on your service the logs will be in a different location. You’ll need to google on for that.
Now, let’s continue if you didn’t get an output from the above commands. It will mean the service, in this case MySQL broke down because of a different reason.
If it was MySQL the problem, then it will be displayed in the error log of MySQL. You can find where this log is stored by opening the file
There the path to your log file would be presented. In the error log file, the reason behind the crash will be displayed. You’ll need to act upon that.
Alternatively, your server just doesn’t have enough RAM and you’ll need to upgrade it to keep it from crashing again.
Lastly, you’ll need to create a script which checks if MySQL is actually down and start it up again. This can be executed every minute by a cron job on your server.