ganapathi / rpms / mariadb

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