vim /etc/init.d/mysqld

/etc/init.d 是一般开机的启动服务存放在这个目录下

/etc/rc.d启动的配置文件和脚本

/etc/init.d & /etc/rc.d/init.d是同一个目录,内容相同
实现机制,其实/etc/init.d是一个符号链接文件,链接指向/etc/rc.d/init.d
你运行以下命令可以看出来

[root@localhost ~]# ls -ld /etc/init.d
lrwxrwxrwx. 1 root root 11 Oct 2 12:15 /etc/init.d -> rc.d/init.d

建立方法是:ln -s /etc/rc.d/init.d /etc/init.d

[root@localhost ]# vim /etc/init.d/mysqld

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

posted @ 2014-10-05 15:45  chromebook  阅读(2677)  评论(0编辑  收藏  举报