mysqld_safe.sh 10.6 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10
#!/bin/sh
# Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
# This file is public domain and comes with NO WARRANTY of any kind
#
# scripts to start the MySQL daemon and restart it if it dies unexpectedly
#
# This should be executed in the MySQL base directory if you are using a
# binary installation that has other paths than you are using.
#
# mysql.server works by first doing a cd to the base directory and from there
unknown's avatar
unknown committed
11
# executing mysqld_safe
unknown's avatar
unknown committed
12

13 14
KILL_MYSQLD=1;

unknown's avatar
unknown committed
15 16
trap '' 1 2 3 15			# we shouldn't let anyone kill us

17 18
umask 007

unknown's avatar
unknown committed
19 20
defaults=
case "$1" in
unknown's avatar
unknown committed
21
    --no-defaults|--defaults-file=*|--defaults-extra-file=*)
unknown's avatar
unknown committed
22 23 24 25 26
      defaults="$1"; shift
      ;;
esac

parse_arguments() {
unknown's avatar
unknown committed
27 28 29 30 31 32 33 34 35 36 37 38
  # We only need to pass arguments through to the server if we don't
  # handle them here.  So, we collect unrecognized options (passed on
  # the command line) into the args variable.
  pick_args=
  if test "$1" = PICK-ARGS-FROM-ARGV
  then
    pick_args=1
    shift
  fi

  for arg do
    case "$arg" in
39 40 41
      --skip-kill-mysqld*)
        KILL_MYSQLD=0;
        ;;
unknown's avatar
unknown committed
42 43 44
      # these get passed explicitly to mysqld
      --basedir=*) MY_BASEDIR_VERSION=`echo "$arg" | sed -e "s;--basedir=;;"` ;;
      --datadir=*) DATADIR=`echo "$arg" | sed -e "s;--datadir=;;"` ;;
unknown's avatar
unknown committed
45
      --pid-file=*) pid_file=`echo "$arg" | sed -e "s;--pid-file=;;"` ;;
46
      --user=*) user=`echo "$arg" | sed -e "s;--[^=]*=;;"` ; SET_USER=1 ;;
unknown's avatar
unknown committed
47

unknown's avatar
unknown committed
48
      # these two might have been set in a [mysqld_safe] section of my.cnf
49 50 51
      # they are added to mysqld command line to override settings from my.cnf
      --socket=*)  mysql_unix_port=`echo "$arg" | sed -e "s;--socket=;;"` ;;
      --port=*)    mysql_tcp_port=`echo "$arg" | sed -e "s;--port=;;"` ;;
unknown's avatar
unknown committed
52

unknown's avatar
unknown committed
53
      # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
unknown's avatar
unknown committed
54
      --ledir=*)   ledir=`echo "$arg" | sed -e "s;--ledir=;;"` ;;
55
      # err-log should be removed in 5.0
unknown's avatar
unknown committed
56
      --err-log=*) err_log=`echo "$arg" | sed -e "s;--err-log=;;"` ;;
57 58
      --log-error=*) err_log=`echo "$arg" | sed -e "s;--log-error=;;"` ;;
      # QQ The --open-files should be removed in 5.0
59
      --open-files=*) open_files=`echo "$arg" | sed -e "s;--open-files=;;"` ;;
unknown's avatar
unknown committed
60
      --open-files-limit=*) open_files=`echo "$arg" | sed -e "s;--open-files-limit=;;"` ;;
61
      --core-file-size=*) core_file_size=`echo "$arg" | sed -e "s;--core-file-size=;;"` ;;
62
      --timezone=*) TZ=`echo "$arg" | sed -e "s;--timezone=;;"` ; export TZ; ;;
unknown's avatar
unknown committed
63
      --mysqld=*)   MYSQLD=`echo "$arg" | sed -e "s;--mysqld=;;"` ;;
unknown's avatar
unknown committed
64 65 66 67 68 69 70 71 72
      --mysqld-version=*)
	tmp=`echo "$arg" | sed -e "s;--mysqld-version=;;"`
	if test -n "$tmp"
	then
	  MYSQLD="mysqld-$tmp"
	else
	  MYSQLD="mysqld"
	fi
	;;
