42 lines
1.2 KiB
Bash
42 lines
1.2 KiB
Bash
#!/usr/bin/env sh
|
|
set -eu
|
|
|
|
# Maximum number of attempts (can be overridden via MAX_TRIES env var)
|
|
MAX_TRIES="${MAX_TRIES:-10}"
|
|
# Interval in seconds between attempts (can be overridden via INTERVAL env var)
|
|
INTERVAL="${INTERVAL:-5}"
|
|
|
|
try=0
|
|
|
|
echo "debug: $0";
|
|
|
|
while [ "$try" -lt "$MAX_TRIES" ]; do
|
|
try=$((try + 1))
|
|
echo "trying to reach mysql server (${WORDPRESS_DB_HOST:-${DB_HOST}}:${WORDPRESS_DB_PORT:-${DB_PORT:-3306}}) ${try}/${MAX_TRIES}...";
|
|
|
|
# Test MySQL server readiness using PHP (no mysql client or WP install required)
|
|
if php -r '
|
|
error_reporting(0);
|
|
$host = getenv("WORDPRESS_DB_HOST") ?: getenv("DB_HOST") ?: "mysql";
|
|
$user = getenv("WORDPRESS_DB_USER") ?: getenv("DB_USER") ?: "root";
|
|
$pass = getenv("WORDPRESS_DB_PASSWORD") ?: getenv("DB_PASSWORD") ?: "";
|
|
$port = getenv("WORDPRESS_DB_PORT") ?: getenv("DB_PORT") ?: 3306;
|
|
|
|
$mysqli = @new mysqli($host, $user, $pass, "", (int) $port);
|
|
|
|
if ($mysqli && !$mysqli->connect_errno) {
|
|
exit(0); // connection OK
|
|
}
|
|
exit(1); // connection failed
|
|
'; then
|
|
#echo "MySQL is ready, continuing..."
|
|
exit 0
|
|
fi
|
|
|
|
sleep "$INTERVAL"
|
|
done
|
|
|
|
DURATION=$((MAX_TRIES * INTERVAL))
|
|
echo "MySQL was not ready after ${DURATION}s" >&2
|
|
exit 1
|