Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mauritz-fhd's avatar

Connection refused in github runner/action

Hello, I'm trying to set up automated testing with github workflows. My problem is, that when I try to run php artisan migrate automatically via the workflow I get the error that the mysql connection is refused

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'todo' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

If I go into the bash manually and execute php artisan migrate or try to connect to the mysql service everything works as expected. My .env.ci config for the database is:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=password

and my .github/workflows/migration.yml:

on: push
name: CI
jobs:
  migration:
    runs-on: ubuntu-latest
    container:
      image: kirschbaumdevelopment/laravel-test-runner:8.2
  
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: test
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
 
    steps:
    - uses: actions/checkout@v4
 
    - name: Install composer dependencies
      run: |
        composer install --no-scripts
 
    - name: Prepare Laravel Application
      run: |
        cp .env.ci .env
        php artisan key:generate
    - run: ls
    - name: Run Migration
      run: php artisan migrate
0 likes
7 replies
enoch91's avatar

It seems like your workflow is attempting to run the migrations before the MySQL service is fully up and running,

First update the options in the mysql service by adding depends-on flag, to ensure that the Laravel application container only starts after the mysql service is ready.

options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 --depends-on=mysql

Next add one more step to your steps, just before the Run Migration step like so

- name: Wait for MySQL to be ready
      run: |
        until nc -zv ${{ job.services.mysql }} 3306; do echo "Waiting for MySQL to be ready..."; sleep 5; done
mauritz-fhd's avatar

@enoch91 Thanks for your answer, sadly nc is not available in the container but I tried sleep 60. Still the same result Manually connecting to the container and logging in and accessing the tables works without any problem

mysql -h mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'todo' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name;
Empty set (0.00 sec)
enoch91's avatar

@mauritz-fhd I recommend adding some debugging steps to gather more information about the state of the MySQL container and the network connections, helping you identify potential issues.

    - run: sleep 60 

    - name: Debugging Information
      run: |
        docker exec -it your_mysql_container_name_or_id netstat -tuln
        docker exec -it your_mysql_container_name_or_id ifconfig

Do this before your run migration step

mauritz-fhd's avatar

@enoch91 Thanks again for the reply! netstat nor ifconfig (net-tools) is installed on the mysql container and also apt nor apt-get is available.

What I think is weird is that I can migrate manually if I connect to the laravel/php container and execute the command. My connection attempts were also from the laravel container

mauritz-fhd's avatar
mauritz-fhd
OP
Best Answer
Level 1

My mistake was that I use checkout and my changes weren't commited nor pushed. 6 hours of debugging...

Valeri0's avatar

@mauritz-fhd I have the same problem. I didn't really understand the problem you found using checkout. Can you add some detail? Thank you for sharing.

mauritz-fhd's avatar

@Valeri0 I've used act runner to test my ci scripts locally. One step of the pipeline was to checkout a specific branch. I've pushed to the dev branch but only main would get checked out, so my correct commits weren't in the .github folder.

Please or to participate in this conversation.