sysbench scripts (6)

    銜接上文,以下是test文件夾下的腳本。

    6、server_op.sh腳本

#! /bin/sh

###########################################################
# Copyright (c) 2012, Heng.Wang. All rights reserved.
#
# This program is benifit for sysbench oltp test.
###########################################################

# set -x

# Get the key value of input arguments format like '--args=value'.
get_key_value()
{
    echo "$1" | sed 's/^--[a-zA-Z_-]*=//'     
}

# Usage will be helpful when you need to input the valid arguments.
usage()
{
cat <<EOF
Usage: $0 [configure-options]
  -?, --help                       Show this help message.
  --mysqldir=<>                    Set the mysql directory
  --defaults-file=<>               Set the configure file for mysql
  --host=<>                        Set the host name.
  --port=<>                        Set the port number.
  --user=<>                        Set the user name.
  --password=<>                    Set the password.
  --socket=<>                      Set the socket file.
  -s,--start                       Start the mysql server.
  -d,--shutdown                    Shutdown the mysql server.
  
Note: this script is intended for internal use by developers.

EOF
}

# Print the default value of the arguments of the script.
print_default()
{
cat <<EOF
  The default value of the variables:
  
  mysqldir          $MYSQLDIR
  defaults-file	    $CONFIG
  host              $HOST
  port              $PORT
  user              $USER
  password          $PASSWORD
  socket            $SOCKET
  start             FALSE
  shutdown          FALSE
  
EOF
}

# Parse the input arguments and get the value of the input argument.
parse_options()
{
  while test $# -gt 0
  do
    case "$1" in    
    --mysqldir=*)
      MYSQLDIR=`get_key_value "$1"`;;
    --defaults-file=*)
      CONFIG=`get_key_value "$1"`;;
    --host=*)
      HOST=`get_key_value "$1"`;;
    --port=*)
      PORT=`get_key_value "$1"`;;
    --user=*)
      USER=`get_key_value "$1"`;;
    --password=*)
      PASSWORD=`get_key_value "$1"`;;
    --socket=*)
      SOCKET=`get_key_value "$1"`;;
    -s | --start)
      START=1;;
    -d | --shutdown)
      SHUTDOWN=1;;
    -? | --help)
      usage
      print_default
      exit 0;;
    *)
      echo "Unknown option '$1'"
      exit 1;;
    esac
    shift
  done
}

#Shutdown the mysql server after the sysbench test.
shutdown_mysqld()
{
  cd $MYSQLDIR
  ./bin/mysqladmin --host=$HOST --port=$PORT --user=$USER --password=$PASSWORD --socket=$SOCKET shutdown > /dev/null
  if [ $? -ne 0 ]
  then
    echo "Exit with error when shutdown the mysql server procedure!"
    exit -1
  fi
  sleep 10
}

#Start the mysql server if the server is not running.
start_mysqld()
{
  cd $MYSQLDIR
  ./bin/mysqladmin  --host=$HOST --port=$PORT --user=$USER --password=$PASSWORD --socket=$SOCKET status > /dev/null
  if [ $? -ne 0 ]
  then
    ./bin/mysqld_safe --defaults-file=$CONFIG > /dev/null &
    if [ $? -ne 0 ]
    then
      echo "Exit with error when start the mysql server procedure!"
      exit -1
    fi
    sleep 30
  fi
}

#############################################################
# Define the variables the script used for executing.
MYSQLDIR=/opt/Percona-Server
CONFIG=/opt/Percona-Server/etc/my.cnf
HOST=localhost
PORT=3306
USER=root
PASSWORD=
SOCKET=/tmp/mysql.sock
START=0
SHUTDOWN=0

# Call the parse_options function to parse the input arguments.
parse_options "$@"

if [ $START -eq 0 ] &&  [ $SHUTDOWN -eq 0 ]
then
  echo "Please be ensure you operation for mysql server."
  exit 1
fi

if [ $START -eq 1  ]
then
  #Start the mysql server
  start_mysqld
  echo "The server is successfully started!"
fi
 
if [ $SHUTDOWN -eq 1 ]
then
  #Shutdown the mysql server
  shutdown_mysqld
  echo "The server is successfully shutdown!"
fi

exit 0


發佈了34 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章