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  

Hi @starvsion.

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

jobs:
test-host:
runs-on:ubuntu-latestservices:
mysql:
image:mysql:5.7env:
MYSQL\_ROOT\_PASSWORD:passwordports:
        -3306options:--health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3steps:
    -name:Verify MySQL connection from hostrun:| 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-latestcontainer:ubuntuservices:
mysql:
image:mysql:5.7env:
MYSQL\_ROOT\_PASSWORD:passwordoptions:--health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5steps:
    -name:Verify MySQL connection from container run:| apt-get update apt-get install -y mysql-client mysql --host mysql -uroot -ppassword -e "SHOW DATABASES"
8 Likes

hey @mscoutermarsh 

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

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

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.

1 Like

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

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
1 Like

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.

1 Like

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 

1 Like

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

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

3 Likes

Is there any way to connect through localhost. I can’t use IP because the package that I use for connecting to DB uses only hostname. 

Thanks!

Is there any update on the timeline for this? This is affecting our ability to adopt github actions, and we’re concerned that if we make the conversion for the installed mysql, we’ll be forced to adapt again if it’s dropped

before you guys do that, could you provide a section in the docs giving an example, like PG or redis?  That way I wouldnt have to beat my head against trying to get it set up for hours like i have :( 

I banged my head on this for a bit (trying to get mysql working (but I also had failures w/ mariadb)) – as @ethomson alluded to – simply use mysql as a docker service, and have mysql target port and host (127.0.0.1).

services:mysql:image:mysql:5.7env:MYSQL\_ALLOW\_EMPTY\_PASSWORD:yesMYSQL\_DATABASE:testports:-13306:3306options:--health-cmd="mysqladminping"--health-interval=10s--health-timeout=5s--health-retries=3

-name:Executesql commandrun:mysql -u root -h 127.0.0.1 -P13306&nbsp;-e&nbsp;"blah"

ref:
https://freek.dev/1590-how-to-use-a-mysql-database-on-github-actions

This is basically what caused my own issues - mixing up the database that was already on the machine with the one I added.

Ended up just using the one Github provides - easy.

Formatting isn’t great but this snippet is DOPE! It helped A LOT.

Here is the formatted version:

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"
1 Like