You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.3 KiB

  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 $named $time
  8. # Should-Stop: $network $named $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 MariaDB 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/sbin/mysqld || 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 MariaDB 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 MariaDB!
  94. /usr/bin/mysqld_safe "${@:2}" > /dev/null 2>&1 &
  95. # 6s was reported in #352070 to be too few when using ndbcluster
  96. for i in $(seq 1 "${MYSQLD_STARTUP_TIMEOUT:-30}"); do
  97. sleep 1
  98. if mysqld_status check_alive nowarn ; then break; fi
  99. log_progress_msg "."
  100. done
  101. if mysqld_status check_alive warn; then
  102. log_end_msg 0
  103. # Now start mysqlcheck or whatever the admin wants.
  104. output=$(/etc/mysql/debian-start)
  105. [ -n "$output" ] && log_action_msg "$output"
  106. else
  107. log_end_msg 1
  108. log_failure_msg "Please take a look at the syslog"
  109. fi
  110. fi
  111. ;;
  112. 'stop')
  113. # * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
  114. # at least for cron, we can rely on it here, too. (although we have
  115. # to specify it explicit as e.g. sudo environments points to the normal
  116. # users home and not /root)
  117. log_daemon_msg "Stopping MariaDB database server" "mysqld"
  118. if ! mysqld_status check_dead nowarn; then
  119. set +e
  120. shutdown_out=`$MYADMIN shutdown 2>&1`; r=$?
  121. set -e
  122. if [ "$r" -ne 0 ]; then
  123. log_end_msg 1
  124. [ "$VERBOSE" != "no" ] && log_failure_msg "Error: $shutdown_out"
  125. log_daemon_msg "Killing MariaDB database server by signal" "mysqld"
  126. killall -15 mysqld
  127. server_down=
  128. for i in `seq 1 600`; do
  129. sleep 1
  130. if mysqld_status check_dead nowarn; then server_down=1; break; fi
  131. done
  132. if test -z "$server_down"; then killall -9 mysqld; fi
  133. fi
  134. fi
  135. if ! mysqld_status check_dead warn; then
  136. log_end_msg 1
  137. log_failure_msg "Please stop MariaDB manually and read /usr/share/doc/mariadb-server-10.0/README.Debian.gz!"
  138. exit -1
  139. else
  140. log_end_msg 0
  141. fi
  142. ;;
  143. 'restart')
  144. set +e; $SELF stop; set -e
  145. $SELF start
  146. ;;
  147. 'reload'|'force-reload')
  148. log_daemon_msg "Reloading MariaDB database server" "mysqld"
  149. $MYADMIN reload
  150. log_end_msg 0
  151. ;;
  152. 'status')
  153. if mysqld_status check_alive nowarn; then
  154. log_action_msg "$($MYADMIN version)"
  155. else
  156. log_action_msg "MariaDB is stopped."
  157. exit 3
  158. fi
  159. ;;
  160. *)
  161. echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
  162. exit 1
  163. ;;
  164. esac