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