Friday, 23 October 2026

Check MySQL Server readiness in Unit Test Setting up

#!/bin/bash

apk add mariadb-connector-c

//more codes here

//this while loop is to check Mysql server readiness
while ! mariadb-admin ping -h"$HOST" --protocol=tcp  --skip-ssl --silent; do
    sleep 1
done

//then do real stuff
 mysql --protocol=tcp  --skip-ssl -h$HOST -u$DB_USER -p$DB_PASS < ${ROOT_DIR}/library/WWAPI/recreate-schema.sql

Why needs to check server readiness

For MariaDB/MySQL, startup can include:

  • reading config
  • initializing storage engine internals
  • crash recovery / redo log replay
  • loading grant tables / auth plugins
  • opening sockets
  • replication setup (if any)

Why need "--protocol=tcp --skip-ssl"

those flags force a specific connection path that is often required in CI/unit-test environments.

--protocol=tcp Forces the client to connect over TCP/IP. Without it, mysql may try a Unix socket (on Linux/macOS) when host is localhost or certain defaults apply. If your test DB is in Docker/remote host/no local socket, socket connect fails.

--skip-ssl Forces a non-TLS connection. Without it, client/server may attempt SSL/TLS negotiation (depending on defaults and server settings). If certs/SSL config are missing or incompatible in the test environment, connection can fail.