safe_mysqld.sh
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:9k
源码类别:

模拟服务器

开发平台:

C/C++

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. #
  5. # scripts to start the MySQL daemon and restart it if it dies unexpectedly
  6. #
  7. # This should be executed in the MySQL base directory if you are using a
  8. # binary installation that has other paths than you are using.
  9. #
  10. # mysql.server works by first doing a cd to the base directory and from there
  11. # executing safe_mysqld
  12. trap '' 1 2 3 15 # we shouldn't let anyone kill us
  13. umask 007
  14. defaults=
  15. case "$1" in
  16.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  17.       defaults="$1"; shift
  18.       ;;
  19. esac
  20. parse_arguments() {
  21.   # We only need to pass arguments through to the server if we don't
  22.   # handle them here.  So, we collect unrecognized options (passed on
  23.   # the command line) into the args variable.
  24.   pick_args=$1; shift
  25.   for arg do
  26.     case "$arg" in
  27.       # these get passed explicitly to mysqld
  28.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  29.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  30.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  31.       --user=*)    user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
  32.       # these two might have been set in a [safe_mysqld] section of my.cnf
  33.       # they get passed via environment variables to safe_mysqld
  34.       --socket=*)  MYSQL_UNIX_PORT=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  35.       --port=*)    MYSQL_TCP_PORT=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  36.       # safe_mysqld-specific options
  37.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  38.       --err-log=*) err_log=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  39.       # QQ The --open-files should be removed
  40.       --open-files=*) open_files=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  41.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  42.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  43.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; export TZ; ;;
  44.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--[^=]*=;;"` ;;
  45.       --mysqld-version=*)
  46. tmp=`echo "$arg" | sed -e "s;--[^=]*=;;"`
  47. if test -n "$tmp"
  48. then
  49.   MYSQLD="mysqld-$tmp"
  50. else
  51.   MYSQLD="mysqld"
  52. fi
  53. ;;
  54.       *)
  55.         if test $pick_args -eq 1
  56.         then
  57.           # This sed command makes sure that any special chars are quoted,
  58.           # so the arg gets passed exactly to the server.
  59.           args="$args "`echo "$arg" | sed -e 's,([^a-zA-Z0-9_.-]),\\1,g'`
  60.         fi
  61.         ;;
  62.     esac
  63.   done
  64. }
  65. MY_PWD=`pwd`
  66. # Check if we are starting this relative (for the binary release)
  67. if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a 
  68.  -x ./bin/mysqld
  69. then
  70.   MY_BASEDIR_VERSION=$MY_PWD # Where bin, share and data are
  71.   ledir=$MY_BASEDIR_VERSION/bin # Where mysqld is
  72.   DATADIR=$MY_BASEDIR_VERSION/data
  73.   if test -z "$defaults"
  74.   then
  75.     defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  76.   fi
  77. # Check if this is a 'moved install directory'
  78. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a 
  79.  -x ./libexec/mysqld
  80. then
  81.   MY_BASEDIR_VERSION=$MY_PWD # Where libexec, share and var are
  82.   ledir=$MY_BASEDIR_VERSION/libexec # Where mysqld is
  83.   DATADIR=$MY_BASEDIR_VERSION/var
  84. else
  85.   MY_BASEDIR_VERSION=@prefix@
  86.   DATADIR=@localstatedir@
  87.   ledir=@libexecdir@
  88. fi
  89. MYSQL_UNIX_PORT=${MYSQL_UNIX_PORT:-@MYSQL_UNIX_ADDR@}
  90. MYSQL_TCP_PORT=${MYSQL_TCP_PORT:-@MYSQL_TCP_PORT@}
  91. user=@MYSQLD_USER@
  92. # Use the mysqld-max binary by default if the user doesn't specify a binary
  93. if test -x $ledir/mysqld-max
  94. then
  95.   MYSQLD=mysqld-max
  96. else
  97.   MYSQLD=mysqld
  98. fi
  99. # these rely on $DATADIR by default, so we'll set them later on
  100. pid_file=
  101. err_log=
  102. SET_USER=0
  103. # Get first arguments from the my.cnf file, groups [mysqld] and [safe_mysqld]
  104. # and then merge with the command line arguments
  105. if test -x ./bin/my_print_defaults
  106. then
  107.   print_defaults="./bin/my_print_defaults"
  108. elif test -x @bindir@/my_print_defaults
  109. then
  110.   print_defaults="@bindir@/my_print_defaults"
  111. elif test -x @bindir@/mysql_print_defaults
  112. then
  113.   print_defaults="@bindir@/mysql_print_defaults"
  114. else
  115.   print_defaults="my_print_defaults"
  116. fi
  117. args=
  118. parse_arguments 0 `$print_defaults $defaults mysqld server safe_mysqld`
  119. parse_arguments 1 "$@"
  120. if test ! -x $ledir/$MYSQLD
  121. then
  122.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  123.   echo "Please do a cd to the mysql installation directory and restart"
  124.   echo "this script from there as follows:"
  125.   echo "./bin/safe_mysqld".
  126.   exit 1
  127. fi
  128. if test -z "$pid_file"
  129. then
  130.   pid_file=$DATADIR/`@HOSTNAME@`.pid
  131. else
  132.   case "$pid_file" in
  133.     /* ) ;;
  134.     * )  pid_file="$DATADIR/$pid_file" ;;
  135.   esac
  136. fi
  137. test -z "$err_log"  && err_log=$DATADIR/`@HOSTNAME@`.err
  138. export MYSQL_UNIX_PORT
  139. export MYSQL_TCP_PORT
  140. NOHUP_NICENESS="nohup"
  141. if test -w /
  142. then
  143.   NOHUP_NICENESS=`nohup nice 2>&1`
  144.   if test $? -eq 0 && test x"$NOHUP_NICENESS" != x0 && nice --1 echo foo > /dev/null 2>&1
  145.   then
  146.     if test $NOHUP_NICENESS -gt 0
  147.     then
  148.       NOHUP_NICENESS="nice --$NOHUP_NICENESS nohup"
  149.     else
  150.       NOHUP_NICENESS="nice -$NOHUP_NICENESS nohup"
  151.     fi
  152.   else
  153.     NOHUP_NICENESS="nohup"
  154.   fi
  155. fi
  156. USER_OPTION=""
  157. if test -w /
  158. then
  159.   if test "$user" != "root" -o $SET_USER = 1
  160.   then
  161.     USER_OPTION="--user=$user"
  162.   fi
  163.   # If we are root, change the err log to the right user.
  164.   touch $err_log; chown $user $err_log
  165.   if test -n "$open_files"
  166.   then
  167.     ulimit -n $open_files
  168.   fi
  169.   if test -n "$core_file_size"
  170.   then
  171.     ulimit -c $core_file_size
  172.   fi
  173. fi
  174. #
  175. # If there exists an old pid file, check if the daemon is already running
  176. # Note: The switches to 'ps' may depend on your operating system
  177. if test -f $pid_file
  178. then
  179.   PID=`cat $pid_file`
  180.   if @CHECK_PID@
  181.   then
  182.     if @FIND_PROC@
  183.     then    # The pid contains a mysqld process
  184.       echo "A mysqld process already exists"
  185.       echo "A mysqld process already exists at " `date` >> $err_log
  186.       exit 1
  187.     fi
  188.   fi
  189.   rm -f $pid_file
  190.   if test -f $pid_file
  191.   then
  192.     echo "Fatal error: Can't remove the pid file: $pid_file"
  193.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  194.     echo "Please remove it manually and start $0 again"
  195.     echo "mysqld daemon not started"
  196.     exit 1
  197.   fi
  198. fi
  199. #
  200. # Uncomment the following lines if you want all tables to be automaticly
  201. # checked and repaired at start
  202. #
  203. # echo "Checking tables in $DATADIR"
  204. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check -O key_buffer=64M -O sort_buffer=64M $DATADIR/*/*.MYI
  205. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force -O sort_buffer=64M $DATADIR/*/*.ISM
  206. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  207. # Does this work on all systems?
  208. #if type ulimit | grep "shell builtin" > /dev/null
  209. #then
  210. #  ulimit -n 256 > /dev/null 2>&1 # Fix for BSD and FreeBSD systems
  211. #fi
  212. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  213. while true
  214. do
  215.   rm -f $MYSQL_UNIX_PORT $pid_file # Some extra safety
  216.   if test -z "$args"
  217.   then
  218.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file @MYSQLD_DEFAULT_SWITCHES@ >> $err_log 2>&1
  219.   else
  220.     eval "$NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file @MYSQLD_DEFAULT_SWITCHES@ $args >> $err_log 2>&1"
  221.   fi
  222.   if test ! -f $pid_file # This is removed if normal shutdown
  223.   then
  224.     break
  225.   fi
  226.   if @IS_LINUX@
  227.   then
  228.     # Test if one process was hanging.
  229.     # This is only a fix for Linux (running as base 3 mysqld processes)
  230.     # but should work for the rest of the servers.
  231.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  232.     # kill -9 is used or the process won't react on the kill.
  233.     numofproces=`ps xa | grep -v "grep" | grep -c $ledir/$MYSQLD`
  234.     echo -e "nNumber of processes running now: $numofproces" | tee -a $err_log
  235.     I=1
  236.     while test "$I" -le "$numofproces"
  237.     do 
  238.       PROC=`ps xa | grep $ledir/$MYSQLD | grep -v "grep" | sed -n '$p'` 
  239. for T in $PROC
  240. do
  241.   break
  242. done
  243. #    echo "TEST $I - $T **"
  244. if kill -9 $T
  245. then
  246.   echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  247. else 
  248.   break
  249. fi
  250. I=`expr $I + 1`
  251.     done
  252.   fi
  253.   echo "`date +'%y%m%d %H:%M:%S'`  mysqld restarted" | tee -a $err_log
  254. done
  255. echo "`date +'%y%m%d %H:%M:%S'`  mysqld ended" | tee -a $err_log
  256. echo "" | tee -a $err_log