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 /usr, 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.
72 libexecdir
=/usr
/libexec
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 while test $i -ne $service_startup_timeout ; do
158 test -s $pid_file && i
='' && break
159 kill -0 $2 ||
break # if the program goes away, stop waiting
162 test ! -s $pid_file && i
='' && break
165 echo "wait_for_pid () usage: wait_for_pid created|removed"
169 echo $echo_n ".$echo_c"
173 if test -z "$i" ; then
182 # Get arguments from the my.cnf file,
183 # the only group, which is read from now on is [mysqld]
184 if test -x .
/bin
/my_print_defaults
186 print_defaults
="./bin/my_print_defaults"
187 elif test -x $bindir/my_print_defaults
189 print_defaults
="$bindir/my_print_defaults"
190 elif test -x $bindir/mysql_print_defaults
192 print_defaults
="$bindir/mysql_print_defaults"
194 # Try to find basedir in /etc/my.cnf
199 subpat
='^[^=]*basedir[^=]*=\(.*\)$'
200 dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
203 d
=`echo $d | sed -e 's/[ ]//g'`
204 if test -x "$d/bin/my_print_defaults"
206 print_defaults
="$d/bin/my_print_defaults"
209 if test -x "$d/bin/mysql_print_defaults"
211 print_defaults
="$d/bin/mysql_print_defaults"
217 # Hope it's in the PATH ... but I doubt it
218 test -z "$print_defaults" && print_defaults
="my_print_defaults"
222 # Read defaults file from 'basedir'. If there is no defaults file there
223 # check if it's in the old (depricated) place (datadir) and read it from there
227 if test -r "$basedir/my.cnf"
229 extra_args
="-e $basedir/my.cnf"
231 if test -r "$datadir/my.cnf"
233 extra_args
="-e $datadir/my.cnf"
237 parse_server_arguments
`$print_defaults $extra_args mysqld server mysql_server mysql.server`
239 # Look for the pidfile
240 parse_manager_arguments
`$print_defaults $extra_args manager`
243 # Set pid file if not given
245 if test -z "$pid_file"
247 pid_file
=$datadir/mysqlmanager-
`/usr/bin/hostname`.pid
251 * ) pid_file
="$datadir/$pid_file" ;;
254 if test -z "$server_pid_file"
256 server_pid_file
=$datadir/`/usr/bin/hostname`.pid
258 case "$server_pid_file" in
260 * ) server_pid_file
="$datadir/$server_pid_file" ;;
268 # Safeguard (relative paths, core dumps..)
271 manager
=$bindir/mysqlmanager
272 if test -x $libexecdir/mysqlmanager
274 manager
=$libexecdir/mysqlmanager
275 elif test -x $sbindir/mysqlmanager
277 manager
=$sbindir/mysqlmanager
280 echo $echo_n "Starting MySQL"
281 if test -x $manager -a "$use_mysqld_safe" = "0"
283 if test -n "$other_args"
285 log_failure_msg
"MySQL manager does not support options '$other_args'"
288 # Give extra arguments to mysqld with the my.cnf file. This script may
289 # be overwritten at next upgrade.
290 echo "Executing: " "$manager" \
291 --mysqld-safe-compatible \
293 --pid-file="$pid_file" >/dev
/null
2>&1 &
295 --mysqld-safe-compatible \
297 --pid-file="$pid_file" >/dev
/null
2>&1 &
298 wait_for_pid created $
!; return_value
=$?
300 # Make lock for RedHat / SuSE
301 if test -w /var
/lock
/subsys
303 touch /var
/lock
/subsys
/mysqlmanager
306 elif test -x $bindir/mysqld_safe
308 # Give extra arguments to mysqld with the my.cnf file. This script
309 # may be overwritten at next upgrade.
310 pid_file
=$server_pid_file
311 echo "Executing " $bindir/mysqld_safe
--datadir=$datadir --pid-file=$server_pid_file $other_args
312 $bindir/mysqld_safe
--datadir=$datadir --pid-file=$server_pid_file $other_args >/dev
/null
2>&1 &
313 wait_for_pid created $
!; return_value
=$?
315 # Make lock for RedHat / SuSE
316 if test -w /var
/lock
/subsys
318 touch /var
/lock
/subsys
/mysql
322 log_failure_msg
"Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
327 # Stop daemon. We use a signal here to avoid having to know the
330 # The RedHat / SuSE lock directory to remove
331 lock_dir
=/var
/lock
/subsys
/mysqlmanager
333 # If the manager pid_file doesn't exist, try the server's
334 if test ! -s "$pid_file"
336 pid_file
=$server_pid_file
337 lock_dir
=/var
/lock
/subsys
/mysql
340 if test -s "$pid_file"
342 mysqlmanager_pid
=`cat $pid_file`
343 echo $echo_n "Shutting down MySQL"
344 kill $mysqlmanager_pid
345 # mysqlmanager should remove the pid_file when it exits, so wait for it.
346 wait_for_pid removed
; return_value
=$?
348 # delete lock for RedHat / SuSE
355 log_failure_msg
"MySQL manager or server PID file could not be found!"
360 # Stop the service and regardless of whether it was
361 # running or not, start it again.
362 if $0 stop
$other_args; then
365 log_failure_msg
"Failed to stop running server, so refusing to try to start."
370 'reload'|
'force-reload')
371 if test -s "$server_pid_file" ; then
372 read mysqld_pid
< $server_pid_file
373 kill -HUP $mysqld_pid && log_success_msg
"Reloading service MySQL"
374 touch $server_pid_file
376 log_failure_msg
"MySQL PID file could not be found!"
381 # First, check to see if pid file exists
382 if test -s "$server_pid_file" ; then
383 read mysqld_pid
< $server_pid_file
384 if kill -0 $mysqld_pid 2>/dev
/null
; then
385 log_success_msg
"MySQL running ($mysqld_pid)"
388 log_failure_msg
"MySQL is not running, but PID file exists"
392 # Try to find appropriate mysqld process
393 mysqld_pid
=`pidof $libexecdir/mysqld`
394 if test -z $mysqld_pid ; then
395 if test "$use_mysqld_safe" = "0" ; then
396 lockfile
=/var
/lock
/subsys
/mysqlmanager
398 lockfile
=/var
/lock
/subsys
/mysql
400 if test -f $lockfile ; then
401 log_failure_msg
"MySQL is not running, but lock exists"
404 log_failure_msg
"MySQL is not running"
407 log_failure_msg
"MySQL is running but PID file could not be found"
414 echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"