1,根据mysql端口3306来判断服务启动正不正常
netstat -lnt|grep 3306|awk -F '[: ]+ ' '{print $5}' //获取端口号比较来判断#!/bin/sh
PortNum=`netstat -lnt|grep 3306|awk -F '[: ]+ ' '{print $5}'`
if [ "$PortNum" == 3306 ];then
echo "db is running"
else
/data/3306/mysql restart
fi
netstat -lnt|grep 3306|wc -l //结果等于1,数据库运行正常;结果等于0,数据库运行不正常
#!/bin/shPortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -eq 1 ];then
echo "db is running"
else
/data/3306/mysql restart
fi
2,如果mysql端口和进程同时存在,才认定mysql服务正常
ps -ef|grep mysql|grep -v grep|wc -l //把进程转换为行数,方便做判断:正常启动进程数为2#!/bin/sh
PortNum=`netstat -lnt|grep 3306|wc -l`
mysqlProcessNum=`ps -ef|grep mysqld|grep -v grep|wc -l`
//if [ $PortNum -eq 1 ] && [ $mysqlProcessNum -eq 2 ];then
if [ $PortNum -eq 1 -a $mysqlProcessNum -eq 2 ];then
echo "db is running"
else
/data/3306/mysql start
fi
#!/bin/sh
mysqlProcessNum=`ps -ef|grep mysqld|grep -v grep|wc -l`
PortNum=`netstat -lnt|grep 3306|wc -l`
MysqlStartUp="/data/3306/mysql"
LogPath="/tmp/mysql.log"
if [ $PortNum -eq 1 -a $mysqlProcessNum -eq 2 ];then
echo "db is running"
else
$MysqlStartUp start >$LogPath
sleep 10;
mysqlProcessNum=`ps -ef|grep mysqld|grep -v grep|wc -l`
PortNum=`netstat -lnt|grep 3306|wc -l`
if [ $PortNum -ne 1 ] && [ $mysqlProcessNum -ne 2 ];then
while true
do
killall mysqld >/dev/null 2>&1
[ $? -ne 0 ] && break
sleep 1
done
$MysqlStartUp start >>$LogPath && status="successfully"||status="failure"
[ $PortNum -eq 0 ] && $MysqlStartUp start >>/tmp/mysql.log
[ $? -eq 0 ] && echo "mysql is started"
fi
mail -s "mysql startup status is $status" 441009395@qq.com
fi
模拟登陆判断:mysql -uroot -p'123456' -S /data/3306/mysql.sock -e "select version();" >/dev/null 2>&1#!/bin/sh
mysqlstatus=`mysql -uroot -p'123456' -S /data/3306/mysql.sock -e "select version();" >/dev/null 2>&1`
if [ $? -eq 0 ];then
echo "db is running"
else
/data/3306/mysql restart
fi