removed main.cfg, not used anywhere
[limo.git] / mysql.server
blob98ca7ba345497fedec4e138a6ccb4510c3038678
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
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
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
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:
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.
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.
46 basedir=
47 datadir=
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
54 service_startup_timeout=900
56 # The following variables are only set for letting mysql.server find things.
58 # Set some defaults
59 pid_file=
60 server_pid_file=
61 use_mysqld_safe=1
62 user=mysql
63 if test -z "$basedir"
64 then
65 basedir=/usr
66 bindir=/usr/bin
67 if test -z "$datadir"
68 then
69 datadir=/usr/var
71 sbindir=/usr/sbin
72 libexecdir=/usr/libexec
73 else
74 bindir="$basedir/bin"
75 if test -z "$datadir"
76 then
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.)
85 datadir_set=
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
92 . $lsb_functions
93 else
94 log_success_msg()
96 echo " SUCCESS! $@"
98 log_failure_msg()
100 echo " ERROR! $@"
104 PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin
105 export PATH
107 mode=$1 # start or stop
108 shift
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' ;;
118 esac
120 parse_server_arguments() {
121 for arg do
122 case "$arg" in
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/^[^=]*=//'`
132 datadir_set=1
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;;
139 esac
140 done
143 parse_manager_arguments() {
144 for arg do
145 case "$arg" in
146 --pid-file=*) pid_file=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
147 --user=*) user=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
148 esac
149 done
152 wait_for_pid () {
154 while test $i -ne $service_startup_timeout ; do
155 sleep 1
156 case "$1" in
157 'created')
158 test -s $pid_file && i='' && break
159 kill -0 $2 || break # if the program goes away, stop waiting
161 'removed')
162 test ! -s $pid_file && i='' && break
165 echo "wait_for_pid () usage: wait_for_pid created|removed"
166 exit 1
168 esac
169 echo $echo_n ".$echo_c"
170 i=`expr $i + 1`
171 done
173 if test -z "$i" ; then
174 log_success_msg
175 return 0
176 else
177 log_failure_msg
178 return 1
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
185 then
186 print_defaults="./bin/my_print_defaults"
187 elif test -x $bindir/my_print_defaults
188 then
189 print_defaults="$bindir/my_print_defaults"
190 elif test -x $bindir/mysql_print_defaults
191 then
192 print_defaults="$bindir/mysql_print_defaults"
193 else
194 # Try to find basedir in /etc/my.cnf
195 conf=/etc/my.cnf
196 print_defaults=
197 if test -r $conf
198 then
199 subpat='^[^=]*basedir[^=]*=\(.*\)$'
200 dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
201 for d in $dirs
203 d=`echo $d | sed -e 's/[ ]//g'`
204 if test -x "$d/bin/my_print_defaults"
205 then
206 print_defaults="$d/bin/my_print_defaults"
207 break
209 if test -x "$d/bin/mysql_print_defaults"
210 then
211 print_defaults="$d/bin/mysql_print_defaults"
212 break
214 done
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
226 extra_args=""
227 if test -r "$basedir/my.cnf"
228 then
229 extra_args="-e $basedir/my.cnf"
230 else
231 if test -r "$datadir/my.cnf"
232 then
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"
246 then
247 pid_file=$datadir/mysqlmanager-`/usr/bin/hostname`.pid
248 else
249 case "$pid_file" in
250 /* ) ;;
251 * ) pid_file="$datadir/$pid_file" ;;
252 esac
254 if test -z "$server_pid_file"
255 then
256 server_pid_file=$datadir/`/usr/bin/hostname`.pid
257 else
258 case "$server_pid_file" in
259 /* ) ;;
260 * ) server_pid_file="$datadir/$server_pid_file" ;;
261 esac
264 case "$mode" in
265 'start')
266 # Start daemon
268 # Safeguard (relative paths, core dumps..)
269 cd $basedir
271 manager=$bindir/mysqlmanager
272 if test -x $libexecdir/mysqlmanager
273 then
274 manager=$libexecdir/mysqlmanager
275 elif test -x $sbindir/mysqlmanager
276 then
277 manager=$sbindir/mysqlmanager
280 echo $echo_n "Starting MySQL"
281 if test -x $manager -a "$use_mysqld_safe" = "0"
282 then
283 if test -n "$other_args"
284 then
285 log_failure_msg "MySQL manager does not support options '$other_args'"
286 exit 1
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 \
292 --user="$user" \
293 --pid-file="$pid_file" >/dev/null 2>&1 &
294 "$manager" \
295 --mysqld-safe-compatible \
296 --user="$user" \
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
302 then
303 touch /var/lock/subsys/mysqlmanager
305 exit $return_value
306 elif test -x $bindir/mysqld_safe
307 then
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
317 then
318 touch /var/lock/subsys/mysql
320 exit $return_value
321 else
322 log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)"
326 'stop')
327 # Stop daemon. We use a signal here to avoid having to know the
328 # root password.
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"
335 then
336 pid_file=$server_pid_file
337 lock_dir=/var/lock/subsys/mysql
340 if test -s "$pid_file"
341 then
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
349 if test -f $lock_dir
350 then
351 rm -f $lock_dir
353 exit $return_value
354 else
355 log_failure_msg "MySQL manager or server PID file could not be found!"
359 'restart')
360 # Stop the service and regardless of whether it was
361 # running or not, start it again.
362 if $0 stop $other_args; then
363 $0 start $other_args
364 else
365 log_failure_msg "Failed to stop running server, so refusing to try to start."
366 exit 1
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
375 else
376 log_failure_msg "MySQL PID file could not be found!"
377 exit 1
380 'status')
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)"
386 exit 0
387 else
388 log_failure_msg "MySQL is not running, but PID file exists"
389 exit 1
391 else
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
397 else
398 lockfile=/var/lock/subsys/mysql
400 if test -f $lockfile ; then
401 log_failure_msg "MySQL is not running, but lock exists"
402 exit 2
404 log_failure_msg "MySQL is not running"
405 exit 3
406 else
407 log_failure_msg "MySQL is running but PID file could not be found"
408 exit 4
413 # usage
414 echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"
415 exit 1
417 esac
419 exit 0