服扛不住,一查mysql的锅,重启就好了,但是不能每天都上去重启,毕竟不是专业搞这个的,没时间盯,然后就搜到这么一篇无人值守,弄了,收藏之、分享之。
首先新建一个脚本文件,命名为:checkmysql.sh 内容如下:
#!/bin/bash
#/usr/bin/nmap localhost | grep 3306
#lsof -i:3306
MYSQLPORT=`netstat -na|grep "LISTEN"|grep "3306"|awk -F[:" "]+ '{print $5}'`
function checkMysqlStatus(){
/usr/bin/mysql -uroot -p11111 --connect_timeout=5 -e "show databases;" &>/dev/null 2>&1
if [ $? -ne 0 ]
then
restartMysqlService
if [ "$MYSQLPORT" == "3306" ];then
echo "mysql restart successful......"
else
echo "mysql restart failure......"
echo "Server: $MYSQLIP mysql is down, please try to restart mysql by manual!" > /var/log/mysqlerr
mail -s "WARN! server: $MYSQLIP mysql is down" xxx@xxx.com < /var/log/mysqlerr
fi
else
echo "mysql is running..."
fi
}
function restartMysqlService(){
echo "try to restart the mysql service......"
/bin/ps aux |grep mysql |grep -v grep | awk '{print $2}' | xargs kill -9
service mysql start
}
if [ "$MYSQLPORT" == "3306" ]
then
checkMysqlStatus
else
restartMysqlService
fi
PS:以上xxx@xxx.com部分替换成自己的邮箱地址,如果MySQL重启失败可以收到提醒邮件。
把该文件上传到服务器上的某一个文件夹,我的对应位置是/root/lnmp1.4/tools/checkmysql.sh
之所以上传这个位置是因为这里还存放了lnmp作者写的其他几个维护脚本,以后找起来也方便
上传完毕后,打开服务器上的/etc/crontab 定时任务文件,在最底下加一行并保存,我设置了每20分检查一次:
*/20 * * * * root /bin/bash /root/lnmp1.4/tools/checkmysql.sh
然后添加这个脚本文件的执行权限,命令
chmod +x /root/lnmp1.4/tools/checkmysql.sh
最后重启一下定时任务:
service crond restart
至此,大功告成。但是这个并不是解决问题的办法,如果服上的东西多,总不能一直重启吧。那就趁着有活动去换配置。
然后,别着急,作者又整了一段,继续往下:
脚本内容已更换:
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
echo "At time: `date` :MySQL is stop .">> /home/wwwlogs/mysql_messages
service mysql start
#echo "At time: `date` :MySQL server is stop."
else
echo "MySQL server is running ."
fi
另外,我的内容源自这里:https://blog.brain1981.com/2012.html
正好拿来借鉴一下,谢谢
正好拿来借鉴一下,谢谢
暂时还没遇到mysql崩溃,好像mysql状态还不错就是缓存查询率只有七十多上不去
这个好像有问题,让我再研究研究