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
# MySQL daemon start/stop script.
7
# Usually this is put in /etc/init.d (at least on machines SYSV R4 based
8
# systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
9
# When this is done the mysql server will be started when the machine is
10
# started and shut down when the systems goes down.
12
# Comments to support chkconfig on RedHat Linux
13
# chkconfig: 2345 64 36
14
# description: A very fast and reliable SQL database engine.
16
# Comments to support LSB init script conventions
19
# Required-Start: $local_fs $network $remote_fs
20
# Should-Start: ypbind nscd ldap ntpd xntpd
21
# Required-Stop: $local_fs $network $remote_fs
22
# Default-Start: 2 3 4 5
24
# Short-Description: start and stop MySQL
25
# Description: MySQL is a very fast and reliable SQL database engine.
28
# If you install MySQL on some other places than @prefix@, then you
29
# have to do one of the following things for this script to work:
31
# - Run this script from within the MySQL installation directory
32
# - Create a /etc/my.cnf file with the following information:
34
# basedir=<path-to-mysql-installation-directory>
35
# - Add the above to any other configuration file (for example ~/.my.ini)
36
# and copy my_print_defaults to /usr/bin
37
# - Add the path to the mysql-installation-directory to the basedir variable
40
# If you want to affect other MySQL variables, you should make your changes
41
# in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
43
# If you change base dir, you must also change datadir. These may get
44
# overwritten by settings in the MySQL configuration files.
49
# Default value, in seconds, afterwhich the script should timeout waiting
51
# Value here is overriden by value in my.cnf.
52
# 0 means don't wait at all
53
# Negative numbers mean to wait indefinitely
54
service_startup_timeout=900
56
# Lock directory for RedHat / SuSE.
57
lockdir='/var/lock/subsys'
58
lock_file_path="$lockdir/mysql"
60
# The following variables are only set for letting mysql.server find things.
70
datadir=@localstatedir@
73
libexecdir=@libexecdir@
78
datadir="$basedir/data"
80
sbindir="$basedir/sbin"
81
if test -f "$basedir/bin/mysqld"
83
libexecdir="$basedir/bin"
85
libexecdir="$basedir/libexec"
89
# datadir_set is used to determine if datadir was set (and so should be
90
# *not* set inside of the --basedir= handler.)
94
# Use LSB init script functions for printing messages, if possible
96
lsb_functions="/lib/lsb/init-functions"
97
if test -f $lsb_functions ; then
110
PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
113
mode=$1 # start or stop
115
[ $# -ge 1 ] && shift
118
other_args="$*" # uncommon, but needed when called from an RPM upgrade action
119
# Expected: "--skip-networking --skip-grant-tables"
120
# They are not checked here, intentionally, as it is the resposibility
121
# of the "spec" file author to give correct arguments only.
123
case `echo "testing\c"`,`echo -n testing` in
124
*c*,-n*) echo_n= echo_c= ;;
125
*c*,*) echo_n=-n echo_c= ;;
126
*) echo_n= echo_c='\c' ;;
129
parse_server_arguments() {
132
--basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
133
bindir="$basedir/bin"
134
if test -z "$datadir_set"; then
135
datadir="$basedir/data"
137
sbindir="$basedir/sbin"
138
if test -f "$basedir/bin/mysqld"
140
libexecdir="$basedir/bin"
142
libexecdir="$basedir/libexec"
144
libexecdir="$basedir/libexec"
146
--datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
149
--pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
150
--service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
156
verb="$1" # created | removed
157
pid="$2" # process ID of the program operating on the pid-file
158
pid_file_path="$3" # path to the PID file.
161
avoid_race_condition="by checking again"
163
while test $i -ne $service_startup_timeout ; do
167
# wait for a PID-file to pop into existence.
168
test -s "$pid_file_path" && i='' && break
171
# wait for this PID-file to disappear
172
test ! -s "$pid_file_path" && i='' && break
175
echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
180
# if server isn't running, then pid-file will never be updated
181
if test -n "$pid"; then
182
if kill -0 "$pid" 2>/dev/null; then
183
: # the server still runs
185
# The server may have exited between the last pid-file check and now.
186
if test -n "$avoid_race_condition"; then
187
avoid_race_condition=""
188
continue # Check again.
191
# there's nothing that will affect the file.
192
log_failure_msg "The server quit without updating PID file ($pid_file_path)."
193
return 1 # not waiting any more.
197
echo $echo_n ".$echo_c"
203
if test -z "$i" ; then
212
# Get arguments from the my.cnf file,
213
# the only group, which is read from now on is [mysqld]
214
if test -x ./bin/my_print_defaults
216
print_defaults="./bin/my_print_defaults"
217
elif test -x $bindir/my_print_defaults
219
print_defaults="$bindir/my_print_defaults"
220
elif test -x $bindir/mysql_print_defaults
222
print_defaults="$bindir/mysql_print_defaults"
224
# Try to find basedir in /etc/my.cnf
229
subpat='^[^=]*basedir[^=]*=\(.*\)$'
230
dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
233
d=`echo $d | sed -e 's/[ ]//g'`
234
if test -x "$d/bin/my_print_defaults"
236
print_defaults="$d/bin/my_print_defaults"
239
if test -x "$d/bin/mysql_print_defaults"
241
print_defaults="$d/bin/mysql_print_defaults"
247
# Hope it's in the PATH ... but I doubt it
248
test -z "$print_defaults" && print_defaults="my_print_defaults"
252
# Read defaults file from 'basedir'. If there is no defaults file there
253
# check if it's in the old (depricated) place (datadir) and read it from there
257
if test -r "$basedir/my.cnf"
259
extra_args="-e $basedir/my.cnf"
261
if test -r "$datadir/my.cnf"
263
extra_args="-e $datadir/my.cnf"
267
parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
270
# Set pid file if not given
272
if test -z "$mysqld_pid_file_path"
274
mysqld_pid_file_path=$datadir/`@HOSTNAME@`.pid
276
case "$mysqld_pid_file_path" in
278
* ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
286
# Safeguard (relative paths, core dumps..)
289
echo $echo_n "Starting MySQL"
290
if test -x $bindir/mysqld_safe
292
# Give extra arguments to mysqld with the my.cnf file. This script
293
# may be overwritten at next upgrade.
294
$bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null 2>&1 &
295
wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
297
# Make lock for RedHat / SuSE
298
if test -w "$lockdir"
300
touch "$lock_file_path"
305
log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
310
# Stop daemon. We use a signal here to avoid having to know the
313
if test -s "$mysqld_pid_file_path"
315
mysqld_pid=`cat "$mysqld_pid_file_path"`
317
if (kill -0 $mysqld_pid 2>/dev/null)
319
echo $echo_n "Shutting down MySQL"
321
# mysqld should remove the pid file when it exits, so wait for it.
322
wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
324
log_failure_msg "MySQL server process #$mysqld_pid is not running!"
325
rm "$mysqld_pid_file_path"
328
# Delete lock for RedHat / SuSE
329
if test -f "$lock_file_path"
331
rm -f "$lock_file_path"
335
log_failure_msg "MySQL server PID file could not be found!"
340
# Stop the service and regardless of whether it was
341
# running or not, start it again.
342
if $0 stop $other_args; then
345
log_failure_msg "Failed to stop running server, so refusing to try to start."
350
'reload'|'force-reload')
351
if test -s "$mysqld_pid_file_path" ; then
352
read mysqld_pid < "$mysqld_pid_file_path"
353
kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
354
touch "$mysqld_pid_file_path"
356
log_failure_msg "MySQL PID file could not be found!"
361
# First, check to see if pid file exists
362
if test -s "$mysqld_pid_file_path" ; then
363
read mysqld_pid < "$mysqld_pid_file_path"
364
if kill -0 $mysqld_pid 2>/dev/null ; then
365
log_success_msg "MySQL running ($mysqld_pid)"
368
log_failure_msg "MySQL is not running, but PID file exists"
372
# Try to find appropriate mysqld process
373
mysqld_pid=`pidof $libexecdir/mysqld`
375
# test if multiple pids exist
376
pid_count=`echo $mysqld_pid | wc -w`
377
if test $pid_count -gt 1 ; then
378
log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
380
elif test -z $mysqld_pid ; then
381
if test -f "$lock_file_path" ; then
382
log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
385
log_failure_msg "MySQL is not running"
388
log_failure_msg "MySQL is running but PID file could not be found"
394
# Safeguard (relative paths, core dumps..)
396
echo $echo_n "Testing MySQL configuration syntax"
397
daemon=$bindir/mysqld
398
if test -x $libexecdir/mysqld
400
daemon=$libexecdir/mysqld
401
elif test -x $sbindir/mysqld
403
daemon=$sbindir/mysqld
404
elif test -x `which mysqld`
406
daemon=`which mysqld`
408
log_failure_msg "Unable to locate the mysqld binary!"
411
help_out=`$daemon --help 2>&1`; r=$?
412
if test "$r" != 0 ; then
413
log_failure_msg "$help_out"
414
log_failure_msg "There are syntax errors in the server configuration. Please fix them!"
416
log_success_msg "Syntax OK"
422
basename=`basename "$0"`
423
echo "Usage: $basename {start|stop|restart|reload|force-reload|status|configtest} [ MySQL server options ]"