blob: 6d310872979937fc9f08cad36dcc37dd25035050 [file] [log] [blame]
Ales Komarek90da78d2015-04-30 16:01:11 +02001#!/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 MySQL 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)
25CONF=/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
29ERR_LOGGER="logger -p daemon.err -t /etc/init.d/mysql -i"
30
31# Safeguard (relative paths, core dumps..)
32cd /
33umask 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.
38export HOME=/etc/mysql/
39
40## Fetch a particular option from mysql's invocation.
41#
42# Usage: void mysqld_get_param option
43mysqld_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# Determine parameters once per script invocation
52datadir=`mysqld_get_param datadir`
53[ -z "$datadir" ] && datadir="/var/lib/mysql"
54pidfile=`mysqld_get_param pid_file`
55#[ -z "$pidfile" ] && pidfile="$datadir/$(hostname).pid"
56
57#JPavlik tcp cloud fix for init script
58pidfile="/var/lib/mysql/mysqld.pid"
59## Do some sanity checks before even trying to start mysqld.
60sanity_checks() {
61 # check for config file
62 if [ ! -r /etc/mysql/my.cnf ]; then
63 log_warning_msg "$0: WARNING: /etc/mysql/my.cnf cannot be read. See README.Debian.gz"
64 echo "WARNING: /etc/mysql/my.cnf cannot be read. See README.Debian.gz" | $ERR_LOGGER
65 fi
66
67 # check for diskspace shortage
68 if LC_ALL=C BLOCKSIZE= df --portability $datadir/. | tail -n 1 | awk '{ exit ($4>4096) }'; then
69 log_failure_msg "$0: ERROR: The partition with $datadir is too full!"
70 echo "ERROR: The partition with $datadir is too full!" | $ERR_LOGGER
71 exit 1
72 fi
73}
74
75## Checks if there is a server running and if so if it is accessible.
76#
77# check_alive insists on a pingable server
78# check_dead also fails if there is a lost mysqld in the process list
79#
80# Usage: boolean mysqld_status [check_alive|check_dead] [warn|nowarn]
81mysqld_status() {
82# ping_output=`$MYADMIN ping 2>&1`; ping_alive=$(( ! $? ))
83
84 ps_alive=0
85 if [ -f "$pidfile" ] && ps `cat $pidfile` >/dev/null 2>&1; then ps_alive=1; fi
86 if [ "$1" = "check_alive" -a $ps_alive = 1 ] ||
87 [ "$1" = "check_dead" -a $ps_alive = 0 ]; then
88 return 0 # EXIT_SUCCESS
89 else
90 if [ "$2" = "warn" ]; then
91# echo -e "$ps_alive processes alive and '$MYADMIN ping' resulted in\n$ping_output\n" | $ERR_LOGGER -p daemon.debug
92 echo -e "$ps_alive processes alive\n" | $ERR_LOGGER -p daemon.debug
93 fi
94 return 1 # EXIT_FAILURE
95 fi
96}
97
98
99#
100# main()
101#
102
103cmd=${1:-''}
104[ $# -ge 1 ] && shift
105other_args="$*"
106
107case "$cmd" in
108 'start')
109 sanity_checks;
110 # Start daemon
111 log_daemon_msg "Starting MySQL database server" "mysqld"
112 if mysqld_status check_alive nowarn; then
113 log_progress_msg "already running"
114 log_end_msg 0
115 else
116 # Could be removed during boot
117 test -e /var/run/mysqld || install -m 755 -o mysql -g root -d /var/run/mysqld
118
119 # Check for additional wsrep options
120 WSREP_OPTS=${WSREP_OPTS:-""}
121 WSREP_PROVIDER=${WSREP_PROVIDER:-""}
122 WSREP_CLUSTER_ADDRESS=${WSREP_CLUSTER_ADDRESS:-""}
123 test -n "$WSREP_PROVIDER" && \
124 WSREP_OPTS="$WSREP_OPTS --wsrep_provider=$WSREP_PROVIDER"
125 test -n "$WSREP_CLUSTER_ADDRESS" && \
126 WSREP_OPTS="$WSREP_OPTS --wsrep_cluster_address=$WSREP_CLUSTER_ADDRESS"
127
128 # Start MySQL!.
129 /usr/bin/mysqld_safe $WSREP_OPTS $other_args > /dev/null 2>&1 &
130
131 # 6s was reported in #352070 to be too few when using ndbcluster
132 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14; do
133 sleep 1
134 if mysqld_status check_alive nowarn ; then break; fi
135 log_progress_msg "."
136 done
137 if mysqld_status check_alive warn; then
138 log_end_msg 0
139 # Now start mysqlcheck or whatever the admin wants.
140# output=$(/etc/mysql/debian-start)
141# [ -n "$output" ] && log_action_msg "$output"
142 else
143 log_end_msg 1
144 log_failure_msg "Please take a look at the syslog"
145 fi
146 fi
147 ;;
148
149 'stop')
150 # * As a passwordless mysqladmin (e.g. via ~/.my.cnf) must be possible
151 # at least for cron, we can rely on it here, too. (although we have
152 # to specify it explicit as e.g. sudo environments points to the normal
153 # users home and not /root)
154 log_daemon_msg "Stopping MySQL database server" "mysqld"
155 if ! mysqld_status check_dead nowarn; then
156 log_daemon_msg "Killing MySQL database server by signal" "mysqld"
157 pid=$(cat $pidfile || echo 0)
158 if [ $pid -eq 0 ]; then
159 log_failure_msg "Failed to get MySQL server pid"
160 exit 1
161 fi
162 kill -15 $pid
163 server_down=
164 for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15; do
165 log_progress_msg "."
166 sleep 1
167 if mysqld_status check_dead nowarn; then server_down=1; break; fi
168 done
169 fi
170
171 if ! mysqld_status check_dead warn; then
172 log_end_msg 1
173 log_failure_msg "Please stop MySQL manually and read /usr/share/doc/mysql-server-5.1/README.Debian.gz!"
174 exit -1
175 else
176 log_end_msg 0
177 fi
178 ;;
179
180 'restart')
181 set +e; $SELF stop; set -e
182 $SELF start $other_args
183 ;;
184
185 'reload'|'force-reload')
186 log_daemon_msg "Reloading MySQL database server" "mysqld"
187 $MYADMIN reload
188 log_end_msg 0
189 ;;
190
191 'status')
192 if mysqld_status check_alive nowarn; then
193# log_action_msg "$($MYADMIN version)"
194 log_action_msg "MySQL is running (PID: $(cat $pidfile))"
195 else
196 log_action_msg "MySQL is stopped."
197 exit 3
198 fi
199 ;;
200
201 *)
202 echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
203 exit 1
204 ;;
205esac
206