K
K
KirSupi2022-04-07 01:49:43
Python
KirSupi, 2022-04-07 01:49:43

How to connect Python to MySQL in Docker?

There is a docker-compose file

version: '3.3'

services:
  mysql:
    image: mysql:8.0
    container_name: Mysql8
    restart: unless-stopped
    volumes:
      - ./docker/mysql/data:/var/lib/mysql
    networks:
      - app
    environment:
      ...
    ports:
        - 3306:3306
    
  api:
    image: ...
    container_name: API
    depends_on:
      - mysql
    restart: unless-stopped
    environment:
      - DB_HOST=Mysql8:3306
      ...
    networks:
      - app
    ports:
      - 3002:3002
  
  bot:
    image: ...
    container_name: Bot
    restart: unless-stopped
    depends_on:
      - mysql
    environment:
      - DB_HOST=Mysql8:3306
  ...
    networks:
      - app
    ports:
      - 3006:3006

networks:
  app:
    driver: bridge


There is an API in Golang, it easily connects to Mysql8, but the Bot gives the following on startup:
pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'Mysql8:3306' ([Errno -2] Name or service not known)")
MB is this a feature of PyMySQL?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Karabanov, 2022-04-07
@karabanov

He could not sober up the name Mysql8. Check if it fits /etc/hostsin the container.
I think it should help:

docker-compose down
docker-compose up -d

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question