ganapathi / rpms / mariadb

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