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