Blame SOURCES/mysqld-wait-ready

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