home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 May / INTERNET103.ISO / pc / software / windows / building / mysql / data1.cab / Development / scripts / safe_mysqld < prev   
Encoding:
Text File  |  2003-01-22  |  8.4 KB  |  284 lines

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