I spent too much time trying to make MySQL service work on GitHub Actions and after hours of googling and configuration tests I’d like to share with you my findings.
If you come to this post from a Google search, you may have already read a ton of tutorials that have this code running MySQL inside your workflow:
services:
mysql:
image: mysql:5.7
env:
MYSQL_DATABASE: test_db
MYSQL_USER: user
MYSQL_PASSWORD: password
MYSQL_ROOT_PASSWORD: rootpassword
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3Code language: YAML (yaml)Some may have included additional information on why you need env inside the MySQL service, some may not. Basically, services have stopped automatically receiving all env values from the job-level scope, and you need to copy-paste them manually.
There might be several variations of the ports value.
- Here you will need to use
3306later in your workflow, but depending on your setup you may have port conflicts.
ports:
- 3306:3306Code language: YAML (yaml)- Here you redefine the local port to
33306. But in this case, you need to be able to redefine the port everywhere in your code.
ports:
- 33306:3306Code language: YAML (yaml)- Or even a bit more complicated solution, where later in your workflow steps you need to retrieve the port manually using
${{ job.services.mysql.ports['3306'] }}:
ports:
- 3306Code language: YAML (yaml)But in my experience, none of those ways worked and I was always receiving this error:
Failed to initialize, mysql service is unhealthy.
It looks like this:

It most likely means that a host (ubuntu-20.04) can’t connect to a separate MySQL container that is created using your services:mysql details due to port issues.
The easiest way to work with MySQL in GitHub Actions
Ever wondered why there are official PostgreSQL and Redis services guides available for you on docs.github.com, but not for MySQL?
That’s because MySQL 5.7 is pre-installed on Ubuntu 18.04 containers (and MySQL 8 on 20 and latest), but not running by default. Here is a related announcement by GitHub. That means you don’t need services:mysql at all, you just need to start the built-in mysql process.
I haven’t found this information in any official docs, but the default MySQL login and password are both root. Update: they have this information in their repository. Here is an example for ubuntu-20.04.
Below is the configuration code I crafted to make MySQL work on Ubuntu 18, 20 or 22:
env:
DB_DATABASE: test_db
DB_USER: root
DB_PASSWORD: root
steps:
- name: Set up MySQL
run: |
sudo /etc/init.d/mysql start
mysql -e 'CREATE DATABASE ${{ env.DB_DATABASE }};' -u${{ env.DB_USER }} -p${{ env.DB_PASSWORD }}
Code language: YAML (yaml)On Ubuntu 24.04 (see docs) you will need to slightly modify the first line in the commands section (thank you, Michael Hays, for the updated version – see his comment below):
sudo systemctl start mysql.serviceCode language: Bash (bash)The use of env variables is not mandatory, but I re-use those values in one of the workflow steps later so that’s why I put them separately.
Here is what you will see in the GitHub interface:

That warning:
Using a password on the command line interface can be insecure.
is valid when you operate with a live/staging database. In my case, I was using MySQL to run tests so it’s not really relevant.
I hope this post and the approach above will help you save hours of googling and debugging various MySQL errors while using the Ubuntu environment to run tests or do whatever you need.
Leave a Reply