Hi all! i want to share this usefull script to others.
I wrote it when we had a buggy web-site engine,that causes our mysql server to use 99% of CPU.
We couldnot find problem in website.
While we were developing new version of site i wrote a simple script whos goal was restart sql server automaticly.
Tested on freebsd.
Will not work if mysql compiled with LINUXTHREADS=yes key. because "ps" will return several lines. but only one line is expected.
Im starting this script by another script that liyng in rc.d directory.
# cat /usr/local/etc/rc.d/mysql_restarter.sh
Code:
#!/bin/sh
echo "Starting PROTECTION SCRIPT by makkintosh"
nohup /root/mysqlctl.php &
# cat /root/mysqlctl.php
Code:
#!/bin/sh
while [ -f /bin/sh ]
do
starttime=`ps -clax | grep mysqld | awk ' $1 { print $12 }' | awk -F":" ' $2 { print ($1*60+$2)*100 }'`
sleep 2
endtime=`ps -clax | grep mysqld | awk ' $1 { print $12 }' | awk -F":" ' $2 { print ($1*60+$2)*100 }'`
echo $endtime;
inc=`expr $endtime - $starttime`
if
test $inc -ge 124
then
/usr/local/etc/rc.d/mysql-server.sh stop
sleep 1
date >> /var/log/mysql/restarts
/usr/local/etc/rc.d/mysql-server.sh start >> /var/log/mysql/restarts
echo "mysql restarted $inc" >> /var/log/mysql/restarts
else
fi
sleep 5
done
if u need some more comments - u may ask
i hope this will help somebody
in addition there is a script called "mysql-checker.pl"
it is avalible at sf.net
it sends email to u when test queryes cannot be executed.
Good luck
P.S.
if any improvements can be done - please tell me :-)