7a5573f
#!/bin/sh
7a5573f
7a5573f
# This script waits for mysqld to be ready to accept connections
7a5573f
# (which can be many seconds or even minutes after launch, if there's
7a5573f
# a lot of crash-recovery work to do).
7a5573f
# Running this as ExecStartPost is useful so that services declared as
7a5573f
# "After mysqld" won't be started until the database is really ready.
7a5573f
7a5573f
# Service file passes us the daemon's PID (actually, mysqld_safe's PID)
7a5573f
daemon_pid="$1"
7a5573f
7a5573f
# extract value of a MySQL option from config files
7a5573f
# Usage: get_mysql_option SECTION VARNAME DEFAULT
7a5573f
# result is returned in $result
7a5573f
# We use my_print_defaults which prints all options from multiple files,
7a5573f
# with the more specific ones later; hence take the last match.
7a5573f
get_mysql_option(){
7a5573f
	result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1`
7a5573f
	if [ -z "$result" ]; then
7a5573f
	    # not found, use default
7a5573f
	    result="$3"
7a5573f
	fi
7a5573f
}
7a5573f
7a5573f
# Defaults here had better match what mysqld_safe will default to
7a5573f
get_mysql_option mysqld datadir "/var/lib/mysql"
7a5573f
datadir="$result"
7a5573f
get_mysql_option mysqld socket "/var/lib/mysql/mysql.sock"
7a5573f
socketfile="$result"
7a5573f
7a5573f
# Wait for the server to come up or for the mysqld process to disappear
7a5573f
ret=0
7a5573f
while /bin/true; do
7a5573f
	RESPONSE=`/usr/bin/mysqladmin --no-defaults --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1`
7a5573f
	mret=$?
7a5573f
	if [ $mret -eq 0 ]; then
7a5573f
	    break
7a5573f
	fi
7a5573f
	# exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected,
7a5573f
	# anything else suggests a configuration error
7a5573f
	if [ $mret -ne 1 -a $mret -ne 11 ]; then
7a5573f
	    ret=1
7a5573f
	    break
7a5573f
	fi
7a5573f
	# "Access denied" also means the server is alive
7a5573f
	echo "$RESPONSE" | grep -q "Access denied for user" && break
7a5573f
7a5573f
	# Check process still exists
7a5573f
	if ! /bin/kill -0 $daemon_pid 2>/dev/null; then
7a5573f
	    ret=1
7a5573f
	    break
7a5573f
	fi
7a5573f
	sleep 1
7a5573f
done
7a5573f
7a5573f
exit $ret