On Windows 10, how do I configure my docker-compose to allow Python to connect to MySql 8?

238 views Asked by At

I'm using Docker 19. I have the following Docker container. Note there is a MySql container and a Python/Django container. The Python/Django container is meant to connect to the MySql one ...

services:
  mysql:
    restart: always
    image: mysql:8.0
    cap_add:
      - SYS_NICE  # CAP_SYS_NICE
    environment:
      MYSQL_DATABASE: 'directory_data'
      # So you don't have to use root, but you can if you like
      MYSQL_USER: 'root'
      # You can use whatever password you like
      MYSQL_PASSWORD: 'password'
      # Password for root access
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_ROOT_HOST: '%' 
    ports:
      - "3406:3306"
    volumes:
      - my-db:/var/lib/mysql
    command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci']

  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    env_file: .env
    environment:
      DEBUG: 'true'
    command: /usr/local/bin/gunicorn directory.wsgi:application --reload -w 2 -b :8000
    volumes:
    - ./web/:/app
    depends_on:
      - mysql

I have this .env file

DB_NAME=directory_data
DB_USER=root
DB_PASS=password
DB_SERVICE=mysql
DB_PORT=3306

In my Python setup, I have

DATABASES = {
    'default': {
        #'ENGINE': 'mysql.connector.django',
        'ENGINE': 'django.db.backends.mysql',
        'NAME': os.environ['DB_NAME'],
        'USER': os.environ['DB_USER'],
        'PASSWORD': os.environ['DB_PASS'],
        'HOST': os.environ['DB_SERVICE'],
        'PORT': os.environ['DB_PORT']
    }
}

However, when I run "docker-compose up" on Windows 10, I see the following error (oddly this doesn't happen on my Mac) ...

...
web_1     |   File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 598, in connect
web_1     |     self._get_server_information()
web_1     |   File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 975, in _get_server_information
web_1     |     packet = self._read_packet()
web_1     |   File "/usr/local/lib/python3.8/site-packages/pymysql/connections.py", line 684, in _read_packet
web_1     |     packet.check_error()
web_1     |   File "/usr/local/lib/python3.8/site-packages/pymysql/protocol.py", line 220, in check_error
web_1     |     err.raise_mysql_exception(self._data)
web_1     |   File "/usr/local/lib/python3.8/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
web_1     |     raise errorclass(errno, errval)
web_1     | django.db.utils.InternalError: (1130, "Host '172.23.0.3' is not allowed to connect to this MySQL server")

What else do I need to do to allow the Python container to connec to the MySql container?

0

There are 0 answers