Skip to content
Latchkey

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.mysql with image: mysql:8.0.
  • Set MYSQL_ROOT_PASSWORD and MYSQL_DATABASE.
  • Map 3306:3306 and add a mysqladmin ping health check.
  • Connect over 127.0.0.1:3306 (not localhost, 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 test

Gotchas

  • Use 127.0.0.1 instead of localhost; some clients read localhost as a Unix socket that does not exist on the runner.
  • MySQL prints ready for connections twice during init; the health check, not the log, is the reliable readiness signal.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →