~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
# Script 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 is not installed in its compile-time default
9
# location
10
#
11
# mysql.server works by first doing a cd to the base directory and from there
12
# executing mysqld_safe
13
14
KILL_MYSQLD=1;
15
MYSQLD=
16
niceness=0
17
# Initial logging status: error log is not open, and not using syslog
18
logging=init
19
want_syslog=0
20
syslog_tag=
21
user=@MYSQLD_USER@
22
pid_file=
23
err_log=
24
25
syslog_tag_mysqld=mysqld
26
syslog_tag_mysqld_safe=mysqld_safe
27
28
trap '' 1 2 3 15			# we shouldn't let anyone kill us
29
30
umask 007
31
32
defaults=
33
case "$1" in
34
    --no-defaults|--defaults-file=*|--defaults-extra-file=*)
35
      defaults="$1"; shift
36
      ;;
37
esac
38
39
usage () {
40
        cat <<EOF
41
Usage: $0 [OPTIONS]
42
  --no-defaults              Don't read the system defaults file
43
  --defaults-file=FILE       Use the specified defaults file
44
  --defaults-extra-file=FILE Also use defaults from the specified file
45
  --ledir=DIRECTORY          Look for mysqld in the specified directory
46
  --open-files-limit=LIMIT   Limit the number of open files
47
  --core-file-size=LIMIT     Limit core files to the specified size
48
  --timezone=TZ              Set the system timezone
49
  --mysqld=FILE              Use the specified file as mysqld
50
  --mysqld-version=VERSION   Use "mysqld-VERSION" as mysqld
51
  --nice=NICE                Set the scheduling priority of mysqld
52
  --skip-kill-mysqld         Don't try to kill stray mysqld processes
53
  --syslog                   Log messages to syslog with 'logger'
54
  --skip-syslog              Log messages to error log (default)
55
  --syslog-tag=TAG           Pass -t "mysqld-TAG" to 'logger'
56
57
All other options are passed to the mysqld program.
58
59
EOF
60
        exit 1
61
}
62
63
my_which ()
64
{
65
  save_ifs="${IFS-UNSET}"
66
  IFS=:
67
  for file
68
  do
69
    for dir in $PATH
70
    do
71
      if [ -f "$dir/$file" ]
72
      then
73
        echo "$dir/$file"
74
        continue 2
75
      fi
76
    done
77
    return 1  # Failure, didn't find file in path
78
  done
79
  if [ "$save_ifs" = UNSET ]
80
  then
81
    unset IFS
82
  else
83
    IFS="$save_ifs"
84
  fi
85
  return 0  # Success
86
}
87
88
log_generic () {
89
  priority="$1"
90
  shift
91
92
  msg="`date +'%y%m%d %H:%M:%S'` mysqld_safe $*"
93
  echo "$msg"
94
  case $logging in
95
    init) ;;  # Just echo the message, don't save it anywhere
96
    file) echo "$msg" >> "$err_log" ;;
97
    syslog) logger -t "$syslog_tag_mysqld_safe" -p "$priority" "$*" ;;
98
    *)
99
      echo "Internal program error (non-fatal):" \
100
           " unknown logging method '$logging'" >&2
101
      ;;
102
  esac
103
}
104
105
log_error () {
106
  log_generic daemon.error "$@" >&2
107
}
108
109
log_notice () {
110
  log_generic daemon.notice "$@"
111
}
112
113
eval_log_error () {
114
  cmd="$1"
115
  case $logging in
116
    file) cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" ;;
117
    syslog)
118
      # mysqld often prefixes its messages with a timestamp, which is
119
      # redundant when logging to syslog (which adds its own timestamp)
120
      # However, we don't strip the timestamp with sed here, because
121
      # sed buffers output (only GNU sed supports a -u (unbuffered) option)
122
      # which means that messages may not get sent to syslog until the
123
      # mysqld process quits.
124
      cmd="$cmd 2>&1 | logger -t '$syslog_tag_mysqld' -p daemon.error"
125
      ;;
126
    *)
127
      echo "Internal program error (non-fatal):" \
128
           " unknown logging method '$logging'" >&2
129
      ;;
130
  esac
131
132
  #echo "Running mysqld: [$cmd]"
133
  eval "$cmd"
134
}
135
136
shell_quote_string() {
137
  # This sed command makes sure that any special chars are quoted,
138
  # so the arg gets passed exactly to the server.
139
  echo "$1" | sed -e 's,\([^a-zA-Z0-9/_.=-]\),\\\1,g'
140
}
141
142
parse_arguments() {
143
  # We only need to pass arguments through to the server if we don't
144
  # handle them here.  So, we collect unrecognized options (passed on
145
  # the command line) into the args variable.
146
  pick_args=
147
  if test "$1" = PICK-ARGS-FROM-ARGV
148
  then
149
    pick_args=1
150
    shift
151
  fi
152
153
  for arg do
154
    val=`echo "$arg" | sed -e "s;--[^=]*=;;"`
155
    case "$arg" in
156
      # these get passed explicitly to mysqld
157
      --basedir=*) MY_BASEDIR_VERSION="$val" ;;
158
      --datadir=*) DATADIR="$val" ;;
159
      --pid-file=*) pid_file="$val" ;;
160
      --user=*) user="$val"; SET_USER=1 ;;
161
162
      # these might have been set in a [mysqld_safe] section of my.cnf
163
      # they are added to mysqld command line to override settings from my.cnf
164
      --log-error=*) err_log="$val" ;;
165
      --port=*) mysql_tcp_port="$val" ;;
166
      --socket=*) mysql_unix_port="$val" ;;
167
168
      # mysqld_safe-specific options - must be set in my.cnf ([mysqld_safe])!
169
      --core-file-size=*) core_file_size="$val" ;;
170
      --ledir=*) ledir="$val" ;;
171
      --mysqld=*) MYSQLD="$val" ;;
172
      --mysqld-version=*)
173
        if test -n "$val"
174
        then
175
          MYSQLD="mysqld-$val"
176
        else
177
          MYSQLD="mysqld"
178
        fi
179
        ;;
180
      --nice=*) niceness="$val" ;;
181
      --open-files-limit=*) open_files="$val" ;;
182
      --skip-kill-mysqld*) KILL_MYSQLD=0 ;;
183
      --syslog) want_syslog=1 ;;
184
      --skip-syslog) want_syslog=0 ;;
185
      --syslog-tag=*) syslog_tag="$val" ;;
186
      --timezone=*) TZ="$val"; export TZ; ;;
187
188
      --help) usage ;;
189
190
      *)
191
        if test -n "$pick_args"
192
        then
193
          append_arg_to_args "$arg"
194
        fi
195
        ;;
196
    esac
197
  done
198
}
199
200
201
#
202
# First, try to find BASEDIR and ledir (where mysqld is)
203
#
204
205
if echo '@pkgdatadir@' | grep '^@prefix@' > /dev/null
206
then
207
  relpkgdata=`echo '@pkgdatadir@' | sed -e 's,^@prefix@,,' -e 's,^/,,' -e 's,^,./,'`
208
else
209
  # pkgdatadir is not relative to prefix
210
  relpkgdata='@pkgdatadir@'
211
fi
212
213
MY_PWD=`pwd`
214
# Check for the directories we would expect from a binary release install
215
if test -f "$relpkgdata"/english/errmsg.sys -a -x ./bin/mysqld
216
then
217
  MY_BASEDIR_VERSION=$MY_PWD		# Where bin, share and data are
218
  ledir=$MY_BASEDIR_VERSION/bin		# Where mysqld is
219
# Check for the directories we would expect from a source install
220
elif test -f "$relpkgdata"/english/errmsg.sys -a -x ./libexec/mysqld
221
then
222
  MY_BASEDIR_VERSION=$MY_PWD		# Where libexec, share and var are
223
  ledir=$MY_BASEDIR_VERSION/libexec	# Where mysqld is
224
# Since we didn't find anything, used the compiled-in defaults
225
else
226
  MY_BASEDIR_VERSION=@prefix@
227
  ledir=@libexecdir@
228
fi
229
230
231
#
232
# Second, try to find the data directory
233
#
234
235
# Try where the binary installs put it
236
if test -d $MY_BASEDIR_VERSION/data/mysql
237
then
238
  DATADIR=$MY_BASEDIR_VERSION/data
239
  if test -z "$defaults" -a -r "$DATADIR/my.cnf"
240
  then
241
    defaults="--defaults-extra-file=$DATADIR/my.cnf"
242
  fi
243
# Next try where the source installs put it
244
elif test -d $MY_BASEDIR_VERSION/var/mysql
245
then
246
  DATADIR=$MY_BASEDIR_VERSION/var
247
# Or just give up and use our compiled-in default
248
else
249
  DATADIR=@localstatedir@
250
fi
251
252
if test -z "$MYSQL_HOME"
253
then 
254
  if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
255
  then
256
    log_error "WARNING: Found two instances of my.cnf -
257
$MY_BASEDIR_VERSION/my.cnf and
258
$DATADIR/my.cnf
259
IGNORING $DATADIR/my.cnf"
260
261
    MYSQL_HOME=$MY_BASEDIR_VERSION
262
  elif test -r "$DATADIR/my.cnf"
263
  then
264
    log_error "WARNING: Found $DATADIR/my.cnf
265
The data directory is a deprecated location for my.cnf, please move it to
266
$MY_BASEDIR_VERSION/my.cnf"
267
    MYSQL_HOME=$DATADIR
268
  else
269
    MYSQL_HOME=$MY_BASEDIR_VERSION
270
  fi
271
fi
272
export MYSQL_HOME
273
274
275
# Get first arguments from the my.cnf file, groups [mysqld] and [mysqld_safe]
276
# and then merge with the command line arguments
277
if test -x ./bin/my_print_defaults
278
then
279
  print_defaults="./bin/my_print_defaults"
280
elif test -x @bindir@/my_print_defaults
281
then
282
  print_defaults="@bindir@/my_print_defaults"
283
elif test -x @bindir@/mysql_print_defaults
284
then
285
  print_defaults="@bindir@/mysql_print_defaults"
286
else
287
  print_defaults="my_print_defaults"
288
fi
289
290
append_arg_to_args () {
291
  args="$args "`shell_quote_string "$1"`
292
}
293
294
args=
295
296
SET_USER=2
297
parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
298
if test $SET_USER -eq 2
299
then
300
  SET_USER=0
301
fi
302
303
parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
304
parse_arguments PICK-ARGS-FROM-ARGV "$@"
305
306
# Determine what logging facility to use
307
308
# Ensure that 'logger' exists, if it's requested
309
if [ $want_syslog -eq 1 ]
310
then
311
  my_which logger > /dev/null 2>&1
312
  if [ $? -ne 0 ]
313
  then
314
    log_error "--syslog requested, but no 'logger' program found.  Please ensure that 'logger' is in your PATH, or do not specify the --syslog option to mysqld_safe."
315
    exit 1
316
  fi
317
fi
318
319
if [ -n "$err_log" -o $want_syslog -eq 0 ]
320
then
321
  if [ -n "$err_log" ]
322
  then
323
    # mysqld adds ".err" if there is no extension on the --log-error
324
    # argument; must match that here, or mysqld_safe will write to a
325
    # different log file than mysqld
326
327
    # mysqld does not add ".err" to "--log-error=foo."; it considers a
328
    # trailing "." as an extension
329
    if expr "$err_log" : '.*\.[^/]*$' > /dev/null
330
    then
331
        :
332
    else
333
      err_log="$err_log".err
334
    fi
335
336
    case "$err_log" in