73
      --nice=*) niceness=`echo "$arg" | sed -e "s;--nice=;;"` ;;
unknown's avatar
unknown committed
74 75 76 77 78 79 80 81
      *)
        if test -n "$pick_args"
        then
          # This sed command makes sure that any special chars are quoted,
          # so the arg gets passed exactly to the server.
          args="$args "`echo "$arg" | sed -e 's,\([^a-zA-Z0-9_.-]\),\\\\\1,g'`
        fi
        ;;
unknown's avatar
unknown committed
82 83 84 85
    esac
  done
}

86

unknown's avatar
unknown committed
87 88 89 90 91 92 93 94
MY_PWD=`pwd`
# Check if we are starting this relative (for the binary release)
if test -d $MY_PWD/data/mysql -a -f ./share/mysql/english/errmsg.sys -a \
 -x ./bin/mysqld
then
  MY_BASEDIR_VERSION=$MY_PWD		# Where bin, share and data are
  ledir=$MY_BASEDIR_VERSION/bin		# Where mysqld is
  DATADIR=$MY_BASEDIR_VERSION/data
unknown's avatar
unknown committed
95
  if test -z "$defaults"
unknown's avatar
unknown committed
96 97 98
  then
    defaults="--defaults-extra-file=$MY_BASEDIR_VERSION/data/my.cnf"
  fi
unknown's avatar
unknown committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112
# Check if this is a 'moved install directory'
elif test -f ./var/mysql/db.frm -a -f ./share/mysql/english/errmsg.sys -a \
 -x ./libexec/mysqld
then
  MY_BASEDIR_VERSION=$MY_PWD		# Where libexec, share and var are
  ledir=$MY_BASEDIR_VERSION/libexec	# Where mysqld is
  DATADIR=$MY_BASEDIR_VERSION/var
else
  MY_BASEDIR_VERSION=@prefix@
  DATADIR=@localstatedir@
  ledir=@libexecdir@
fi

user=@MYSQLD_USER@
113
niceness=0
114 115 116 117 118 119 120 121

# Use the mysqld-max binary by default if the user doesn't specify a binary
if test -x $ledir/mysqld-max
then
  MYSQLD=mysqld-max
else
  MYSQLD=mysqld
fi
unknown's avatar
unknown committed
122

unknown's avatar
unknown committed
123 124 125 126
# these rely on $DATADIR by default, so we'll set them later on
pid_file=
err_log=

unknown's avatar
unknown committed
127
# Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
unknown's avatar
unknown committed
128
# and then merge with the command line arguments
unknown's avatar
unknown committed
129 130
if test -x ./bin/my_print_defaults
then
unknown's avatar
unknown committed
131
  print_defaults="./bin/my_print_defaults"
unknown's avatar
unknown committed
132 133
elif test -x @bindir@/my_print_defaults
then
unknown's avatar
unknown committed
134
  print_defaults="@bindir@/my_print_defaults"
unknown's avatar
unknown committed
135 136
elif test -x @bindir@/mysql_print_defaults
then
unknown's avatar
unknown committed
137 138 139 140
  print_defaults="@bindir@/mysql_print_defaults"
else
  print_defaults="my_print_defaults"
fi
unknown's avatar
unknown committed
141 142

args=
143
SET_USER=2
unknown's avatar
unknown committed
144
parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
145 146 147 148
if test $SET_USER -eq 2
then
  SET_USER=0
fi
unknown's avatar
unknown committed
149
parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
unknown's avatar
unknown committed
150
parse_arguments PICK-ARGS-FROM-ARGV "$@"
151
safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-@MYSQL_UNIX_ADDR@}}
unknown's avatar
unknown committed
152

unknown's avatar
unknown committed
153
if test ! -x $ledir/$MYSQLD
unknown's avatar
unknown committed
154
then
unknown's avatar
unknown committed
155
  echo "The file $ledir/$MYSQLD doesn't exist or is not executable"
