Advertisement
Guest User

/etc/init.d/mysql

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