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
5
# Script to start the MySQL daemon and restart it if it dies unexpectedly
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
11
# mysql.server works by first doing a cd to the base directory and from there
12
# executing mysqld_safe
17
# Initial logging status: error log is not open, and not using syslog
25
syslog_tag_mysqld=mysqld
26
syslog_tag_mysqld_safe=mysqld_safe
28
trap '' 1 2 3 15 # we shouldn't let anyone kill us
34
--no-defaults|--defaults-file=*|--defaults-extra-file=*)
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'
57
All other options are passed to the mysqld program.
65
save_ifs="${IFS-UNSET}"
71
if [ -f "$dir/$file" ]
77
return 1 # Failure, didn't find file in path
79
if [ "$save_ifs" = UNSET ]
92
msg="`date +'%y%m%d %H:%M:%S'` mysqld_safe $*"
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" "$*" ;;
99
echo "Internal program error (non-fatal):" \
100
" unknown logging method '$logging'" >&2
106
log_generic daemon.error "$@" >&2
110
log_generic daemon.notice "$@"
116
file) cmd="$cmd >> "`shell_quote_string "$err_log"`" 2>&1" ;;
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"
127
echo "Internal program error (non-fatal):" \
128
" unknown logging method '$logging'" >&2
132
#echo "Running mysqld: [$cmd]"
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'
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.
147
if test "$1" = PICK-ARGS-FROM-ARGV
154
val=`echo "$arg" | sed -e "s;--[^=]*=;;"`
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 ;;
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" ;;
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" ;;
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; ;;
191
if test -n "$pick_args"
193
append_arg_to_args "$arg"
202
# First, try to find BASEDIR and ledir (where mysqld is)
205
if echo '@pkgdatadir@' | grep '^@prefix@' > /dev/null
207
relpkgdata=`echo '@pkgdatadir@' | sed -e 's,^@prefix@,,' -e 's,^/,,' -e 's,^,./,'`
209
# pkgdatadir is not relative to prefix
210
relpkgdata='@pkgdatadir@'
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
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
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
226
MY_BASEDIR_VERSION=@prefix@
232
# Second, try to find the data directory
235
# Try where the binary installs put it
236
if test -d $MY_BASEDIR_VERSION/data/mysql
238
DATADIR=$MY_BASEDIR_VERSION/data
239
if test -z "$defaults" -a -r "$DATADIR/my.cnf"
241
defaults="--defaults-extra-file=$DATADIR/my.cnf"
243
# Next try where the source installs put it
244
elif test -d $MY_BASEDIR_VERSION/var/mysql
246
DATADIR=$MY_BASEDIR_VERSION/var
247
# Or just give up and use our compiled-in default
249
DATADIR=@localstatedir@
252
if test -z "$MYSQL_HOME"
254
if test -r "$MY_BASEDIR_VERSION/my.cnf" && test -r "$DATADIR/my.cnf"
256
log_error "WARNING: Found two instances of my.cnf -
257
$MY_BASEDIR_VERSION/my.cnf and
259
IGNORING $DATADIR/my.cnf"
261
MYSQL_HOME=$MY_BASEDIR_VERSION
262
elif test -r "$DATADIR/my.cnf"
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"
269
MYSQL_HOME=$MY_BASEDIR_VERSION
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
279
print_defaults="./bin/my_print_defaults"
280
elif test -x @bindir@/my_print_defaults
282
print_defaults="@bindir@/my_print_defaults"
283
elif test -x @bindir@/mysql_print_defaults
285
print_defaults="@bindir@/mysql_print_defaults"
287
print_defaults="my_print_defaults"
290
append_arg_to_args () {
291
args="$args "`shell_quote_string "$1"`
297
parse_arguments `$print_defaults $defaults --loose-verbose mysqld server`
298
if test $SET_USER -eq 2
303
parse_arguments `$print_defaults $defaults --loose-verbose mysqld_safe safe_mysqld`
304
parse_arguments PICK-ARGS-FROM-ARGV "$@"
306
# Determine what logging facility to use
308
# Ensure that 'logger' exists, if it's requested
309
if [ $want_syslog -eq 1 ]
311
my_which logger > /dev/null 2>&1
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."
319
if [ -n "$err_log" -o $want_syslog -eq 0 ]
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
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
333
err_log="$err_log".err
338
* ) err_log="$DATADIR/$err_log" ;;
341
err_log=$DATADIR/`@HOSTNAME@`.err
344
append_arg_to_args "--log-error=$err_log"
346
if [ $want_syslog -eq 1 ]
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."
352
# Log to err_log file
353
log_notice "Logging to '$err_log'."
356
if [ -n "$syslog_tag" ]
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"
363
log_notice "Logging to syslog."
368
if test -w / -o "$USER" = "root"
370
if test "$user" != "root" -o $SET_USER = 1
372
USER_OPTION="--user=$user"
374
# Change the err log to the right user, if it is in use
375
if [ $want_syslog -eq 0 ]; then
379
if test -n "$open_files"
381
ulimit -n $open_files
382
append_arg_to_args "--open-files-limit=$open_files"
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 ]
391
mkdir $mysql_unix_port_dir
392
chown $user $mysql_unix_port_dir
393
chmod 755 $mysql_unix_port_dir
396
# If the user doesn't specify a binary, we assume name "mysqld"
402
if test ! -x $ledir/$MYSQLD
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:
408
See http://dev.mysql.com/doc/mysql/en/mysqld-safe.html for more information"
412
if test -z "$pid_file"
414
pid_file=$DATADIR/`@HOSTNAME@`.pid
418
* ) pid_file="$DATADIR/$pid_file" ;;
421
append_arg_to_args "--pid-file=$pid_file"
423
if test -n "$mysql_unix_port"
425
append_arg_to_args "--socket=$mysql_unix_port"
427
if test -n "$mysql_tcp_port"
429
append_arg_to_args "--port=$mysql_tcp_port"
432
if test $niceness -eq 0
434
NOHUP_NICENESS="nohup"
436
NOHUP_NICENESS="nohup nice -$niceness"
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
446
normal_niceness=`nice`
447
nohup_niceness=`nohup nice 2>/dev/null`
449
numeric_nice_values=1
450
for val in $normal_niceness $nohup_niceness
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] )
457
numeric_nice_values=0 ;;
461
if test $numeric_nice_values -eq 1
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
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"
475
if nohup echo testing > /dev/null 2>&1
479
# nohup doesn't work on this system
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"
488
ulimit -c $core_file_size
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
500
then # The pid contains a mysqld process
501
log_error "A mysqld process already exists"
508
log_error "Fatal error: Can't remove the pid file:
510
Please remove it manually and start $0 again;
511
mysqld daemon not started"
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
521
# Alternatively, you can start mysqld with the "myisam-recover" option. See
522
# the manual for details.
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
528
# Does this work on all systems?
529
#if type ulimit | grep "shell builtin" > /dev/null
531
# ulimit -n 256 > /dev/null 2>&1 # Fix for BSD and FreeBSD systems
534
cmd="$NOHUP_NICENESS"
536
for i in "$ledir/$MYSQLD" "$defaults" "--basedir=$MY_BASEDIR_VERSION" \
537
"--datadir=$DATADIR" "$USER_OPTION"
539
cmd="$cmd "`shell_quote_string "$i"`
542
# Avoid 'nohup: ignoring input' warning
543
test -n "$NOHUP_NICENESS" && cmd="$cmd < /dev/null"
545
log_notice "Starting $MYSQLD daemon with databases from $DATADIR"
548
rm -f $safe_mysql_unix_port $pid_file # Some extra safety
550
eval_log_error "$cmd"
552
if test ! -f $pid_file # This is removed if normal shutdown
557
if @TARGET_LINUX@ && test $KILL_MYSQLD -eq 1
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"`
566
log_notice "Number of processes running now: $numofproces"
568
while test "$I" -le "$numofproces"
570
PROC=`ps xaww | grep "$ledir/$MYSQLD\>" | grep -v "grep" | grep "pid-file=$pid_file" | sed -n '$p'`
576
# echo "TEST $I - $T **"
579
log_error "$MYSQLD process hanging, pid $T - killed"
586
log_notice "mysqld restarted"
589
log_notice "mysqld from pid file $pid_file ended"