Python can't connect to local MySQL server

python
mysql

#1

i’m struggling to get python & mysql work together.

my current circle.yml looks like the following:

    version: 2
    jobs:
      build:
        working_directory: /tmp
        docker:
          - image: python:3.4.4
          - image: mysql:5.7
            environment:
              MYSQL_ALLOW_EMPTY_PASSWORD: yes
              MYSQL_USER: ubuntu
              MYSQL_ROOT_PASSWORD: ""
              MYSQL_DATABASE: circle_test
          - image: selenium/standalone-chrome:3.1.0

when trying to run the tests in tox, i’m getting the following error:

    django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
    ERROR: InvocationError: '/tmp/.tox/should_be_34/bin/python -Wall /tmp/admin_server/manage.py test /tmp/backend/'

After some researches, below are a list of other approaches (and combinations/mix-n-match) I’ve tried, but all resulted in the same error message:

approach 1:

    version: 2
    jobs:
      build:
        working_directory: /tmp
        docker:
          - image: python:3.4.4
            environment:
              TEST_DATABASE_URL: mysql://ubuntu@127.0.0.1:3306/circle_test?sslmode=disable
              DB_CONNECTION: mysql
              DB_HOST: 127.0.0.1
              DB_PORT: 3306
              DB_DATABASE: circle_test
              DB_USERNAME: ubuntu
          - image: mysql:5.7
            environment:
              MYSQL_ALLOW_EMPTY_PASSWORD: yes
              MYSQL_USER: ubuntu
              MYSQL_ROOT_PASSWORD: ""
              MYSQL_DATABASE: circle_test
          - image: selenium/standalone-chrome:3.1.0

approach 2:

    version: 2
    jobs:
      build:
        working_directory: /tmp
        docker:
          - image: python:3.4.4
            environment:
              TEST_DATABASE_URL: mysql://ubuntu@127.0.0.1:3306/circle_test?sslmode=disable
              DB_CONNECTION: mysql
              DB_HOST: 127.0.0.1
              DB_PORT: 3306
              DB_DATABASE: circle_test
              DB_USERNAME: ubuntu
              DB_PASSWORD: ubuntu
          - image: mysql:5.7
            environment:
              MYSQL_ALLOW_EMPTY_PASSWORD: yes
              MYSQL_USER: ubuntu
              MYSQL_ROOT_PASSWORD: ""
              MYSQL_DATABASE: circle_test
          - image: selenium/standalone-chrome:3.1.0

approach 3:

    version: 2
    jobs:
      build:
        working_directory: /tmp
        docker:
          - image: python:3.4.4
            environment:
              DB_CONNECTION: mysql
              DB_HOST: 127.0.0.1
              DB_PORT: 3306
              DB_DATABASE: circle_test
              DB_USERNAME: ubuntu
              DB_PASSWORD: ubuntu
          - image: mysql:5.7
            environment:
              MYSQL_USER: ubuntu
              MYSQL_DATABASE: circle_test
          - image: selenium/standalone-chrome:3.1.0

#2

You need to specify the host as 127.0.0.1 in your config


#3

Just to be clear, @rohara is specifically referring to your settings.py file for Django. Your error looks like its trying to connect on the socket instead of 127.0.0.1.

i.e its not using the DB_HOST: 127.0.0.1 ENVAR


#4

Thanks!

It worked after adding 127.0.0.1 to db host, and also able to simply the circle.yml to the following:

version: 2
jobs:
  build:
    working_directory: /tmp
    docker:
      - image: python:3.4.4
      - image: mysql:5.7
        environment:
          MYSQL_ROOT_HOST: "%"
          MYSQL_ROOT_PASSWORD: password
      - image: selenium/standalone-chrome:3.1.0

#5

This topic was automatically closed after 24 hours. New replies are no longer allowed.