home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2004 July / INTERNET119.ISO / pc / software / windows / building / mysql / data1.cab / Development / scripts / mysqld_safe < prev    next >
Encoding:
Text File  |  2004-02-11  |  10.5 KB  |  353 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 mysqld_safe
  12.  
  13. KILL_MYSQLD=1;
  14.  
  15. trap '' 1 2 3 15            # we shouldn't let anyone kill us
  16.  
  17. umask 007
  18.  
  19. defaults=
  20. case "$1" in
  21.     --no-defaults|--defaults-file=*|--defaults-extra-file=*)
  22.       defaults="$1"; shift
  23.       ;;
  24. esac
  25.  
  26. parse_arguments() {
  27.   # We only need to pass arguments through to the server if we don't
  28.   # handle them here.  So, we collect unrecognized options (passed on
  29.   # the command line) into the args variable.
  30.   pick_args=
  31.   if test "$1" = PICK-ARGS-FROM-ARGV
  32.   then
  33.     pick_args=1
  34.     shift
  35.   fi
  36.  
  37.   for arg do
  38.     case "$arg" in
  39.       --skip-kill-mysqld*)
  40.         KILL_MYSQLD=0;
  41.         ;;
  42.       # these get passed explicitly to mysqld
  43.       --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
  44.       --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
  45.       --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
  46.       --user=*)
  47.         if test $SET_USER -eq 0
  48.         then
  49.           user=`echo "$arg" | sed -e "s;--[^=]*=;;"`
  50.         fi
  51.         SET_USER=1
  52.         ;;
  53.  
  54.       # these two might have been set in a [mysqld_safe] section of my.cnf
  55.       # they are added to mysqld command line to override settings from my.cnf
  56.       --socket=*)  mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
  57.       --port=*)    mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
  58.  
  59.       # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
  60.       --ledir=*)   ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
  61.       # err-log should be removed in 5.0
  62.       --err-log=*) err_log=`echo "$arg" | sed -e "s;--err-log=;;"` ;;
  63.       --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
  64.       # QQ The --open-files should be removed in 5.0
  65.       --open-files=*) open_files=`echo "$arg" | sed -e "s;--open-files=;;"` ;;
  66.       --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
  67.       --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
  68.       --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
  69.       --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
  70.       --mysqld-version=*)
  71.     tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
  72.     if test -n "$tmp"
  73.     then
  74.       MYSQLD="mysqld-$tmp"
  75.     else
  76.       MYSQLD="mysqld"
  77.     fi
  78.     ;;
  79.       --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
  80.       *)
  81.         if test -n "$pick_args"
  82.         then
  83.           # This sed command makes sure that any special chars are quoted,
  84.           # so the arg gets passed exactly to the server.
  85.           args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
  86.         fi
  87.         ;;
  88.     esac
  89.   done
  90. }
  91.  
  92.  
  93. MY_PWD=`pwd`
  94. # Check if we are starting this relative (for the binary release)
  95. if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
  96.  -x ./bin/mysqld
  97. then
  98.   MY_BASEDIR_VERSION=$MY_PWD        # Where bin, share and data are
  99.   ledir=$MY_BASEDIR_VERSION/bin        # Where mysqld is
  100.   DATADIR=$MY_BASEDIR_VERSION/data
  101.   if test -z "$defaults"
  102.   then
  103.     defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  104.   fi
  105. # Check if this is a 'moved install directory'
  106. elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
  107.  -x ./libexec/mysqld
  108. then
  109.   MY_BASEDIR_VERSION=$MY_PWD        # Where libexec, share and var are
  110.   ledir=$MY_BASEDIR_VERSION/libexec    # Where mysqld is
  111.   DATADIR=$MY_BASEDIR_VERSION/var
  112. else
  113.   MY_BASEDIR_VERSION=/usr/local/mysql
  114.   DATADIR=/usr/local/mysql/var
  115.   ledir=/usr/local/mysql/libexec
  116. fi
  117.  
  118. user=mysql
  119. niceness=0
  120.  
  121. # Use the mysqld-max binary by default if the user doesn't specify a binary
  122. if test -x $ledir/mysqld-max
  123. then
  124.   MYSQLD=mysqld-max
  125. else
  126.   MYSQLD=mysqld
  127. fi
  128.  
  129. # these rely on $DATADIR by default, so we'll set them later on
  130. pid_file=
  131. err_log=
  132.  
  133. # Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
  134. # and then merge with the command line arguments
  135. if test -x ./bin/my_print_defaults
  136. then
  137.   print_defaults="./bin/my_print_defaults"
  138. elif test -x /usr/local/mysql/bin/my_print_defaults
  139. then
  140.   print_defaults="/usr/local/mysql/bin/my_print_defaults"
  141. elif test -x /usr/local/mysql/bin/mysql_print_defaults
  142. then
  143.   print_defaults="/usr/local/mysql/bin/mysql_print_defaults"
  144. else
  145.   print_defaults="my_print_defaults"
  146. fi
  147.  
  148. args=
  149. SET_USER=2
  150. parse_arguments `$print_defaults --loose-verbose $defaults mysqld server`
  151. if test $SET_USER -eq 2
  152. then
  153.   SET_USER=0
  154. fi
  155. parse_arguments `$print_defaults --loose-verbose $defaults mysqld_safe safe_mysqld`
  156. parse_arguments PICK-ARGS-FROM-ARGV "$@"
  157. safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-/tmp/mysql.sock}}
  158.  
  159. if test ! -x $ledir/$MYSQLD
  160. then
  161.   echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
  162.   echo "Please do a cd to the mysql installation directory and restart"
  163.   echo "this script from there as follows:"
  164.   echo "./bin/mysqld_safe".
  165.   exit 1
  166. fi
  167.  
  168. if test -z "$pid_file"
  169. then
  170.   pid_file=$DATADIR/`/bin/hostname`.pid
  171. else
  172.   case "$pid_file" in
  173.     /* ) ;;
  174.     * )  pid_file="$DATADIR/$pid_file" ;;
  175.   esac
  176. fi
  177. test -z "$err_log"  && err_log=$DATADIR/`/bin/hostname`.err
  178.  
  179. if test -n "$mysql_unix_port"
  180. then
  181.   args="--socket=$mysql_unix_port $args"
  182. fi
  183. if test -n "$mysql_tcp_port"
  184. then
  185.   args="--port=$mysql_tcp_port $args"
  186. fi
  187.  
  188. if test $niceness -eq 0
  189. then
  190.   NOHUP_NICENESS="nohup"
  191. else
  192.   NOHUP_NICENESS="nohup nice -$niceness"
  193. fi
  194.  
  195. # Using nice with no args to get the niceness level is GNU-specific.
  196. # This check could be extended for other operating systems (e.g.,
  197. # BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
  198. # But, it also seems that GNU nohup is the only one which messes
  199. # with the priority, so this is okay.
  200. if nohup nice > /dev/null 2>&1
  201. then
  202.     normal_niceness=`nice`
  203.     nohup_niceness=`nohup nice`
  204.  
  205.     numeric_nice_values=1
  206.     for val in $normal_niceness $nohup_niceness
  207.     do
  208.         case "$val" in
  209.             -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
  210.              [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
  211.                 ;;
  212.             * )
  213.                 numeric_nice_values=0 ;;
  214.         esac
  215.     done
  216.  
  217.     if test $numeric_nice_values -eq 1
  218.     then
  219.         nice_value_diff=`expr $nohup_niceness - $normal_niceness`
  220.         if test $? -eq 0 && test $nice_value_diff -gt 0 && \
  221.             nice --$nice_value_diff echo testing > /dev/null 2>&1
  222.         then
  223.             # nohup increases the priority (bad), and we are permitted
  224.             # to lower the priority with respect to the value the user
  225.             # might have been given
  226.             niceness=`expr $niceness - $nice_value_diff`
  227.             NOHUP_NICENESS="nice -$niceness nohup"
  228.         fi
  229.     fi
  230. else
  231.     if nohup echo testing > /dev/null 2>&1
  232.     then
  233.         :
  234.     else
  235.         # nohup doesn't work on this system
  236.         NOHUP_NICENESS=""
  237.     fi
  238. fi
  239.  
  240. USER_OPTION=""
  241. if test -w / -o "$USER" = "root"
  242. then
  243.   if test "$user" != "root" -o $SET_USER = 1
  244.   then
  245.     USER_OPTION="--user=$user"
  246.   fi
  247.   # If we are root, change the err log to the right user.
  248.   touch $err_log; chown $user $err_log
  249.   if test -n "$open_files"
  250.   then
  251.     ulimit -n $open_files
  252.     args="--open-files-limit=$open_files $args"
  253.   fi
  254.   if test -n "$core_file_size"
  255.   then
  256.     ulimit -c $core_file_size
  257.   fi
  258. fi
  259.  
  260. #
  261. # If there exists an old pid file, check if the daemon is already running
  262. # Note: The switches to 'ps' may depend on your operating system
  263. if test -f $pid_file
  264. then
  265.   PID=`cat $pid_file`
  266.   if /bin/kill -0 $PID > /dev/null 2> /dev/null
  267.   then
  268.     if /bin/ps p $PID | grep mysqld > /dev/null
  269.     then    # The pid contains a mysqld process
  270.       echo "A mysqld process already exists"
  271.       echo "A mysqld process already exists at " `date` >> $err_log
  272.       exit 1
  273.     fi
  274.   fi
  275.   rm -f $pid_file
  276.   if test -f $pid_file
  277.   then
  278.     echo "Fatal error: Can't remove the pid file: $pid_file"
  279.     echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
  280.     echo "Please remove it manually and start $0 again"
  281.     echo "mysqld daemon not started"
  282.     exit 1
  283.   fi
  284. fi
  285.  
  286. #
  287. # Uncomment the following lines if you want all tables to be automatically
  288. # checked and repaired during startup. You should add sensible key_buffer
  289. # and sort_buffer values to my.cnf to improve check performance or require
  290. # less disk space.
  291. # Alternatively, you can start mysqld with the "myisam-recover" option. See
  292. # the manual for details.
  293. #
  294. # echo "Checking tables in $DATADIR"
  295. # $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
  296. # $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
  297.  
  298. echo "Starting $MYSQLD daemon with databases from $DATADIR"
  299.  
  300. # Does this work on all systems?
  301. #if type ulimit | grep "shell builtin" > /dev/null
  302. #then
  303. #  ulimit -n 256 > /dev/null 2>&1        # Fix for BSD and FreeBSD systems
  304. #fi
  305.  
  306. echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
  307. while true
  308. do
  309.   rm -f $safe_mysql_unix_port $pid_file    # Some extra safety
  310.   if test -z "$args"
  311.   then
  312.     $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file --skip-locking >> $err_log 2>&1
  313.   else
  314.     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"
  315.   fi
  316.   if test ! -f $pid_file        # This is removed if normal shutdown
  317.   then
  318.     break
  319.   fi
  320.  
  321.   if test true -a $KILL_MYSQLD -eq 1
  322.   then
  323.     # Test if one process was hanging.
  324.     # This is only a fix for Linux (running as base 3 mysqld processes)
  325.     # but should work for the rest of the servers.
  326.     # The only thing is ps x => redhat 5 gives warnings when using ps -x.
  327.     # kill -9 is used or the process won't react on the kill.
  328.     numofproces=`ps xa | grep -v "grep" | grep -c $ledir/$MYSQLD`
  329.     echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
  330.     I=1
  331.     while test "$I" -le "$numofproces"
  332.     do 
  333.       PROC=`ps xa | grep $ledir/$MYSQLD | grep -v "grep" | sed -n '$p'` 
  334.     for T in $PROC
  335.     do
  336.       break
  337.     done
  338.     #    echo "TEST $I - $T **"
  339.     if kill -9 $T
  340.     then
  341.       echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
  342.     else 
  343.       break
  344.     fi
  345.     I=`expr $I + 1`
  346.     done
  347.   fi
  348.   echo "`date +'%y%m%d %H:%M:%S'`  mysqld restarted" | tee -a $err_log
  349. done
  350.  
  351. echo "`date +'%y%m%d %H:%M:%S'`  mysqld ended" | tee -a $err_log
  352. echo "" | tee -a $err_log
  353.