{"id":329,"date":"2013-02-12T16:35:35","date_gmt":"2013-02-12T16:35:35","guid":{"rendered":"http:\/\/invisiblezero.net\/?p=329"},"modified":"2013-02-12T16:35:35","modified_gmt":"2013-02-12T16:35:35","slug":"linux-auto-rescue-mysql-server-when-it-stop-working","status":"publish","type":"post","link":"http:\/\/ndthanh.com\/linux-auto-rescue-mysql-server-when-it-stop-working\/","title":{"rendered":"Linux – auto rescue mysql server when it stop working"},"content":{"rendered":"
if you are a network administrator or you have a private web server , i think you will at least faced unexpected shutdown issue of mysql server. rumors said it happened due to lacking of system resources (RAM is the most common case). it seems that Linux will kill a process that exceeded its RAM limit – depend on system resources. you know, i have a workaround for this issue \ud83d\ude42
\n
\nIn order for our websites to work properly again, we’ll need to restart mysql server. what you need to do is to setup a cron job to monitor server and restart mysql server if mysql is down. firstly, create rescuemysql.sh and save it<\/p>\n
\n\n#auto process script\n\n#check for process instance and trigger restart if no instance found\n\n#author: Atheotsky\n\n#set process name\n\nPNAME="mysqld"\n\n#count processes function\n\ncountProcess()\n\n{\n\n#set total of given process to global PTOTAL\n\nPTOTAL=$(ps -e | grep "$1" | wc -l)\n\n}\n\n#invoke function with param PNAME\n\ncountProcess $PNAME\n\n#conditional command\n\nif [ "$PTOTAL" -eq "0" ]\n\nthen\n\n#need to update PATH so cron can locate execute path for program\n\nPATH=$PATH:\/usr\/local\/sbin:\/usr\/local\/bin:\/usr\/sbin:\/usr\/bin:\/sbin:\/bin\n\n#\/etc\/init.d\/mysql restart\n\nservice mysql restart\n\nfi\n<\/pre>\nnext step is to setup your cron job, i add this script to root crontab because “service mysql restart” requires root privilege.<\/p>\n
execute this command to open root crontab editor<\/p>\n
\nsudo crontab -e -u root\n<\/pre>\nadd following line to the end of root cron file<\/p>\n
\n# rescue mysqld\n\n*\/1 * * * * \/bin\/bash \/path\/to\/your\/script.sh\n<\/pre>\nlook a bit confusing ? take a look at this<\/p>\n
\n\n*\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0 *\u00a0 command to be executed\n \u252c\u00a0\u00a0\u00a0 \u252c\u00a0\u00a0\u00a0 \u252c\u00a0\u00a0\u00a0 \u252c\u00a0\u00a0\u00a0 \u252c\n \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\n \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\n \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2514\u2500\u2500\u2500\u2500\u2500 day of week (0 - 7) (0 or 7 are Sunday, or use names)\n \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 month (1 - 12)\n \u2502\u00a0\u00a0\u00a0 \u2502\u00a0\u00a0\u00a0 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 day of month (1 - 31)\n \u2502\u00a0\u00a0\u00a0 \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 hour (0 - 23)\n \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 min (0 - 59)\n\n<\/pre>\nAt min position, we have *\/1, this means your script will run every minute – don’t worry, our script is very light and only restart mysql server if it’s down.<\/p>\n
ok, your server can rescue mysql server by itself from now on. cheer!<\/p>\n","protected":false},"excerpt":{"rendered":"
if you are a network administrator or you have a private web server , i think you will at least faced unexpected shutdown issue of mysql server. rumors said it happened due to lacking of system resources (RAM is the most common case). it seems that Linux will kill a process that exceeded its RAM…<\/p>\n