Old Bird takes you to develop professional standard MySQL startup script

  • 2021-12-12 10:13:29
  • OfStack

Every qualified Linux operation and maintenance personnel should be skilled or proficient in Shell scripting, because Shell scripting language is almost the simplest language among all programming languages. If Shell script fails, it means that the road of operation and maintenance may end before it starts. -Teacher old boys


#!/bin/bash
# chkconfig: 2345 64 36 # Configure system self-startup 
# description: A very fast and reliable SQL database engine.
##############################################################
# File Name: mysqld
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
# Created Time : 2018-06-05 08:58:19
##############################################################
# Introducing system function library 
. /etc/init.d/functions

# Basic path definition 
basedir='/application/mysql'
bindir='/application/mysql/bin'
lockdir='/var/lock/subsys'                    
lock_file_path="$lockdir/mysql"
mysqld_pid_file_path='$basedir/data/`uname -n`.pid'

# Success prompt function 
log_success_msg(){
  #action Is a special hint function, $@ Is for all parameters. 
  action "SUCCESS! $@" /bin/true
}
# Failure prompt function 
log_failure_msg(){
  action "ERROR! $@" /bin/false
 }
 
#mysql Startup function 
start(){
  echo $"Starting MySQL"
  # Test mysqld_safe Is it executable 
  if test -x $bindir/mysqld_safe
  then
    # Background execution startup mysql Command 
    $bindir/mysqld_safe &>/dev/null &
    # Get the return value 
    retval=$?
    # Determine whether the return value is 0
    if [ $retval -eq 0 ]
    then
      # Call the success prompt function. 
      log_success_msg "mysql Startup"
      if test -w "$lockdir" # Determines whether the lock directory is writable. 
      then
        touch "$lock_file_path" # Create a lock file. 
      fi
      return $retval # Giving a return value is a professional performance. 
    else
      log_failure_msg "MySQL Startup" # Call failed function prompt. 
      return $retval
    fi
  else
    log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
  fi
}
# Stop MySQL Function. 
stop(){
  # Judge mysql pid file Whether the size is 0 . 
  if test -s "$mysqld_pid_file_path"
  then
    # Read pidfile
    mysqld_pid=`cat "$mysqld_pid_file_path"`
    # Judge mysql pid Whether the corresponding process exists. 
    if (kill -0 $mysqld_pid 2>/dev/null)
    then
      echo $"Shutting down MySQL"
      kill $mysqld_pid # Stop MySQL Orders. 
      retval=$?
      if [ $retval -eq 0 ]
      then
        log_success_msg "MySQL Stop" # Call the stop success function. 
        if test -f "$lock_file_path"
        then
          rm -f "$lock_file_path" # Delete the lock file. 
        fi
        return $retval
      else
        log_failure_msg "MySQL Stop."
        return $retval
      fi
    else
      log_failure_msg "MySQL server process mysqld_pid is not running!"
      rm "$mysqld_pid_file_path"
    fi 
  else
    log_failure_msg "MySQL server PID file is null or not exist!"
  fi
}
# Receiving parameter transmission judgment and executing corresponding functions. 
case "$1" in
  start)
    start
    retval=$?
    ;;
  stop)
    stop
    retval=$?
    ;;
  restart)
    stop
    sleep 2 # It's important here to rest 1 Under. 
    start
    retval=$?
    ;;
  *)
    echo $"Usage:$0 {start|stop|restart}"
    exit 2
esac
exit $retval  # After executing the script, it is more professional to have a return value. 


Related articles: