Guest User

Untitled

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