Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 12

github actions - cannot connect to mysql service

so I am trying to setup a laravel ci server with github actions. And, i am using `edbizarro/gitlab-ci-pipeline-php:7.3` as my container, and I use the official mariadb docker as mysql service.:

 

services:
mysql:
image: mariadb:10.3
env:
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
ports:
- 13306:3306 
...

and I am trying to run, run mysql command to import db:

- name: Setting Up DB
run: |
mysql -Tv -h mysql -P 13306 --protocol=tcp -u root -proot < ./storage/test.sql

And here are the several attempts I did:

| host | error message |
|--------------------|------------------------------------------------------------------------|
| mysql,"${DB_HOST}" | ERROR 2005 (HY000): Unknown MySQL server host 'mysql' (-2) |
| 0.0.0.0 | ERROR 2002 (HY000): Can't connect to MySQL server on '0.0.0.0' (115) |
| 127.0.0.1 | ERROR 2002 (HY000): Can't connect to MySQL server on '127.0.0.1' (115) |
| (without host) | ERROR 2002 (HY000): Can't connect to MySQL server on 'localhost' (99) |


Not sure what else I can do here ...

here is what I also tried, all tells me that **option don't exist**:

```
services:
mysql:
image: mariadb:10.3
options: --bind_address=""
env:
MYSQL_USER: root
MYSQL_PASSWORD: root
MYSQL_ROOT_PASSWORD: root
ports:
- 13306:3306
```

and I tried with options :

1. `--disable_bind_address`
2. `--bind-address=""`

Here are some log messages I got from github: (-Tv did not give anything useful)

```
docker.io/library/mariadb:10.3
/usr/bin/docker create --name 607ae471e5844570b9e2fcf4f57ed78a_mariadb103_d94369 --label 2b5be7 --workdir /__w/laravel/laravel --network github_network_ab8a7ba8c1624f95bdc2784147b4b5e1 --network-alias mysql -p 13306:3306 -e "MYSQL_USER=root" -e "MYSQL_PASSWORD=root" -e "MYSQL_ROOT_PASSWORD=root" -e "HOME=/github/home" -v "/home/runner/work":"/__w" -v "/home/runner/runners/2.157.3/externals":"/__e":ro -v "/home/runner/work/_temp":"/__w/_temp" -v "/home/runner/work/_actions":"/__w/_actions" -v "/opt/hostedtoolcache":"/__t" -v "/home/runner/work/_temp/_github_home":"/github/home" -v "/home/runner/work/_temp/_github_workflow":"/github/workflow" mariadb:10.3
c3b10a5c4799a671f55c2f2b7a33803abd9228830be5e8821b51f10a2b2951b3
/usr/bin/docker start c3b10a5c4799a671f55c2f2b7a33803abd9228830be5e8821b51f10a2b2951b3
c3b10a5c4799a671f55c2f2b7a33803abd9228830be5e8821b51f10a2b2951b3
/usr/bin/docker ps --all --filter id=c3b10a5c4799a671f55c2f2b7a33803abd9228830be5e8821b51f10a2b2951b3 --filter status=running --no-trunc --format "{{.ID}} {{.Status}}"
c3b10a5c4799a671f55c2f2b7a33803abd9228830be5e8821b51f10a2b2951b3 Up Less than a second
/usr/bin/docker port c3b10a5c4799a671f55c2f2b7a33803abd9228830be5e8821b51f10a2b2951b3
3306/tcp -> 0.0.0.0:13306
```

11 Replies
Highlighted
GitHub Staff
Message 2 of 12

Re: github actions - cannot connect to mysql service

Hi @starvsion.

 

Here's a couple samples for running MySQL. Hope you find this helpful.

jobs:
  test-host:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: password
        ports:
        - 3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    steps:
    - name: Verify MySQL connection from host
      run: |
        sudo apt-get install -y mysql-client
        mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports['3306'] }} -uroot -ppassword -e "SHOW DATABASES"
  test-container:
    runs-on: ubuntu-latest
    container: ubuntu
    services:
      mysql:
        image: mysql:5.7
        env:
          MYSQL_ROOT_PASSWORD: password
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
    steps:
    - name: Verify MySQL connection from container 
      run: |
        apt-get update
        apt-get install -y mysql-client
        mysql --host mysql -uroot -ppassword -e "SHOW DATABASES"

 

Highlighted
Pilot Lvl 1
Message 3 of 12

Re: github actions - cannot connect to mysql service

hey @mscoutermarsh 

 

Can you check this https://github.com/linuxjuggler/laravel-mysql-test/commit/6fa319eeb46c5691a7d0782e16c10e20319f7765/c...

 

I can confirm that we can connect to mysql but for some reason connecting to mysql is denied check https://github.com/linuxjuggler/laravel-mysql-test/runs/215079011#step:8:31

------------------------

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Highlighted
GitHub Staff
Message 4 of 12

Re: github actions - cannot connect to mysql service

Try connecting to 127.0.0.1

 

I forget the exact reason. Someone shared in Slack there is an issue with MySQL accepting connections at localhost.

Highlighted
Pilot Lvl 1
Message 5 of 12

Re: github actions - cannot connect to mysql service

Hey @mscoutermarsh thanks for the reply.

 

i just want to say that the connection is already to 127.0.0.1

 

this is the log that I referenced 

PDO::__construct("mysql:host=127.0.0.1;port=3306;dbname=laravel", "root", "password", [])

Illuminate\Database\Connectors\Connector::createPdoConnection("mysql:host=127.0.0.1;port=3306;dbname=laravel", "root", "password", [])

 

As you can see w are trying to connect to the IP not localhost

------------------------

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Highlighted
GitHub Staff
Message 6 of 12

Re: github actions - cannot connect to mysql service

But from the logs, it also looks like it's trying to connect on port 3306 instead of 13306.

 

Getting MySQL to use an actual IP connection instead of the unix socket is tricky, and MySQL isn't very explicit about how it's actually connecting.  Even though it says it's connecting to 127.0.0.1, it looks like it's actually using the mysql.sock file.

 

If you specify both the IP address and port explicitly, that should override that behavior.

 

mysql --host 127.0.0.1 --port 13306
Highlighted
Pilot Lvl 1
Message 7 of 12

Re: github actions - cannot connect to mysql service

Thanks @ethomson  and @mscoutermarsh 

 

I found the issue, MySQL will return a random port which I can use so instead of using 3306 the one I specified for MySQL, I should be using

 

 

${{ job.services.mysql.ports['3306'] }}

 

To get the random port that the service is using!! Which makes no sense to me at all.

 

Anyway, it works for now.

 

 

------------------------

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!

Highlighted
Copilot Lvl 2
Message 8 of 12

Re: github actions - cannot connect to mysql service

Maybe you can just use mysql that comes with the platform, for example

- name: Init Database
run: |
mysql -uroot -h127.0.0.1 -proot -e 'CREATE DATABASE IF NOT EXISTS db1;'
mysql -uroot -h127.0.0.1 -proot -e 'CREATE DATABASE IF NOT EXISTS db2;'

 

 https://help.github.com/en/articles/software-in-virtual-environments-for-github-actions 

 

image.png

Highlighted
Copilot Lvl 2
Message 9 of 12

Re: github actions - cannot connect to mysql service

Thanks ... Your answer works . And I realized that it wouldn't work in the job runs inside a container ... so ... 

Highlighted
GitHub Staff
Message 10 of 12

Re: github actions - cannot connect to mysql service

We expect to remove the mysql installation from the base image and recommend that you use a service container.