How to Spin Up a MySQL Service for Tests in GitHub Actions
A MySQL service container starts before your steps; a mysqladmin ping health check keeps the job from racing the server.
Declare mysql under services:, set MYSQL_ROOT_PASSWORD and MYSQL_DATABASE, map port 3306, and gate readiness with mysqladmin ping.
Steps
- Add
services.mysqlwithimage: mysql:8.0. - Set
MYSQL_ROOT_PASSWORDandMYSQL_DATABASE. - Map
3306:3306and add amysqladmin pinghealth check. - Connect over
127.0.0.1:3306(notlocalhost, which may try a socket).
Workflow
.github/workflows/ci.yml
jobs:
test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: app_test
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -h 127.0.0.1 -uroot -proot"
--health-interval 10s
--health-timeout 5s
--health-retries 5
env:
DATABASE_URL: mysql://root:root@127.0.0.1:3306/app_test
steps:
- uses: actions/checkout@v4
- run: npm ci && npm testGotchas
- Use
127.0.0.1instead oflocalhost; some clients readlocalhostas a Unix socket that does not exist on the runner. - MySQL prints
ready for connectionstwice during init; the health check, not the log, is the reliable readiness signal.
Related guides
How to Spin Up a MariaDB Service for Tests in GitHub ActionsRun MariaDB as a GitHub Actions service container with a healthcheck.sh readiness probe so tests connect to a…
How to Spin Up a Postgres Service for Tests in GitHub ActionsRun a real Postgres alongside your GitHub Actions tests using a service container with a pg_isready health ch…
How to Wait for a Database to Be Ready in GitHub ActionsMake GitHub Actions wait until a database accepts connections using service health checks or a pg_isready ret…