1 |
#!/bin/sh |
2 |
|
3 |
# This script waits for mysqld to be ready to accept connections |
4 |
# (which can be many seconds or even minutes after launch, if there's |
5 |
# a lot of crash-recovery work to do). |
6 |
# Running this as ExecStartPost is useful so that services declared as |
7 |
# "After mysqld" won't be started until the database is really ready. |
8 |
|
9 |
# Service file passes us the daemon's PID |
10 |
daemon_pid="$1" |
11 |
|
12 |
# extract value of a MySQL option from config files |
13 |
# Usage: get_mysql_option SECTION VARNAME DEFAULT |
14 |
# result is returned in $result |
15 |
# We use my_print_defaults which prints all options from multiple files, |
16 |
# with the more specific ones later; hence take the last match. |
17 |
get_mysql_option(){ |
18 |
result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` |
19 |
if [ -z "$result" ]; then |
20 |
# not found, use default |
21 |
result="$3" |
22 |
fi |
23 |
} |
24 |
|
25 |
# Defaults here had better match what mysqld_safe will default to |
26 |
get_mysql_option mysqld datadir "/var/lib/mysql" |
27 |
datadir="$result" |
28 |
get_mysql_option mysqld socket "$datadir/mysql.sock" |
29 |
socketfile="$result" |
30 |
|
31 |
# Wait for the server to come up or for the mysqld process to disappear |
32 |
ret=0 |
33 |
while /bin/true; do |
34 |
RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` |
35 |
mret=$? |
36 |
if [ $mret -eq 0 ]; then |
37 |
break |
38 |
fi |
39 |
# exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected, |
40 |
# anything else suggests a configuration error |
41 |
if [ $mret -ne 1 -a $mret -ne 11 ]; then |
42 |
ret=1 |
43 |
break |
44 |
fi |
45 |
# "Access denied" also means the server is alive |
46 |
echo "$RESPONSE" | grep -q "Access denied for user" && break |
47 |
|
48 |
# Check process still exists |
49 |
if ! /bin/kill -0 $daemon_pid 2>/dev/null; then |
50 |
ret=1 |
51 |
break |
52 |
fi |
53 |
sleep 1 |
54 |
done |
55 |
|
56 |
exit $ret |