mysql 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #!/bin/bash
  2. #
  3. ### BEGIN INIT INFO
  4. # Provides: mysql
  5. # Required-Start: $remote_fs $syslog
  6. # Required-Stop: $remote_fs $syslog
  7. # Should-Start: $network $time
  8. # Should-Stop: $network $time
  9. # Default-Start: 2 3 4 5
  10. # Default-Stop: 0 1 6
  11. # Short-Description: Start and stop the mysql database server daemon
  12. # Description: Controls the main MySQL database server daemon "mysqld"
  13. # and its wrapper script "mysqld_safe".
  14. ### END INIT INFO
  15. #
  16. set -e
  17. set -u
  18. ${DEBIAN_SCRIPT_DEBUG:+ set -v -x}
  19. test -x /usr/bin/mysqld_safe || exit 0
  20. . /lib/lsb/init-functions
  21. SELF=$(cd $(dirname $0); pwd -P)/$(basename $0)
  22. CONF=/etc/mysql/my.cnf
  23. MYADMIN="/usr/bin/mysqladmin --defaults-file=/etc/mysql/debian.cnf"
  24. # priority can be overriden and "-s" adds output to stderr
  25. ERR_LOGGER="logger -p daemon.err -t /etc/init.d/mysql -i"
  26. # Safeguard (relative paths, core dumps..)
  27. cd /
  28. umask 077
  29. # mysqladmin likes to read /root/.my.cnf. This is usually not what I want
  30. # as many admins e.g. only store a password without a username there and
  31. # so break my scripts.
  32. export HOME=/etc/mysql/
  33. ## Fetch a particular option from mysql's invocation.
  34. #
  35. # Usage: void mysqld_get_param option
  36. mysqld_get_param() {
  37. /usr/sbin/mysqld --print-defaults \
  38. | tr " " "\n" \
  39. | grep -- "--$1" \
  40. | tail -n 1 \
  41. | cut -d= -f2
  42. }
  43. ## Do some sanity checks before even trying to start mysqld.
  44. sanity_checks() {
  45. # check for config file
  46. if [ ! -r /etc/mysql/my.cnf ]; then
  47. log_warning_msg "$0: WARNING: /etc/mysql/my.cnf cannot be read. See README.Debian.gz"
  48. echo "WARNING: /etc/mysql/my.cnf cannot be read. See README.Debian.gz" | $ERR_LOGGER
  49. fi
  50. # check for diskspace shortage
  51. # datadir=`mysqld_get_param datadir`
  52. # if LC_ALL=C BLOCKSIZE= df --portability $datadir/. | tail -n 1 | awk '{ exit ($4>4096) }'; then
  53. # log_failure_msg "$0: ERROR: The partition with $datadir is too full!"
  54. # echo "ERROR: The partition with $datadir is too full!" | $ERR_LOGGER
  55. # exit 1
  56. # fi
  57. }
  58. ## Checks if there is a server running and if so if it is accessible.
  59. #
  60. # check_alive insists on a pingable server
  61. # check_dead also fails if there is a lost mysqld in the process list
  62. #
  63. # Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
  64. mysqld_status () {
  65. ping_output=`$MYADMIN ping 2>&1`; ping_alive=$(( ! $? ))
  66. ps_alive=0
  67. pidfile=`mysqld_get_param pid-file`
  68. if [ -f "$pidfile" ] && ps `cat $pidfile` >/dev/null 2>&1; then ps_alive=1; fi
  69. if [ "$1" = "check_alive" -a $ping_alive = 1 ] ||
  70. [ "$1" = "check_dead" -a $ping_alive = 0 -a $ps_alive = 0 ]; then
  71. return 0 # EXIT_SUCCESS
  72. else
  73. if [ "$2" = "warn" ]; then
  74. echo -e "$ps_alive processes alive and '$MYADMIN ping' resulted in\n$ping_output\n" | $ERR_LOGGER -p daemon.debug
  75. fi
  76. return 1 # EXIT_FAILURE
  77. fi
  78. }
  79. #
  80. # main()
  81. #
  82. case "${1:-''}" in
  83. 'start')
  84. sanity_checks;
  85. # Start daemon
  86. log_daemon_msg "Starting MySQL database server" "mysqld"
  87. if mysqld_status check_alive nowarn; then
  88. log_progress_msg "already running"
  89. log_end_msg 0
  90. else
  91. # Could be removed during boot
  92. test -e /var/run/mysqld || install -m 755 -o mysql -g root -d /var/run/mysqld
  93. # Start MySQL!
  94. su - mysql -s /bin/sh -c "/usr/bin/mysqld_safe > /dev/null 2>&1 &"
  95. # 6s was reported in #352070 to be too few when using ndbcluster
  96. # 14s was reported in #736452 to be too few with large installs
  97. for i in $(seq 1 30); do
  98. sleep 1
  99. if mysqld_status check_alive nowarn ; then break; fi
  100. log_progress_msg "."
  101. done
  102. if mysqld_status check_alive warn; then
  103. log_end_msg 0
  104. # Now start mysqlcheck or whatever the admin wants.
  105. output=$(/etc/mysql/debian-start)
  106. [ -n "$output" ] && log_action_msg "$output"
  107. else
  108. log_end_msg 1
  109. log_failure_msg "Please take a look at the syslog"
  110. fi
  111. fi
  112. ;;
  113. 'stop')
  114. # * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
  115. # at least for cron, we can rely on it here, too. (although we have
  116. # to specify it explicit as e.g. sudo environments points to the normal
  117. # users home and not /root)
  118. log_daemon_msg "Stopping MySQL database server" "mysqld"
  119. if ! mysqld_status check_dead nowarn; then
  120. set +e
  121. shutdown_out=`$MYADMIN shutdown 2>&1`; r=$?
  122. set -e
  123. if [ "$r" -ne 0 ]; then
  124. log_end_msg 1
  125. [ "$VERBOSE" != "no" ] && log_failure_msg "Error: $shutdown_out"
  126. log_daemon_msg "Killing MySQL database server by signal" "mysqld"
  127. killall -15 mysqld
  128. server_down=
  129. for i in 1 2 3 4 5 6 7 8 9 10; do
  130. sleep 1
  131. if mysqld_status check_dead nowarn; then server_down=1; break; fi
  132. done
  133. if test -z "$server_down"; then killall -9 mysqld; fi
  134. fi
  135. fi
  136. if ! mysqld_status check_dead warn; then
  137. log_end_msg 1
  138. log_failure_msg "Please stop MySQL manually and read /usr/share/doc/mysql-server-5.6/README.Debian.gz!"
  139. exit -1
  140. else
  141. log_end_msg 0
  142. fi
  143. ;;
  144. 'restart')
  145. set +e; $SELF stop; set -e
  146. $SELF start
  147. ;;
  148. 'reload'|'force-reload')
  149. log_daemon_msg "Reloading MySQL database server" "mysqld"
  150. $MYADMIN reload
  151. log_end_msg 0
  152. ;;
  153. 'status')
  154. if mysqld_status check_alive nowarn; then
  155. log_action_msg "$($MYADMIN version)"
  156. else
  157. log_action_msg "MySQL is stopped."
  158. exit 3
  159. fi
  160. ;;
  161. *)
  162. echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
  163. exit 1
  164. ;;
  165. esac