unknown's avatar
unknown committed
156 157
  echo "Please do a cd to the mysql installation directory and restart"
  echo "this script from there as follows:"
unknown's avatar
unknown committed
158
  echo "./bin/mysqld_safe".
159 160
  echo "See http://dev.mysql.com/doc/mysql/en/mysqld_safe.html for more"
  echo "information"
unknown's avatar
unknown committed
161
  exit 1
unknown's avatar
unknown committed
162 163
fi

164 165 166 167 168 169 170 171 172
if test -z "$pid_file"
then
  pid_file=$DATADIR/`@HOSTNAME@`.pid
else
  case "$pid_file" in
    /* ) ;;
    * )  pid_file="$DATADIR/$pid_file" ;;
  esac
fi
unknown's avatar
unknown committed
173 174
test -z "$err_log"  && err_log=$DATADIR/`@HOSTNAME@`.err

175 176 177 178 179 180 181 182
if test -n "$mysql_unix_port"
then
  args="--socket=$mysql_unix_port $args"
fi
if test -n "$mysql_tcp_port"
then
  args="--port=$mysql_tcp_port $args"
fi
unknown's avatar
unknown committed
183

184 185 186 187 188 189
if test $niceness -eq 0
then
  NOHUP_NICENESS="nohup"
else
  NOHUP_NICENESS="nohup nice -$niceness"
fi
190 191 192 193 194 195 196

# Using nice with no args to get the niceness level is GNU-specific.
# This check could be extended for other operating systems (e.g.,
# BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
# But, it also seems that GNU nohup is the only one which messes
# with the priority, so this is okay.
if nohup nice > /dev/null 2>&1
unknown's avatar
unknown committed
197
then
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    normal_niceness=`nice`
    nohup_niceness=`nohup nice`

    numeric_nice_values=1
    for val in $normal_niceness $nohup_niceness
    do
        case "$val" in
            -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
             [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
                ;;
            * )
                numeric_nice_values=0 ;;
        esac
    done

    if test $numeric_nice_values -eq 1
    then
        nice_value_diff=`expr $nohup_niceness - $normal_niceness`
        if test $? -eq 0 && test $nice_value_diff -gt 0 && \
            nice --$nice_value_diff echo testing > /dev/null 2>&1
        then
            # nohup increases the priority (bad), and we are permitted
220 221 222 223
            # to lower the priority with respect to the value the user
            # might have been given
            niceness=`expr $niceness - $nice_value_diff`
            NOHUP_NICENESS="nice -$niceness nohup"
224 225 226 227 228 229 230 231 232 233
        fi
    fi
else
    if nohup echo testing > /dev/null 2>&1
    then
        :
    else
        # nohup doesn't work on this system
        NOHUP_NICENESS=""
    fi
unknown's avatar
unknown committed
234 235
fi

236
USER_OPTION=""
237
if test -w / -o "$USER" = "root"
unknown's avatar
unknown committed
238
then
239 240 241 242
  if test "$user" != "root" -o $SET_USER = 1
  then
    USER_OPTION="--user=$user"
  fi
unknown's avatar
unknown committed
243 244
  # If we are root, change the err log to the right user.
  touch $err_log; chown $user $err_log
245 246 247
  if test -n "$open_files"
  then
    ulimit -n $open_files
248
    args="--open-files-limit=$open_files $args"
249 250 251 252 253
  fi
  if test -n "$core_file_size"
  then
    ulimit -c $core_file_size
  fi
unknown's avatar
unknown committed
254
fi
unknown's avatar
unknown committed
255 256 257 258 259 260 261 262 263 264 265 266 267

#
# If there exists an old pid file, check if the daemon is already running
# Note: The switches to 'ps' may depend on your operating system
if test -f $pid_file
then
  PID=`cat $pid_file`
  if @CHECK_PID@
  then
    if @FIND_PROC@
    then    # The pid contains a mysqld process
      echo "A mysqld process already exists"
      echo "A mysqld process already exists at " `date` >> $err_log
unknown's avatar
unknown committed
268
      exit 1
unknown's avatar
unknown committed
269 270 271 272 273 274 275 276 277
    fi
  fi
  rm -f $pid_file
  if test -f $pid_file
  then
    echo "Fatal error: Can't remove the pid file: $pid_file"
    echo "Fatal error: Can't remove the pid file: $pid_file at " `date` >> $err_log
    echo "Please remove it manually and start $0 again"
    echo "mysqld daemon not started"
unknown's avatar
unknown committed
278
    exit 1
unknown's avatar
unknown committed
279 280 281 282
  fi
fi

#
283 284 285 286 287 288
# Uncomment the following lines if you want all tables to be automatically
# checked and repaired during startup. You should add sensible key_buffer
# and sort_buffer values to my.cnf to improve check performance or require
# less disk space.
# Alternatively, you can start mysqld with the "myisam-recover" option. See
# the manual for details.
unknown's avatar
unknown committed
289 290
#
# echo "Checking tables in $DATADIR"
291 292
# $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
# $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
unknown's avatar
unknown committed
293

unknown's avatar
unknown committed
294
echo "Starting $MYSQLD daemon with databases from $DATADIR"
unknown's avatar
unknown committed
295 296 297 298 299 300 301 302 303 304

# Does this work on all systems?
#if type ulimit | grep "shell builtin" > /dev/null
#then
#  ulimit -n 256 > /dev/null 2>&1		# Fix for BSD and FreeBSD systems
#fi

echo "`date +'%y%m%d %H:%M:%S  mysqld started'`" >> $err_log
while true
do
305
  rm -f $safe_mysql_unix_port $pid_file	# Some extra safety
unknown's avatar
unknown committed
306
  if test -z "$args"
unknown's avatar
unknown committed
307
  then
unknown's avatar
unknown committed
308
    $NOHUP_NICENESS $ledir/$MYSQLD $defaults --basedir=$MY_BASEDIR_VERSION --datadir=$DATADIR $USER_OPTION --pid-file=$pid_file @MYSQLD_DEFAULT_SWITCHES@ >> $err_log 2>&1
unknown's avatar
unknown committed
309
  else
unknown's avatar
unknown committed
310
    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"
unknown's avatar
unknown committed
311 312 313
  fi
  if test ! -f $pid_file		# This is removed if normal shutdown
  then
unknown's avatar
unknown committed
314
    echo "STOPPING server from pid file $pid_file"
unknown's avatar
unknown committed
315
    break
unknown's avatar
unknown committed
316
  fi
unknown's avatar
unknown committed
317

318
  if @IS_LINUX@ && test $KILL_MYSQLD -eq 1
unknown's avatar
unknown committed
319 320 321 322 323 324
  then
    # Test if one process was hanging.
    # This is only a fix for Linux (running as base 3 mysqld processes)
    # but should work for the rest of the servers.
    # The only thing is ps x => redhat 5 gives warnings when using ps -x.
    # kill -9 is used or the process won't react on the kill.
325
    numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
unknown's avatar
unknown committed
326

unknown's avatar
unknown committed
327 328 329 330
    echo -e "\nNumber of processes running now: $numofproces" | tee -a $err_log
    I=1
    while test "$I" -le "$numofproces"
    do 
331
      PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
unknown's avatar
unknown committed
332 333 334 335 336 337 338

      for T in $PROC
      do
        break
      done
      #    echo "TEST $I - $T **"
      if kill -9 $T
unknown's avatar
unknown committed
339
      then
unknown's avatar
unknown committed
340 341 342
        echo "$MYSQLD process hanging, pid $T - killed" | tee -a $err_log
      else 
        break
unknown's avatar
unknown committed
343
      fi
unknown's avatar
unknown committed
344
      I=`expr $I + 1`
unknown's avatar
unknown committed
345 346
    done
  fi
347
  echo "`date +'%y%m%d %H:%M:%S'`  mysqld restarted" | tee -a $err_log
unknown's avatar
unknown committed
348 349
done

unknown's avatar
unknown committed
350
echo "`date +'%y%m%d %H:%M:%S'`  mysqld ended" | tee -a $err_log
351
echo "" | tee -a $err_log
unknown's avatar
unknown committed
352