MySQL 啓動腳本

1.1 部署路徑

MySQL程序部署路徑:/data/apps/mysql

MySQL實例部署路徑:/data/mysql/3306/

MySQL實例PID路徑:/data/mysql/3306/mysql.pid

MySQL實例sock路徑:/data/mysql/3306/mysql.sock

MySQL配置文件路徑:/data/mysql/3306/my.cnf

1.2 腳本內容

#!/bin/bash
#
# Load os variables
source /etc/bashrc
source /etc/profile

# Define variables
RETVAL=0
Port=3306
User=root
Pass=chenliang
Pid=/data/mysql/3306/mysql.pid
Sock=/data/mysql/3306/mysql.sock
My=/data/mysql/3306/my.cnf
Path=/data/apps/mysql/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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章