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
# The following variables are only set for letting mysql.server find things.
69
datadir=@localstatedir@
72
libexecdir=@libexecdir@
77
datadir="$basedir/data"
79
sbindir="$basedir/sbin"
80
libexecdir="$basedir/libexec"
83
# datadir_set is used to determine if datadir was set (and so should be
84
# *not* set inside of the --basedir= handler.)
88
# Use LSB init script functions for printing messages, if possible
90
lsb_functions="/lib/lsb/init-functions"
91
if test -f $lsb_functions ; then
104
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
107
mode=$1 # start or stop
109
other_args="$*" # uncommon, but needed when called from an RPM upgrade action
110
# Expected: "--skip-networking --skip-grant-tables"
111
# They are not checked here, intentionally, as it is the resposibility
112
# of the "spec" file author to give correct arguments only.
114
case `echo "testing\c"`,`echo -n testing` in
115
*c*,-n*) echo_n= echo_c= ;;
116
*c*,*) echo_n=-n echo_c= ;;
117
*) echo_n= echo_c='\c' ;;
120
parse_server_arguments() {
123
--basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
124
bindir="$basedir/bin"
125
if test -z "$datadir_set"; then
126
datadir="$basedir/data"
128
sbindir="$basedir/sbin"
129
libexecdir="$basedir/libexec"
131
--datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
134
--user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
135
--pid-file=*) server_pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
136
--service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
137
--use-mysqld_safe) use_mysqld_safe=1;;
138
--use-manager) use_mysqld_safe=0;;
143
parse_manager_arguments() {
146
--pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
147
--user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
154
manager_pid="$2" # process ID of the program operating on the pid-file
156
avoid_race_condition="by checking again"
157
while test $i -ne $service_startup_timeout ; do
161
# wait for a PID-file to pop into existence.
162
test -s $pid_file && i='' && break
165
# wait for this PID-file to disappear
166
test ! -s $pid_file && i='' && break
169
echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid"
174
# if manager isn't running, then pid-file will never be updated
175
if test -n "$manager_pid"; then
176
if kill -0 "$manager_pid" 2>/dev/null; then
177
: # the manager still runs
179
# The manager may have exited between the last pid-file check and now.
180
if test -n "$avoid_race_condition"; then
181
avoid_race_condition=""
182
continue # Check again.
185
# there's nothing that will affect the file.
186
log_failure_msg "Manager of pid-file quit without updating file."
187
return 1 # not waiting any more.
191
echo $echo_n ".$echo_c"
196
if test -z "$i" ; then
205
# Get arguments from the my.cnf file,
206
# the only group, which is read from now on is [mysqld]
207
if test -x ./bin/my_print_defaults
209
print_defaults="./bin/my_print_defaults"
210
elif test -x $bindir/my_print_defaults
212
print_defaults="$bindir/my_print_defaults"
213
elif test -x $bindir/mysql_print_defaults
215
print_defaults="$bindir/mysql_print_defaults"
217
# Try to find basedir in /etc/my.cnf
222
subpat='^[^=]*basedir[^=]*=\(.*\)$'
223
dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
226
d=`echo $d | sed -e 's/[ ]//g'`
227
if test -x "$d/bin/my_print_defaults"
229
print_defaults="$d/bin/my_print_defaults"
232
if test -x "$d/bin/mysql_print_defaults"
234
print_defaults="$d/bin/mysql_print_defaults"
240
# Hope it's in the PATH ... but I doubt it
241
test -z "$print_defaults" && print_defaults="my_print_defaults"
245
# Read defaults file from 'basedir'. If there is no defaults file there
246
# check if it's in the old (depricated) place (datadir) and read it from there
250
if test -r "$basedir/my.cnf"
252
extra_args="-e $basedir/my.cnf"
254
if test -r "$datadir/my.cnf"
256
extra_args="-e $datadir/my.cnf"
260
parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
262
# Look for the pidfile
263
parse_manager_arguments `$print_defaults $extra_args manager`
266
# Set pid file if not given
268
if test -z "$pid_file"
270
pid_file=$datadir/mysqlmanager-`@HOSTNAME@`.pid
274
* ) pid_file="$datadir/$pid_file" ;;
277
if test -z "$server_pid_file"
279
server_pid_file=$datadir/`@HOSTNAME@`.pid
281
case "$server_pid_file" in
283
* ) server_pid_file="$datadir/$server_pid_file" ;;
291
# Safeguard (relative paths, core dumps..)
294
manager=$bindir/mysqlmanager
295
if test -x $libexecdir/mysqlmanager
297
manager=$libexecdir/mysqlmanager
298
elif test -x $sbindir/mysqlmanager
300
manager=$sbindir/mysqlmanager
303
echo $echo_n "Starting MySQL"
304
if test -x $manager -a "$use_mysqld_safe" = "0"
306
if test -n "$other_args"
308
log_failure_msg "MySQL manager does not support options '$other_args'"
311
# Give extra arguments to mysqld with the my.cnf file. This script may
312
# be overwritten at next upgrade.
314
--mysqld-safe-compatible \
316
--pid-file="$pid_file" >/dev/null 2>&1 &
317
wait_for_pid created $!; return_value=$?
319
# Make lock for RedHat / SuSE
320
if test -w /var/lock/subsys
322
touch /var/lock/subsys/mysqlmanager
325
elif test -x $bindir/mysqld_safe
327
# Give extra arguments to mysqld with the my.cnf file. This script
328
# may be overwritten at next upgrade.
329
pid_file=$server_pid_file
330
$bindir/mysqld_safe --datadir=$datadir --pid-file=$server_pid_file $other_args >/dev/null 2>&1 &
331
wait_for_pid created $!; return_value=$?
333
# Make lock for RedHat / SuSE
334
if test -w /var/lock/subsys
336
touch /var/lock/subsys/mysql
340
log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
345
# Stop daemon. We use a signal here to avoid having to know the
348
# The RedHat / SuSE lock directory to remove
349
lock_dir=/var/lock/subsys/mysqlmanager
351
# If the manager pid_file doesn't exist, try the server's
352
if test ! -s "$pid_file"
354
pid_file=$server_pid_file
355
lock_dir=/var/lock/subsys/mysql
358
if test -s "$pid_file"
360
mysqlmanager_pid=`cat $pid_file`
361
echo $echo_n "Shutting down MySQL"
362
kill $mysqlmanager_pid
363
# mysqlmanager should remove the pid_file when it exits, so wait for it.
364
wait_for_pid removed "$mysqlmanager_pid"; return_value=$?
366
# delete lock for RedHat / SuSE
373
log_failure_msg "MySQL manager or server PID file could not be found!"
378
# Stop the service and regardless of whether it was
379
# running or not, start it again.
380
if $0 stop $other_args; then
383
log_failure_msg "Failed to stop running server, so refusing to try to start."
388
'reload'|'force-reload')
389
if test -s "$server_pid_file" ; then
390
read mysqld_pid < $server_pid_file
391
kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
392
touch $server_pid_file
394
log_failure_msg "MySQL PID file could not be found!"
399
# First, check to see if pid file exists
400
if test -s "$server_pid_file" ; then
401
read mysqld_pid < $server_pid_file
402
if kill -0 $mysqld_pid 2>/dev/null ; then
403
log_success_msg "MySQL running ($mysqld_pid)"
406
log_failure_msg "MySQL is not running, but PID file exists"
410
# Try to find appropriate mysqld process
411
mysqld_pid=`pidof $libexecdir/mysqld`
412
if test -z $mysqld_pid ; then
413
if test "$use_mysqld_safe" = "0" ; then
414
lockfile=/var/lock/subsys/mysqlmanager
416
lockfile=/var/lock/subsys/mysql
418
if test -f $lockfile ; then
419
log_failure_msg "MySQL is not running, but lock exists"
422
log_failure_msg "MySQL is not running"
425
log_failure_msg "MySQL is running but PID file could not be found"
432
echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"