337
      /* ) ;;
338
      * ) err_log="$DATADIR/$err_log" ;;
339
    esac
340
  else
341
    err_log=$DATADIR/`@HOSTNAME@`.err
342
  fi
343
344
  append_arg_to_args "--log-error=$err_log"
345
346
  if [ $want_syslog -eq 1 ]
347
  then
348
    # User explicitly asked for syslog, so warn that it isn't used
349
    log_error "Can't log to error log and syslog at the same time.  Remove all --log-error configuration options for --syslog to take effect."
350
  fi
351
352
  # Log to err_log file
353
  log_notice "Logging to '$err_log'."
354
  logging=file
355
else
356
  if [ -n "$syslog_tag" ]
357
  then
358
    # Sanitize the syslog tag
359
    syslog_tag=`echo "$syslog_tag" | sed -e 's/[^a-zA-Z0-9_-]/_/g'`
360
    syslog_tag_mysqld_safe="${syslog_tag_mysqld_safe}-$syslog_tag"
361
    syslog_tag_mysqld="${syslog_tag_mysqld}-$syslog_tag"
362
  fi
363
  log_notice "Logging to syslog."
364
  logging=syslog
365
fi
366
367
USER_OPTION=""
368
if test -w / -o "$USER" = "root"
369
then
370
  if test "$user" != "root" -o $SET_USER = 1
371
  then
372
    USER_OPTION="--user=$user"
373
  fi
374
  # Change the err log to the right user, if it is in use
375
  if [ $want_syslog -eq 0 ]; then
376
    touch $err_log
377
    chown $user $err_log
378
  fi
379
  if test -n "$open_files"
380
  then
381
    ulimit -n $open_files
382
    append_arg_to_args "--open-files-limit=$open_files"
383
  fi
384
fi
385
386
safe_mysql_unix_port=${mysql_unix_port:-${MYSQL_UNIX_PORT:-@MYSQL_UNIX_ADDR@}}
387
# Make sure that directory for $safe_mysql_unix_port exists
388
mysql_unix_port_dir=`dirname $safe_mysql_unix_port`
389
if [ ! -d $mysql_unix_port_dir ]
390
then
391
  mkdir $mysql_unix_port_dir
392
  chown $user $mysql_unix_port_dir
393
  chmod 755 $mysql_unix_port_dir
394
fi
395
396
# If the user doesn't specify a binary, we assume name "mysqld"
397
if test -z "$MYSQLD"
398
then
399
  MYSQLD=mysqld
400
fi
401
402
if test ! -x $ledir/$MYSQLD
403
then
404
  log_error "The file $ledir/$MYSQLD
405
does not exist or is not executable. Please cd to the mysql installation
406
directory and restart this script from there as follows:
407
./bin/mysqld_safe&
408
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information"
409
  exit 1
410
fi
411
412
if test -z "$pid_file"
413
then
414
  pid_file=$DATADIR/`@HOSTNAME@`.pid
415
else
416
  case "$pid_file" in
417
    /* ) ;;
418
    * )  pid_file="$DATADIR/$pid_file" ;;
419
  esac
420
fi
421
append_arg_to_args "--pid-file=$pid_file"
422
423
if test -n "$mysql_unix_port"
424
then
425
  append_arg_to_args "--socket=$mysql_unix_port"
426
fi
427
if test -n "$mysql_tcp_port"
428
then
429
  append_arg_to_args "--port=$mysql_tcp_port"
430
fi
431
432
if test $niceness -eq 0
433
then
434
  NOHUP_NICENESS="nohup"
435
else
436
  NOHUP_NICENESS="nohup nice -$niceness"
437
fi
438
439
# Using nice with no args to get the niceness level is GNU-specific.
440
# This check could be extended for other operating systems (e.g.,
441
# BSD could use "nohup sh -c 'ps -o nice -p $$' | tail -1").
442
# But, it also seems that GNU nohup is the only one which messes
443
# with the priority, so this is okay.
444
if nohup nice > /dev/null 2>&1
445
then
446
    normal_niceness=`nice`
447
    nohup_niceness=`nohup nice 2>/dev/null`
448
449
    numeric_nice_values=1
450
    for val in $normal_niceness $nohup_niceness
451
    do
452
        case "$val" in
453
            -[0-9] | -[0-9][0-9] | -[0-9][0-9][0-9] | \
454
             [0-9] |  [0-9][0-9] |  [0-9][0-9][0-9] )
455
                ;;
456
            * )
457
                numeric_nice_values=0 ;;
458
        esac
459
    done
460
461
    if test $numeric_nice_values -eq 1
462
    then
463
        nice_value_diff=`expr $nohup_niceness - $normal_niceness`
464
        if test $? -eq 0 && test $nice_value_diff -gt 0 && \
465
            nice --$nice_value_diff echo testing > /dev/null 2>&1
466
        then
467
            # nohup increases the priority (bad), and we are permitted
468
            # to lower the priority with respect to the value the user
469
            # might have been given
470
            niceness=`expr $niceness - $nice_value_diff`
471
            NOHUP_NICENESS="nice -$niceness nohup"
472
        fi
473
    fi
474
else
475
    if nohup echo testing > /dev/null 2>&1
476
    then
477
        :
478
    else
479
        # nohup doesn't work on this system
480
        NOHUP_NICENESS=""
481
    fi
482
fi
483
484
# Try to set the core file size (even if we aren't root) because many systems
485
# don't specify a hard limit on core file size.
486
if test -n "$core_file_size"
487
then
488
  ulimit -c $core_file_size
489
fi
490
491
#
492
# If there exists an old pid file, check if the daemon is already running
493
# Note: The switches to 'ps' may depend on your operating system
494
if test -f $pid_file
495
then
496
  PID=`cat $pid_file`
497
  if @CHECK_PID@
498
  then
499
    if @FIND_PROC@
500
    then    # The pid contains a mysqld process
501
      log_error "A mysqld process already exists"
502
      exit 1
503
    fi
504
  fi
505
  rm -f $pid_file
506
  if test -f $pid_file
507
  then
508
    log_error "Fatal error: Can't remove the pid file:
509
$pid_file
510
Please remove it manually and start $0 again;
511
mysqld daemon not started"
512
    exit 1
513
  fi
514
fi
515
516
#
517
# Uncomment the following lines if you want all tables to be automatically
518
# checked and repaired during startup. You should add sensible key_buffer
519
# and sort_buffer values to my.cnf to improve check performance or require
520
# less disk space.
521
# Alternatively, you can start mysqld with the "myisam-recover" option. See
522
# the manual for details.
523
#
524
# echo "Checking tables in $DATADIR"
525
# $MY_BASEDIR_VERSION/bin/myisamchk --silent --force --fast --medium-check $DATADIR/*/*.MYI
526
# $MY_BASEDIR_VERSION/bin/isamchk --silent --force $DATADIR/*/*.ISM
527
528
# Does this work on all systems?
529
#if type ulimit | grep "shell builtin" > /dev/null
530
#then
531
#  ulimit -n 256 > /dev/null 2>&1		# Fix for BSD and FreeBSD systems
532
#fi
533
534
cmd="$NOHUP_NICENESS"
535
536
for i in  "$ledir/$MYSQLD" "$defaults" "--basedir=$MY_BASEDIR_VERSION" \
537
  "--datadir=$DATADIR" "$USER_OPTION"
