Unable to connect to MySQL server - Django & Github Actions

I’m trying to setup github actions for my django project. I am unable to connect to the service. django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)").

This is my workflow config.

name: Myapp CI

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  build:

    runs-on: ubuntu-latest
    
    services:
      mysql:
        image: mysql:5.7
        env:
            MYSQL_ALLOW_EMPTY_PASSWORD: yes
            MYSQL_DATABASE: myapp
        ports:
            - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    
    strategy:
      max-parallel: 4
      matrix:
        python-version: [3.8]

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v1
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements-dev.txt
    - name: Run Tests
      run: |
        python manage.py test

It’s been a whole day and I still couldn’t figure out what’s wrong with the MySQL setup. Can anyone help? Is there an official documentation on how to setup MySQL on github actions?

If you give only one port number the port number on the host (what you can reach at localhost/127.0.0.1/::1) is chosen randomly, as shown in the services example. Could that be the issue?

If you want the MySQL service to be available on 127.0.0.1:3306 your ports setting should look like this:

ports:
  - '3306:3306'
1 Like

:pleading_face: Thank you! I am new and learning and didn’t know about this. Spent almost a day trying!

1 Like