1. 程式人生 > >利用Shell語言開發MySQL啟動指令碼

利用Shell語言開發MySQL啟動指令碼

#!/bin/bash
#
# Define variables
RETVAL=0
Port=3306
User=root
Pass=chenliang
Pid=/data/3306/mysql.pid
Sock=/data/3306/mysql.sock
My=/data/3306/my.cnf
Path=/apps/mysql-5.7.20/bin

# Determine the user to execute
if [ $UID -ne $RETVAL ];then
   echo "Must be root to run scripts"
   exit 1
fi

# Load the local functions
[ -f /etc/init.d/functions ] && source /etc/init.d/functions

# Define functions
start(){
       if [ ! -f "$Pid" ];then
          $Path/mysqld_safe --defaults-file=$My >/dev/null 2>&1 &
          RETVAL=$?
          if [ $RETVAL -eq 0 ];then
             action "Start MySQL [3306]" /bin/true
            else
             action "Start MySQL [3306]" /bin/false
          fi
         else
          echo "MySQL 3306 is running"
          exit 1
      fi
      return $RETVAL
}

stop(){
       if [ -f "$Pid" ];then
          $Path/mysqladmin -u$User -p$Pass -S $Sock shutdown >/dev/null 2>&1
          RETVAL=$?
          if [ $RETVAL -eq 0 ];then
             action "Stop MySQL[3306]" /bin/true
            else
             action "Stop MySQL[3306]" /bin/false
          fi
        else
          echo "MySQL [3306] is not running"
          exit 1
       fi
       return $RETVAL
}

status(){
       if [ -f "$Pid" ];then
           echo "MySQL [3306] is running"
          else
           echo "MySQL [3306] is not running"
       fi
       return $RETVAL
}

# Case call functions
case "$1" in
 start)
       start
       RETVAL=$?
       ;;
 stop)
       stop
       RETVAL=$?
       ;;
 restart)
       stop
       sleep 5
       start
       RETVAL=$?
       ;;
 status)
       status
       RETVAL=$?
       ;;
 *)
       echo "USAGE:$0{start|stop|restart|status}"
       exit 1
esac

# Scripts return values
exit $RETVAL