#!/bin/bash
#By:sadoc.blog.51cto.com
. /etc/rc.d/init.d/functions
MYUSER=root
MYPASS="123456"
MYSOCK=/var/lib/mysql/mysql.sock
MY_START="systemctl start mysqld.service"
LOG_PATH=/tmp/mysql
MY_PATH=/usr/bin/mysql
DAYS=`date +%F`
SECOND=`date +%Y/%m/%d-%H:%M:%S`
STARTING="${LOG_PATH}/start-$DAYS.log"
[ -d $LOG_PATH ] || mkdir -p $LOG_PATH
MY_CMD="$MY_PATH -u$MYUSER -p$MYPASS -S $MYSOCK"
$MY_CMD -e "select version ();" >/dev/null 2>&1
if [ $? -eq 0 ];then
action "$SECOND----Mysql" /bin/true >>$LOG_PATH/my_status.log
exit 0
else
$MY_START >>$STARTING
sleep 5;
$MY_CMD -e "select version ();" >/dev/null 2>&1
if [ $? -ne 0 ];then
for down in `seq 5`
do
systemctl stop mysqld.service >/dev/null 2>&1
[ $? -ne 0 ] && break;
sleep 2;
done
$MY_START >>$STARTING
fi
$MY_CMD -e "select version ();" >/dev/null 2>&1 && Status="restarted" || Status="unkown"
echo "$SECOND------Mysql status is $Status" >>$LOG_PATH/my_status.log
fi