Linux – Auto-restart thesql when it dies

linuxmemoryMySQL

I have a rackspace server that I have been renting to run my personal projects upon. Since I am cheap, it has 256Mb of RAM and honestly can't handle alot. Every once in a while, when there is a sharp uptick in traffic, the server decides to start killing processes and it seems that mysqld is a popular one for it to kill. I try to visit my site and am greeted with the message that there was an error establishing the database connection. Inspection of the logs reveals that mysqld was killed due to lack of memory.

Since I am still as poor as I was yesterday and don't want to upgrade my rackspace VM's RAM, is there a way I can tell it to automagically restart mysqld when it dies?

I have a thought to use something like crontab, but alas, I don't know exactly what to do there either. I guess I am product of the "Linux on your desktop" generation since I can do most things on my desktop and laptop (which run Linux almost exclusively), but still lack a lot of server administration skills for Linux.

The server runs CentOS 6.3

Best Answer

This is not a clean solution, it would obviously be better to avoid the problem in the first place. Anyway, I am not sure how CentOS manages services but I think it uses service. If so, you can check if the mysql service is running with

/sbin/service mysql status

This command will exit successfully if mysql is running and return a non 0 exit status if i is not. You can therefore start the service if it is not running with this command:

/sbin/service mysql status || service mysql start

You can add this line to /etc/crontab to launch thes command every minute:

* * * * * /sbin/service mysql status || service mysql start
Related Question