mysqladmin ping: Wait for MySQL in CI
mysqladmin ping exits 0 when a MySQL server is alive, so it gates migrations behind a ready database.
Like Postgres, a MySQL service container needs a moment before it accepts connections. mysqladmin ping reports whether the server is up and returns an exit code you can loop on before running migrations or tests.
What it does
mysqladmin ping sends a ping to the server and prints "mysqld is alive" when it responds, exiting 0. It exits non-zero when the server is unreachable. Note that during early startup MySQL may answer the ping with "Access denied" yet still return exit 0, because a response of any kind proves the server is accepting connections.
Common usage
# single readiness check
mysqladmin ping -h db -u app --password=secret
# wait loop in CI (bash)
until mysqladmin ping -h db -u app --password=secret --silent; do
echo "waiting for mysql..."; sleep 1
doneOptions
| Flag | What it does |
|---|---|
| ping | The subcommand that checks liveness |
| -h <host> | Host to check |
| -P <port> | Port to check (default 3306) |
| -u <user> | User to authenticate as |
| --password=<pw> | Password (or set MYSQL_PWD) |
| --silent | Suppress output, rely on the exit code |
In CI
Run a mysqladmin ping loop before any migration so you never hit ERROR 2002 from a server that is still initializing. Many CI systems accept mysqladmin ping as the service-container health check. Because a server that answers even with "Access denied" is technically ready, add a follow-up real query if you need to confirm credentials too.
Common errors in CI
"mysqladmin: connect to server at \"db\" failed error: \"Can't connect to MySQL server on \"db\" (111)\"" (connection refused) means the server is not up yet; keep looping. "mysqladmin: Ping to server failed" indicates the server did not answer. Seeing "mysqld is alive" immediately after container start can be premature if the init scripts have not finished; add a short real query to confirm the database exists.