Skip to content

Connection to MySql has failed: 2003: Can't connect to MySQL server on 'mysql1:3306' (-2 Name or service not known)

Summary

Can't connect to MySQL Service. I have installed my own runner on ubuntu server 20.04.

docker --version
Docker version 19.03.8, build afacb8b7f0

And the runner version is:

Version:      12.10.1
Git revision: ce065b93
Git branch:   12-10-stable
GO version:   go1.13.8
Built:        2020-04-22T21:29:52+0000
OS/Arch:      linux/amd64

Steps to reproduce

Here is the yml file:

variables:
  # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: "fuse"
  MYSQL_ROOT_PASSWORD: "fuse_root_password"

image: python:3.8.2
before_script:
  - python -V
  - pip install virtualenv

stages:
  - build
  - execute

Installing Requirements:
  stage: build
  services:
    - mysql:8.0.19
  script:
    - python -m virtualenv venv
    - source venv/bin/activate
    - pip install -r requirements.txt
  artifacts:
    paths:
      - venv/
    expire_in: 10 minutes

Running the Script:
  stage: execute
  script:
    - source venv/bin/activate
    - python src/hello-world-requirements.py
    - python -m unittest tests/test_db_gitlab.py

What is the current bug behavior?

python src/hello-world-requirements.py works fine. The tests python -m unittest tests/test_db_gitlab.py shows that it passed. But it also shows the following error:

Connection to MySql has failed: 2003: Can't connect to MySQL server on 'mysql1:3306' (-2 Name or service not known)

What is the expected correct behavior?

The tests should fail if it can't connect to MySQL.

Relevant logs and/or screenshots

Here is the complete output:

Running with gitlab-runner 12.10.1 (ce065b93)
  on ubuntuserverdockervm vmg67FpG
Preparing the "docker" executor
Using Docker executor with image python:3.8.2 ...
Pulling docker image python:3.8.2 ...
Using docker image sha256:4f7cd4269fa9900fe43f5c0db2267926ee972cac6cec74a92b9136e49f8b3489 for python:3.8.2 ...
Preparing environment
Running on runner-vmg67fpg-project-2-concurrent-0 via ubuntuserverdockervm...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /builds/fuse/fuse/.git/
Checking out 8fd080a7 as master...
Removing venv/

Skipping Git submodules setup
Restoring cache
Downloading artifacts
Downloading artifacts for Installing Requirements (72)...
Downloading artifacts from coordinator... ok        id=72 responseStatus=200 OK token=jXUHqBxx
WARNING: venv/bin/python: chmod venv/bin/python: no such file or directory (suppressing repeats) 
Running before_script and script
$ python -V
Python 3.8.2
$ pip install virtualenv
Collecting virtualenv
  Downloading virtualenv-20.0.18-py2.py3-none-any.whl (4.6 MB)
Collecting appdirs<2,>=1.4.3
  Downloading appdirs-1.4.3-py2.py3-none-any.whl (12 kB)
Collecting six<2,>=1.9.0
  Downloading six-1.14.0-py2.py3-none-any.whl (10 kB)
Collecting filelock<4,>=3.0.0
  Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting distlib<1,>=0.3.0
  Downloading distlib-0.3.0.zip (571 kB)
Building wheels for collected packages: distlib
  Building wheel for distlib (setup.py): started
  Building wheel for distlib (setup.py): finished with status 'done'
  Created wheel for distlib: filename=distlib-0.3.0-py3-none-any.whl size=340427 sha256=bdfd2ac97640c9918bb1980aafb764bbe5c8ddd99c7b58d205e1df599abcfd1d
  Stored in directory: /root/.cache/pip/wheels/eb/4e/d2/a903d4184fb49e4ac06474d65715b129aee13d69f7d227e78e
Successfully built distlib
Installing collected packages: appdirs, six, filelock, distlib, virtualenv
Successfully installed appdirs-1.4.3 distlib-0.3.0 filelock-3.0.12 six-1.14.0 virtualenv-20.0.18
$ source venv/bin/activate
$ python src/hello-world-requirements.py
exiting the program now...Good bye!

$ python -m unittest tests/test_db_gitlab.py
.Connection to MySql has failed: 2003: Can't connect to MySQL server on 'mysql1:3306' (-2 Name or service not known)

----------------------------------------------------------------------
Ran 1 test in 0.077s

OK
Running after_script
Saving cache
Uploading artifacts for successful job
Job succeeded

Possible fixes

Here is the test example:

class TestDb(TestCase):
    def setUp(self) -> None:
        self.my_db = Db(database="$MYSQL_DATABASE")
        self.my_db.host = "mysql"
        self.my_db.username = "root"
        self.my_db.passwd = "$MYSQL_ROOT_PASSWORD"

    def test_connect(self):
        try:
            self.my_db.connect()
        except Exception:
            self.fail("connect() raised Exception unexpectedly!")