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