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/ Stop MySQL Community Server daemon
12# Description:       This service script facilitates startup and shutdown of
13#                    mysqld daemon throught its wrapper script mysqld_safe
14### END INIT INFO
15#
16
17# Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
18#
19# This program is free software; you can redistribute it and/or modify
20# it under the terms of the GNU General Public License as published by
21# the Free Software Foundation; version 2 of the License.
22#
23# This program is distributed in the hope that it will be useful,
24# but WITHOUT ANY WARRANTY; without even the implied warranty of
25# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26# GNU General Public License for more details.
27#
28# You should have received a copy of the GNU General Public License
29# along with this program; if not, write to the Free Software
30# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
31
32. /lib/lsb/init-functions
33. /usr/share/mysql/mysql-helpers
34
35cd /
36umask 077
37
38# In case server is taking more to start or stop increase the timeout in defaults file
39STARTTIMEOUT=120
40STOPTIMEOUT=600
41[ -e /etc/default/mysql ] && . /etc/default/mysql || true
42
43VERSION=$(mysqld --version | grep mysqld | cut -d' ' -f4)
44
45get_mysql_option() {
46	RESULT=$(my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1)
47	if [ -z "$RESULT" ];
48	then
49		RESULT="$3"
50	fi
51	echo $RESULT
52}
53
54fix_thp_setting() {
55	THP_SETTING=$(get_mysql_option mysqld_safe thp-setting "")
56	if [ ! -z "${THP_SETTING}" ]; then
57	# Set whatever option is specified in config file
58		echo "${THP_SETTING}" > /sys/kernel/mm/transparent_hugepage/defrag
59		echo "${THP_SETTING}" > /sys/kernel/mm/transparent_hugepage/enabled
60	fi
61}
62
63check_exit_status() {
64        errcode=$1
65        if [ $errcode -gt 0 ];
66        then
67                exit $errcode
68        fi
69}
70
71get_running () {
72	PIDFILE=$(get_mysql_option mysqld_safe pid-file "")
73	MYSQLDATA=$(get_mysql_option mysqld datadir "/var/lib/mysql")
74	if [ -z "$PIDFILE" ];
75	then
76		PIDFILE=$(get_mysql_option mysqld pid-file "$MYSQLDATA/$(hostname).pid")
77	fi
78	case $PIDFILE in
79		/*) ;;
80		*) PIDFILE="$MYSQLDATA/$PIDFILE" ;;
81	esac
82	if [ -e "$PIDFILE" ] && [ -d "/proc/$(cat "$PIDFILE")" ];
83	then
84		echo 1
85	else
86		echo 0
87	fi
88}
89
90verify_server () {
91	TIMEOUT=0
92	if [ "${1}" = "start" ];
93	then
94		TIMEOUT=${STARTTIMEOUT}
95	elif [ "${1}" = "stop" ];
96	then
97		TIMEOUT=${STOPTIMEOUT}
98	fi
99
100	COUNT=0
101	while [ ${COUNT} -lt ${TIMEOUT} ];
102	do
103		COUNT=$(( COUNT+1 ))
104		echo -n .
105		if [ "${1}" = "start" ] && [ "$(get_running)" = 1 ];
106		then
107			if [ -z ${2} ];
108			then
109				echo
110			fi
111			return 0
112		fi
113		if [ "${1}" = "stop" ] && [ "$(get_running)" = 0 ];
114		then
115			if [ -z ${2} ];
116			then
117				echo
118			fi
119			return 0
120		fi
121		sleep 1
122	done
123	return 1
124}
125
126case "$1" in
127  'start')
128	if [ "$(get_running)" -eq 1 ];
129	then
130		log_action_msg "Percona Server is already started"
131	else
132		MYSQLRUN=/var/run/mysqld
133		MYSQLDATA=$(get_mysql_option mysqld datadir "/var/lib/mysql")
134		MYSQLFILES=/var/lib/mysql-files
135		MYSQLLOG=/var/log/mysql
136		MYSQLKEYRING=/var/lib/mysql-keyring
137
138		if [ ! -d ${MYSQLDATA} -a ! -L ${MYSQLDATA} ];
139		then
140                        install -d -m 0750 -omysql -gmysql ${MYSQLDATA}
141                        check_exit_status $?
142		fi
143
144		if [ ! -d ${MYSQLFILES} -a ! -L ${MYSQLFILES} ];
145		then
146                        install -d -m 0770 -omysql -gmysql ${MYSQLFILES}
147                        check_exit_status $?
148		fi
149
150		if [ ! -d ${MYSQLKEYRING} -a ! -L ${MYSQLKEYRING} ];
151		then
152                        install -d -m 0750 -omysql -gmysql ${MYSQLKEYRING}
153                        check_exit_status $?
154		fi
155
156		if [ ! "$(ls -A ${MYSQLDATA})" ] && [ -d ${MYSQLFILES} ] && [ -d ${MYSQLKEYRING} ];
157		then
158			su - mysql -s /bin/bash -c "/usr/sbin/mysqld --initialize-insecure=on 2>&1 > /dev/null"
159                        check_exit_status $?
160			su - mysql -s /bin/bash -c "/usr/sbin/mysqld --log_error_verbosity=2 2>&1 > /dev/null &"
161                        check_exit_status $?
162			verify_server start no-newline
163			mysql -e "INSTALL PLUGIN auth_socket SONAME 'auth_socket.so'"
164			mysql -e "USE mysql; UPDATE user SET plugin='auth_socket' WHERE user='root'"
165			mysqladmin shutdown
166		fi
167
168		if [ -x /usr/bin/mysql_ssl_rsa_setup -a ! -e "${MYSQLDATA}/server-key.pem" ];
169		then
170			/usr/bin/mysql_ssl_rsa_setup --datadir="${MYSQLDATA}" --uid=mysql >/dev/null 2>&1
171		fi
172
173		if [ ! -d ${MYSQLLOG} -a ! -L ${MYSQLLOG} ];
174		then
175                        install -d -m 0750 -omysql -gadm ${MYSQLLOG}
176                        check_exit_status $?
177                        install -m 0640 -omysql -gadm /dev/null ${MYSQLLOG}/error.log
178                        check_exit_status $?
179		fi
180
181		if [ ! -d "${MYSQLRUN}" -a ! -L "${MYSQLRUN}" ];
182		then
183                        install -d -m 0755 -omysql -gmysql ${MYSQLRUN}
184		fi
185
186		# Needed because of TokuDB
187		fix_thp_setting
188
189		su - mysql -s /bin/bash -c "mysqld_safe > /dev/null &"
190                check_exit_status $?
191		verify_server start
192                retval=$?
193		if [ "$retval" -eq 0 ];
194		then
195			log_action_msg "Percona Server ${VERSION} is started"
196		else
197			log_action_msg "Percona Server ${VERSION} did not start. Please check logs for more details."
198                        exit $retval
199		fi
200	fi
201	;;
202
203  'stop')
204	if [ "$(get_running)" -eq 1 ];
205	then
206		killall -u mysql
207		verify_server stop
208                retval=$?
209		if [ "$retval" -eq 0 ];
210		then
211			log_action_msg "Percona Server ${VERSION} is stopped"
212		else
213			log_action_msg "Attempt to shutdown Percona Server ${VERSION} timed out"
214                        exit $retval
215		fi
216	else
217		log_action_msg "Percona Server ${VERSION} is already stopped"
218	fi
219	;;
220
221  'restart'|'reload'|'force-reload')
222	log_action_msg "Stopping Percona Server ${VERSION}"
223	$0 stop
224	log_action_msg "Re-starting Percona Server ${VERSION}"
225	$0 start
226	;;
227
228  'status')
229	if [ "$(get_running)" -eq 1 ];
230	then
231		log_action_msg "Percona Server ${VERSION} is running"
232	else
233		log_action_msg "Percona Server ${VERSION} is not running"
234		exit 3
235	fi
236	;;
237
238  *)
239	echo "Usage: $SELF start|stop|restart|reload|force-reload|status"
240	exit 1
241	;;
242esac
243
244exit 0
245