1
by brian
clean slate |
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 |
||
46 |
basedir= |
|
47 |
datadir= |
|
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
|
|
54 |
service_startup_timeout=900 |
|
55 |
||
56 |
# The following variables are only set for letting mysql.server find things.
|
|
57 |
||
58 |
# Set some defaults
|
|
59 |
pid_file= |
|
60 |
server_pid_file= |
|
61 |
use_mysqld_safe=1 |
|
62 |
user=@MYSQLD_USER@ |
|
63 |
if test -z "$basedir" |
|
64 |
then
|
|
65 |
basedir=@prefix@ |
|
66 |
bindir=@bindir@ |
|
67 |
if test -z "$datadir" |
|
68 |
then
|
|
69 |
datadir=@localstatedir@ |
|
70 |
fi
|
|
71 |
sbindir=@sbindir@ |
|
72 |
libexecdir=@libexecdir@ |
|
73 |
else
|
|
74 |
bindir="$basedir/bin" |
|
75 |
if test -z "$datadir" |
|
76 |
then
|
|
77 |
datadir="$basedir/data" |
|
78 |
fi
|
|
79 |
sbindir="$basedir/sbin" |
|
80 |
libexecdir="$basedir/libexec" |
|
81 |
fi
|
|
82 |
||
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= |
|
86 |
||
87 |
#
|
|
88 |
# Use LSB init script functions for printing messages, if possible
|
|
89 |
#
|
|
90 |
lsb_functions="/lib/lsb/init-functions" |
|
91 |
if test -f $lsb_functions ; then |
|
92 |
. $lsb_functions
|
|
93 |
else
|
|
94 |
log_success_msg()
|
|
95 |
{
|
|
96 |
echo " SUCCESS! $@" |
|
97 |
}
|
|
98 |
log_failure_msg()
|
|
99 |
{
|
|
100 |
echo " ERROR! $@" |
|
101 |
}
|
|
102 |
fi
|
|
103 |
||
104 |
PATH=/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin |
|
105 |
export PATH
|
|
106 |
||
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.
|
|
113 |
||
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
|
|
119 |
||
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" |
|
127 |
fi
|
|
128 |
sbindir="$basedir/sbin" |
|
129 |
libexecdir="$basedir/libexec" |
|
130 |
;;
|
|
131 |
--datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'` |
|
132 |
datadir_set=1 |
|
133 |
;;
|
|
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
|
|
141 |
}
|
|
142 |
||
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
|
|
150 |
}
|
|
151 |
||
152 |
wait_for_pid () { |
|
153 |
verb="$1" |
|
154 |
manager_pid="$2" # process ID of the program operating on the pid-file |
|
155 |
i=0 |
|
156 |
avoid_race_condition="by checking again" |
|
157 |
while test $i -ne $service_startup_timeout ; do |
|
158 |
||
159 |
case "$verb" in |
|
160 |
'created') |
|
161 |
# wait for a PID-file to pop into existence.
|
|
162 |
test -s $pid_file && i='' && break |
|
163 |
;;
|
|
164 |
'removed') |
|
165 |
# wait for this PID-file to disappear
|
|
166 |
test ! -s $pid_file && i='' && break |
|
167 |
;;
|
|
168 |
*)
|
|
169 |
echo "wait_for_pid () usage: wait_for_pid created|removed manager_pid" |
|
170 |
exit 1 |
|
171 |
;;
|
|
172 |
esac
|
|
173 |
||
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
|
|
178 |
else
|
|
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. |
|
183 |
fi
|
|
184 |
||
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. |
|
188 |
fi
|
|
189 |
fi
|
|
190 |
||
191 |
echo $echo_n ".$echo_c" |
|
192 |
i=`expr $i + 1` |
|
193 |
sleep 1
|
|
194 |
done
|
|
195 |
||
196 |
if test -z "$i" ; then |
|
197 |
log_success_msg |
|
198 |
return 0 |
|
199 |
else
|
|
200 |
log_failure_msg |
|
201 |
return 1 |
|
202 |
fi
|
|
203 |
}
|
|
204 |
||
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 |
|
208 |
then
|
|
209 |
print_defaults="./bin/my_print_defaults" |
|
210 |
elif test -x $bindir/my_print_defaults |
|
211 |
then
|
|
212 |
print_defaults="$bindir/my_print_defaults" |
|
213 |
elif test -x $bindir/mysql_print_defaults |
|
214 |
then
|
|
215 |
print_defaults="$bindir/mysql_print_defaults" |
|
216 |
else
|
|
217 |
# Try to find basedir in /etc/my.cnf
|
|
218 |
conf=/etc/my.cnf |
|
219 |
print_defaults= |
|
220 |
if test -r $conf |
|
221 |
then
|
|
222 |
subpat='^[^=]*basedir[^=]*=\(.*\)$' |
|
223 |
dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf` |
|
224 |
for d in $dirs |
|
225 |
do
|
|
226 |
d=`echo $d | sed -e 's/[ ]//g'` |
|
227 |
if test -x "$d/bin/my_print_defaults" |
|
228 |
then
|
|
229 |
print_defaults="$d/bin/my_print_defaults" |
|
230 |
break
|
|
231 |
fi
|
|
232 |
if test -x "$d/bin/mysql_print_defaults" |
|
233 |
then
|
|
234 |
print_defaults="$d/bin/mysql_print_defaults" |
|
235 |
break
|
|
236 |
fi
|
|
237 |
done
|
|
238 |
fi
|
|
239 |
||
240 |
# Hope it's in the PATH ... but I doubt it
|
|
241 |
test -z "$print_defaults" && print_defaults="my_print_defaults" |
|
242 |
fi
|
|
243 |
||
244 |
#
|
|
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
|
|
247 |
#
|
|
248 |
||
249 |
extra_args="" |
|
250 |
if test -r "$basedir/my.cnf" |
|
251 |
then
|
|
252 |
extra_args="-e $basedir/my.cnf" |
|
253 |
else
|
|
254 |
if test -r "$datadir/my.cnf" |
|
255 |
then
|
|
256 |
extra_args="-e $datadir/my.cnf" |
|
257 |
fi
|
|
258 |
fi
|
|
259 |
||
260 |
parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server` |
|
261 |
||
262 |
# Look for the pidfile
|
|
263 |
parse_manager_arguments `$print_defaults $extra_args manager` |
|
264 |
||
265 |
#
|
|
266 |
# Set pid file if not given
|
|
267 |
#
|
|
268 |
if test -z "$pid_file" |
|
269 |
then
|
|
270 |
pid_file=$datadir/mysqlmanager-`@HOSTNAME@`.pid |
|
271 |
else
|
|
272 |
case "$pid_file" in |
|
273 |
/* ) ;; |
|
274 |
* ) pid_file="$datadir/$pid_file" ;; |
|
275 |
esac
|
|
276 |
fi
|
|
277 |
if test -z "$server_pid_file" |
|
278 |
then
|
|
279 |
server_pid_file=$datadir/`@HOSTNAME@`.pid |
|
280 |
else
|
|
281 |
case "$server_pid_file" in |
|
282 |
/* ) ;; |
|
283 |
* ) server_pid_file="$datadir/$server_pid_file" ;; |
|
284 |
esac
|
|
285 |
fi
|
|
286 |
||
287 |
case "$mode" in |
|
288 |
'start') |
|
289 |
# Start daemon
|
|
290 |
||
291 |
# Safeguard (relative paths, core dumps..)
|
|
292 |
cd $basedir |
|
293 |
||
294 |
manager=$bindir/mysqlmanager |
|
295 |
if test -x $libexecdir/mysqlmanager |
|
296 |
then
|
|
297 |
manager=$libexecdir/mysqlmanager |
|
298 |
elif test -x $sbindir/mysqlmanager |
|
299 |
then
|
|
300 |
manager=$sbindir/mysqlmanager |
|
301 |
fi
|
|
302 |
||
303 |
echo $echo_n "Starting MySQL" |
|
304 |
if test -x $manager -a "$use_mysqld_safe" = "0" |
|
305 |
then
|
|
306 |
if test -n "$other_args" |
|
307 |
then
|
|
308 |
log_failure_msg "MySQL manager does not support options '$other_args'" |
|
309 |
exit 1 |
|
310 |
fi
|
|
311 |
# Give extra arguments to mysqld with the my.cnf file. This script may
|
|
312 |
# be overwritten at next upgrade.
|
|
313 |
"$manager" \ |
|
314 |
--mysqld-safe-compatible \
|
|
315 |
--user="$user" \ |
|
316 |
--pid-file="$pid_file" >/dev/null 2>&1 & |
|
317 |
wait_for_pid created $!; return_value=$? |
|
318 |
||
319 |
# Make lock for RedHat / SuSE
|
|
320 |
if test -w /var/lock/subsys |
|
321 |
then
|
|
322 |
touch /var/lock/subsys/mysqlmanager |
|
323 |
fi
|
|
324 |
exit $return_value |
|
325 |
elif test -x $bindir/mysqld_safe |
|
326 |
then
|
|
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=$? |
|
332 |
||
333 |
# Make lock for RedHat / SuSE
|
|
334 |
if test -w /var/lock/subsys |
|
335 |
then
|
|
336 |
touch /var/lock/subsys/mysql |
|
337 |
fi
|
|
338 |
exit $return_value |
|
339 |
else
|
|
340 |
log_failure_msg "Couldn't find MySQL manager ($manager) or server ($bindir/mysqld_safe)" |
|
341 |
fi
|
|
342 |
;;
|
|
343 |
||
344 |
'stop') |
|
345 |
# Stop daemon. We use a signal here to avoid having to know the
|
|
346 |
# root password.
|
|
347 |
||
348 |
# The RedHat / SuSE lock directory to remove
|
|
349 |
lock_dir=/var/lock/subsys/mysqlmanager |
|
350 |
||
351 |
# If the manager pid_file doesn't exist, try the server's
|
|
352 |
if test ! -s "$pid_file" |
|
353 |
then
|
|
354 |
pid_file=$server_pid_file |
|
355 |
lock_dir=/var/lock/subsys/mysql |
|
356 |
fi
|
|
357 |
||
358 |
if test -s "$pid_file" |
|
359 |
then
|
|
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=$? |
|
365 |
||
366 |
# delete lock for RedHat / SuSE
|
|
367 |
if test -f $lock_dir |
|
368 |
then
|
|
369 |
rm -f $lock_dir
|
|
370 |
fi
|
|
371 |
exit $return_value |
|
372 |
else
|
|
373 |
log_failure_msg "MySQL manager or server PID file could not be found!"
|
|
374 |
fi
|
|
375 |
;;
|
|
376 |
||
377 |
'restart') |
|
378 |
# Stop the service and regardless of whether it was
|
|
379 |
# running or not, start it again.
|
|
380 |
if $0 stop $other_args; then |
|
381 |
$0 start $other_args |
|
382 |
else
|
|
383 |
log_failure_msg "Failed to stop running server, so refusing to try to start."
|
|
384 |
exit 1 |
|
385 |
fi
|
|
386 |
;;
|
|
387 |
||
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
|
|
393 |
else
|
|
394 |
log_failure_msg "MySQL PID file could not be found!"
|
|
395 |
exit 1 |
|
396 |
fi
|
|
397 |
;;
|
|
398 |
'status') |
|
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)" |
|
404 |
exit 0 |
|
405 |
else
|
|
406 |
log_failure_msg "MySQL is not running, but PID file exists"
|
|
407 |
exit 1 |
|
408 |
fi
|
|
409 |
else
|
|
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 |
|
415 |
else
|
|
416 |
lockfile=/var/lock/subsys/mysql |
|
417 |
fi
|
|
418 |
if test -f $lockfile ; then |
|
419 |
log_failure_msg "MySQL is not running, but lock exists"
|
|
420 |
exit 2 |
|
421 |
fi
|
|
422 |
log_failure_msg "MySQL is not running"
|
|
423 |
exit 3 |
|
424 |
else
|
|
425 |
log_failure_msg "MySQL is running but PID file could not be found"
|
|
426 |
exit 4 |
|
427 |
fi
|
|
428 |
fi
|
|
429 |
;;
|
|
430 |
*)
|
|
431 |
# usage
|
|
432 |
echo "Usage: $0 {start|stop|restart|reload|force-reload|status} [ MySQL server options ]" |
|
433 |
exit 1 |
|
434 |
;;
|
|
435 |
esac
|
|
436 |
||
437 |
exit 0 |