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# MySQL daemon start/stop script. 6 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. 11 12# Comments to support chkconfig on RedHat Linux 13# chkconfig: 2345 64 36 14# description: A very fast and reliable SQL database engine. 15 16# Comments to support LSB init script conventions 17### BEGIN INIT INFO 18# Provides: mysql 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 23# Default-Stop: 0 1 6 24# Short-Description: start and stop MySQL 25# Description: MySQL is a very fast and reliable SQL database engine. 26### END INIT INFO 27 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: 30# 31# - Run this script from within the MySQL installation directory 32# - Create a /etc/my.cnf file with the following information: 33# [mysqld] 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 38# below. 39# 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. 42 43# If you change base dir, you must also change datadir. These may get 44# overwritten by settings in the MySQL configuration files. 45 46basedir= 47datadir= 48 49# Default value, in seconds, afterwhich the script should timeout waiting 50# for server start. 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 54service_startup_timeout=900 55 56# Lock directory for RedHat / SuSE. 57lockdir='/var/lock/subsys' 58lock_file_path="$lockdir/mysql" 59 60# The following variables are only set for letting mysql.server find things. 61 62# Set some defaults 63mysqld_pid_file_path= 64if test -z "$basedir" 65then 66 basedir=@prefix@ 67 bindir=@bindir@ 68 if test -z "$datadir" 69 then 70 datadir=@localstatedir@ 71 fi 72 sbindir=@sbindir@ 73 libexecdir=@libexecdir@ 74else 75 bindir="$basedir/bin" 76 if test -z "$datadir" 77 then 78 datadir="$basedir/data" 79 fi 80 sbindir="$basedir/sbin" 81 libexecdir="$basedir/libexec" 82fi 83 84# datadir_set is used to determine if datadir was set (and so should be 85# *not* set inside of the --basedir= handler.) 86datadir_set= 87 88# 89# Use LSB init script functions for printing messages, if possible 90# 91lsb_functions="/lib/lsb/init-functions" 92if test -f $lsb_functions ; then 93 . $lsb_functions 94else 95 log_success_msg() 96 { 97 echo " SUCCESS! $@" 98 } 99 log_failure_msg() 100 { 101 echo " ERROR! $@" 102 } 103fi 104 105PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin" 106export PATH 107 108mode=$1 # start or stop 109 110[ $# -ge 1 ] && shift 111 112 113other_args="$*" # uncommon, but needed when called from an RPM upgrade action 114 # Expected: "--skip-networking --skip-grant-tables" 115 # They are not checked here, intentionally, as it is the resposibility 116 # of the "spec" file author to give correct arguments only. 117 118case `echo "testing\c"`,`echo -n testing` in 119 *c*,-n*) echo_n= echo_c= ;; 120 *c*,*) echo_n=-n echo_c= ;; 121 *) echo_n= echo_c='\c' ;; 122esac 123 124parse_server_arguments() { 125 for arg do 126 case "$arg" in 127 --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'` 128 bindir="$basedir/bin" 129 if test -z "$datadir_set"; then 130 datadir="$basedir/data" 131 fi 132 sbindir="$basedir/sbin" 133 libexecdir="$basedir/libexec" 134 ;; 135 --datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` 136 datadir_set=1 137 ;; 138 --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; 139 --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;; 140 esac 141 done 142} 143 144wait_for_pid () { 145 verb="$1" # created | removed 146 pid="$2" # process ID of the program operating on the pid-file 147 pid_file_path="$3" # path to the PID file. 148 149 i=0 150 avoid_race_condition="by checking again" 151 152 while test $i -ne $service_startup_timeout ; do 153 154 case "$verb" in 155 'created') 156 # wait for a PID-file to pop into existence. 157 test -s "$pid_file_path" && i='' && break 158 ;; 159 'removed') 160 # wait for this PID-file to disappear 161 test ! -s "$pid_file_path" && i='' && break 162 ;; 163 *) 164 echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path" 165 exit 1 166 ;; 167 esac 168 169 # if server isn't running, then pid-file will never be updated 170 if test -n "$pid"; then 171 if kill -0 "$pid" 2>/dev/null; then 172 : # the server still runs 173 else 174 # The server may have exited between the last pid-file check and now. 175 if test -n "$avoid_race_condition"; then 176 avoid_race_condition="" 177 continue # Check again. 178 fi 179 180 # there's nothing that will affect the file. 181 log_failure_msg "The server quit without updating PID file ($pid_file_path)." 182 return 1 # not waiting any more. 183 fi 184 fi 185 186 echo $echo_n ".$echo_c" 187 i=`expr $i + 1` 188 sleep 1 189 190 done 191 192 if test -z "$i" ; then 193 log_success_msg 194 return 0 195 else 196 log_failure_msg 197 return 1 198 fi 199} 200 201# Get arguments from the my.cnf file, 202# the only group, which is read from now on is [mysqld] 203if test -x "$bindir/my_print_defaults"; then 204 print_defaults="$bindir/my_print_defaults" 205else 206 # Try to find basedir in /etc/my.cnf 207 conf=/etc/my.cnf 208 print_defaults= 209 if test -r $conf 210 then 211 subpat='^[^=]*basedir[^=]*=\(.*\)$' 212 dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf` 213 for d in $dirs 214 do 215 d=`echo $d | sed -e 's/[ ]//g'` 216 if test -x "$d/bin/my_print_defaults" 217 then 218 print_defaults="$d/bin/my_print_defaults" 219 break 220 fi 221 done 222 fi 223 224 # Hope it's in the PATH ... but I doubt it 225 test -z "$print_defaults" && print_defaults="my_print_defaults" 226fi 227 228# 229# Read defaults file from 'basedir'. If there is no defaults file there 230# check if it's in the old (depricated) place (datadir) and read it from there 231# 232 233extra_args="" 234if test -r "$basedir/my.cnf" 235then 236 extra_args="-e $basedir/my.cnf" 237else 238 if test -r "$datadir/my.cnf" 239 then 240 extra_args="-e $datadir/my.cnf" 241 fi 242fi 243 244parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server` 245 246# 247# Set pid file if not given 248# 249if test -z "$mysqld_pid_file_path" 250then 251 mysqld_pid_file_path=$datadir/`@HOSTNAME@`.pid 252else 253 case "$mysqld_pid_file_path" in 254 /* ) ;; 255 * ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;; 256 esac 257fi 258 259case "$mode" in 260 'start') 261 # Start daemon 262 263 # Safeguard (relative paths, core dumps..) 264 cd $basedir 265 266 echo $echo_n "Starting MySQL" 267 if test -x $bindir/mysqld_safe 268 then 269 # Give extra arguments to mysqld with the my.cnf file. This script 270 # may be overwritten at next upgrade. 271 $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null & 272 wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$? 273 274 # Make lock for RedHat / SuSE 275 if test -w "$lockdir" 276 then 277 touch "$lock_file_path" 278 fi 279 280 exit $return_value 281 else 282 log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)" 283 fi 284 ;; 285 286 'stop') 287 # Stop daemon. We use a signal here to avoid having to know the 288 # root password. 289 290 if test -s "$mysqld_pid_file_path" 291 then 292 mysqld_pid=`cat "$mysqld_pid_file_path"` 293 294 if (kill -0 $mysqld_pid 2>/dev/null) 295 then 296 echo $echo_n "Shutting down MySQL" 297 kill $mysqld_pid 298 # mysqld should remove the pid file when it exits, so wait for it. 299 wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$? 300 else 301 log_failure_msg "MySQL server process #$mysqld_pid is not running!" 302 rm "$mysqld_pid_file_path" 303 fi 304 305 # Delete lock for RedHat / SuSE 306 if test -f "$lock_file_path" 307 then 308 rm -f "$lock_file_path" 309 fi 310 exit $return_value 311 else 312 log_failure_msg "MySQL server PID file could not be found!" 313 fi 314 ;; 315 316 'restart') 317 # Stop the service and regardless of whether it was 318 # running or not, start it again. 319 if $0 stop $other_args; then 320 $0 start $other_args 321 else 322 log_failure_msg "Failed to stop running server, so refusing to try to start." 323 exit 1 324 fi 325 ;; 326 327 'reload'|'force-reload') 328 if test -s "$mysqld_pid_file_path" ; then 329 read mysqld_pid < "$mysqld_pid_file_path" 330 kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL" 331 touch "$mysqld_pid_file_path" 332 else 333 log_failure_msg "MySQL PID file could not be found!" 334 exit 1 335 fi 336 ;; 337 'status') 338 # First, check to see if pid file exists 339 if test -s "$mysqld_pid_file_path" ; then 340 read mysqld_pid < "$mysqld_pid_file_path" 341 if kill -0 $mysqld_pid 2>/dev/null ; then 342 log_success_msg "MySQL running ($mysqld_pid)" 343 exit 0 344 else 345 log_failure_msg "MySQL is not running, but PID file exists" 346 exit 1 347 fi 348 else 349 # Try to find appropriate mysqld process 350 mysqld_pid=`@PIDOF@ $libexecdir/mysqld` 351 352 # test if multiple pids exist 353 pid_count=`echo $mysqld_pid | wc -w` 354 if test $pid_count -gt 1 ; then 355 log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)" 356 exit 5 357 elif test -z $mysqld_pid ; then 358 if test -f "$lock_file_path" ; then 359 log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists" 360 exit 2 361 fi 362 log_failure_msg "MySQL is not running" 363 exit 3 364 else 365 log_failure_msg "MySQL is running but PID file could not be found" 366 exit 4 367 fi 368 fi 369 ;; 370 *) 371 # usage 372 basename=`basename "$0"` 373 echo "Usage: $basename {start|stop|restart|reload|force-reload|status} [ MySQL server options ]" 374 exit 1 375 ;; 376esac 377 378exit 0 379