skip to Main Content

Im trying to setup Github actions for a Laravel project but I cant seem to connect to the MySQL instance …It always gives me a error as follows;

SQLSTATE[HY000] [2002] Connection refused

I am pulling in a container to run my actions on top of the ubuntu-latest, everything else works except for the running of the test suite;

When starting the MySQL container it all seems to work and outputs the following;

mysql service is starting
mysql service is healthy.

My actions file is as follows;

name: Doing some tests n stuff

on: push

jobs:
    tests:
        runs-on: ubuntu-latest
        container: existenz/builder:8.0
        env:
            APP_ENV: testing
            DB_CONNECTION: mysql
            DB_USERNAME: laravel
            DB_DATABASE: laravel
            DB_HOST: 127.0.0.1
            DB_PORT: 3306
            BROADCAST_DRIVER: log
            CACHE_DRIVER: redis
            QUEUE_CONNECTION: redis
            SESSION_DRIVER: redis

        services:
            mysql:
                image: mysql:latest
                env:
                    MYSQL_ALLOW_EMPTY_PASSWORD: yes
                    MYSQL_USER: laravel
                    MYSQL_PASSWORD: password
                    MYSQL_DATABASE: laravel
                ports:
                    - 3306/tcp
                options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

            redis:
                image: redis
                ports:
                    - 6379/tcp
                options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3

        steps:
            - name: Checkout code
              uses: actions/checkout@v3

            - name: Install extensions
              run: apk -U --no-cache add php8-sodium php8-pdo_mysql

            - name: Install Composer dependencies
              run: composer install --prefer-dist --no-interaction

            - name: Create APP_KEY
              run: echo "APP_KEY=" > .env

            - name: Generate APP_KEY
              run: php artisan key:generate

            - name: Clear Config
              run: php artisan config:clear

            - name: Run Testsuite
              run: vendor/bin/phpunit
              env:
                DB_PORT: ${{ job.services.mysql.ports['3306'] }}
                REDIS_PORT: ${{ job.services.redis.ports['6379'] }}

            - name: Upload artifacts
              uses: actions/upload-artifact@master
              if: failure()
              with:
                name: Logs
                path: ./storage/logs

2

Answers


  1. It seems that your Laravel application is not able to connect to the MySQL instance.

    1. Docker Networking Issues.
    2. MySQL not ready yet: The "Connection refused" error can also mean that MySQL service is not ready yet when your tests are running. You can mitigate this by adding a delay before running tests or by using a wait-for-it script.
    3. Exposing Ports: Exposing ports in the services section of the action is not necessary unless you need to connect to the services from outside the job’s host (which is typically not the case). The services can connect to each other on their default ports without exposing them.

    Try to add following steps between Clean config and Run Test Suite:

      - name: Wait for MySQL to initialize
        run: sleep 15 # Adding a delay
    

    Please note that adding a delay with sleep 15 is not the most robust solution and it’s better to use a wait-for-it script to make sure MySQL is fully initialized before proceeding. But for a quick test, the delay could help to identify if the issue is related to timing.

    Login or Signup to reply.
  2. Your tests are running inside a container:

    jobs:
        tests:
            runs-on: ubuntu-latest
            container: existenz/builder:8.0
    

    And you have specified the DB server as:

    DB_HOST: 127.0.0.1
    

    Now, when GitHub Actions creates a service, it does it inside another container.
    If your tests try to access a DB server on 127.0.0.1, that will refer to the container where the tests are running, not to the one where the MySQL service is.

    When the service is created, GitHub Actions gives a network alias to that new container matching the name of the service. In your example, this is your service:

    services:
      mysql:
        image: mysql:latest
        ...
    

    So the container for the MySQL service has a mysql network alias.

    From the other container (the one that runs the tests), you can refer to the MySQL container by using mysql as a hostname.
    Therefore, what you need to do is changing your DB_HOST variable and set it to mysql.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search