Connecting to MySQL on localhost/127.0.0.1


#1

Hi there,

It seems that whatever I do, I can’t get either Python’s mysqlalchemy or mysql client to connect to a server.

I’m trying to use either a base Debian image or the mysql:5.7 image and I’m met with this error:

sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (2003, "Can't connect to MySQL server on '127.0.0.1' (111)") (Background on this error at: http://sqlalche.me/e/e3q8)

/root/.local/share/virtualenvs/api-2w_u5qGM/lib/python3.6/site-packages/MySQLdb/connections.py:204: OperationalError

I’ve also tried a custom Debian image with mysql installed.

I’d love any help with this as I’ve been trying for a few hours now and just can’t get it to work.

version: 2
jobs:
  build:
    docker:
      - image: circleci:python
        environment:
          - MYSQL_PASSWORD='test_pw'
          - MYSQL_DATABASE='test_db'
          - MYSQL_ROOT_HOST=%
          - DB_HOST=127.0.0.1
          - DB_PORT=3306
      - image: mysql:5.7
        environment:
          - MYSQL_PASSWORD=test_pw
          - MYSQL_DATABASE=loans
          - MYSQL_ROOT_HOST=%
          - DB_HOST=127.0.0.1
          - DB_PORT=3306
    steps:
      - checkout
      - run:
          name: Update pipenv
          command: |
          sudo pip3 install --upgrade pipenv
      - run:
          name: Install dependencies and run tests
          command: |
            mysql -u root -h 127.0.0.1 -ptest_pw
            cd api && pipenv --three && pipenv install
            cp config.template.yml test_config.yml
            pipenv run pytest
      - run:
          name: Setup front end
          command: |
            cd frontend
            yarn install
            yarn build
      - store_artifacts:
          path: test-reports
          destination: test-reports

#2

The first thing I would do is to get the Docker logs for the MySQL image to see if it started up (or the MySQL logs themselves). Also, see if waiting before the tests helps - I would guess the Python and MySQL containers are instantiated at the same time, and so a race condition can occur.


#3

Hi @halfer,

What’s the best way of getting the logs? Currently the only way I can seem to do this is have the build run on circleCI web interface (as opposed to the local CLI). This is irritating as I have to keep pushing broken builds.

For what it’s worth, I’m having no problem with the same docker containers locally (but outside circleCI).

Thanks.


#4

Hi again,

I’ve solved this by moving to postgres. Never figured out what was wrong but making this move just worked ‘out of the box’.

Thanks.


#5

I don’t exactly know, it’d need some research. The output in the Circle web app might be Docker log output (and is likely to be valuable) but other Docker logs may be possible. In any case, I’d perhaps start off with MySQL logs to see if the listener is starting up, and what in-container IP it is binding to.

Great! :smiley_cat:


#6

The first step is to fix the syntax of the configuration file. Those environment variables aren’t being listed correctly. They are suppose to be a map not a list. You can review the format here: https://circleci.com/docs/2.0/configuration-reference/#docker

After that, please provided an updated config here and we can continue to look at it and hopefully get you up and running. :slight_smile:


#7

Ah, I completely forgot, I have a working MySQL configuration here. It wasn’t easy to get working, but they were not Circle problems. The major one is MariaDB is not started by default, but there were others too.

I think the person I helped had given up with running MySQL in a separate container, but that ought to work, and it’d probably be easier to set up also. :slightly_smiling_face:


#8

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.