538
do
539
  cmd="$cmd "`shell_quote_string "$i"`
540
done
541
cmd="$cmd $args"
542
# Avoid 'nohup: ignoring input' warning
543
test -n "$NOHUP_NICENESS" && cmd="$cmd < /dev/null"
544
545
log_notice "Starting $MYSQLD daemon with databases from $DATADIR"
546
while true
547
do
548
  rm -f $safe_mysql_unix_port $pid_file	# Some extra safety
549
550
  eval_log_error "$cmd"
551
552
  if test ! -f $pid_file		# This is removed if normal shutdown
553
  then
554
    break
555
  fi
556
557
  if @TARGET_LINUX@ && test $KILL_MYSQLD -eq 1
558
  then
559
    # Test if one process was hanging.
560
    # This is only a fix for Linux (running as base 3 mysqld processes)
561
    # but should work for the rest of the servers.
562
    # The only thing is ps x => redhat 5 gives warnings when using ps -x.
563
    # kill -9 is used or the process won't react on the kill.
564
    numofproces=`ps xaww | grep -v "grep" | grep "$ledir/$MYSQLD\>" | grep -c "pid-file=$pid_file"`
565
566
    log_notice "Number of processes running now: $numofproces"
567
    I=1
568
    while test "$I" -le "$numofproces"
569
    do 
570
      PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'` 
571
572
      for T in $PROC
573
      do
574
        break
575
      done
576
      #    echo "TEST $I - $T **"
577
      if kill -9 $T
578
      then
579
        log_error "$MYSQLD process hanging, pid $T - killed"
580
      else
581
        break
582
      fi
583
      I=`expr $I + 1`
584
    done
585
  fi
586
  log_notice "mysqld restarted"
587
done
588
589
log_notice "mysqld from pid file $pid_file ended"
590