简体   繁体   English

docker和python上的未知mysql服务器主机

[英]Unknown mysql server host on docker and python

I'm building an API that fetches data from a MySQL database using Docker.我正在构建一个使用 Docker 从 MySQL 数据库中获取数据的 API。 I've tried everything and I always get this error: 2005 (HY000): Unknown MySQL server host 'db' (-3) .我已经尝试了所有方法,但总是收到此错误: 2005 (HY000): Unknown MySQL server host 'db' (-3) Here is my docker compose file:这是我的码头工人撰写文件:

version: '3'
services:
  web:
    container_name: nginx
    image: nginx
    volumes:
      - ./nginx/nginx.conf:/tmp/nginx.conf
    environment: 
      - FLASK_SERVER_ADDR=backend:9091
      - DB_PASSWORD=password
      - DB_USER=user
      - DB_HOST=db 
    command: /bin/bash -c "envsubst < /tmp/nginx.conf > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'" 
    ports:
      - 80:80
    networks:
      - local
    depends_on:
      - backend
    
  backend:
    container_name: app
    build: flask
    environment:
      - FLASK_SERVER_PORT=9091
      - DB_PASSWORD=password
    volumes:
      - flask:/tmp/app_data
    restart: unless-stopped
    networks:
      - local
    depends_on:
      - db
    links:
      - db

  db:
    container_name: db
    image: mysql
    restart: unless-stopped
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
    environment:
       - MYSQL_ROOT_PASSWORD=password
       - MYSQL_DATABASE=database
       - MYSQL_USER=user
       - MYSQL_PASSWORD=password
    ports:
      - 3306:3306

networks:
  local:
      
volumes:
  flask:
    driver: local
  db:
    driver: local

Inside the flask directory I have my Dockerfile like so:在烧瓶目录中,我的 Dockerfile 如下所示:

FROM ubuntu:latest
WORKDIR /src
RUN apt -y update
RUN apt -y upgrade
RUN apt install -y python3
RUN apt install -y python3-pip
COPY . .
RUN chmod +x -R .
RUN pip install -r requirements.txt --no-cache-dir
CMD ["python3","app.py"]

Finally, on my app.py file I try to connect to the database with the name of the Docker container.最后,在我的 app.py 文件中,我尝试使用 Docker 容器的名称连接到数据库。 I have tried using localhost and it still gives me the same error.我尝试过使用localhost ,但它仍然给我同样的错误。 This is the part of the code I use to access it:这是我用来访问它的代码的一部分:

conn = mysql.connector.connect(
              host="db",
              port=3306,
              user="user",
              password="password",
              database="database")

What is it that I'm doing wrong?我做错了什么?

The containers aren't on the same networks: , which could be why you're having trouble.容器不在同一个networks: ,这可能就是您遇到问题的原因。

I'd recommend deleting all of the networks: blocks in the file, both the blocks at the top level and the blocks in the web and backend containers.我建议删除所有networks:文件中的块,顶层的块以及webbackend容器中的块。 Compose will create a network named default for you and attach all of the containers to that network. Compose 将为您创建一个名为default的网络,并将所有容器附加到该网络。 Networking in Compose in the Docker documentation has more details on this setup. Docker 文档中的 Compose 中的网络对此设置有更多详细信息。

The links: block is related to an obsolete Docker networking mode, and I've seen it implicated in problems in other SO questions. links:块与过时的 Docker 网络模式有关,我已经看到它与其他 SO 问题中的问题有关。 You should remove it as well.您也应该删除它。

You also do not need to manually specify container_name: in most cases.在大多数情况下,您也不需要手动指定container_name: :。 For the Nginx container, the Docker Hub nginx image already knows how to do the envsubst processing so you do not need to override its command: .对于 Nginx 容器, Docker Hub nginx映像已经知道如何进行envsubst处理,因此您无需覆盖其command: .

This should leave you with:这应该给你留下:

version: '3.8'
services:
  web:
    image: nginx
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/templates/default.conf.template
    environment: { ... }
    ports:
      - 80:80
    depends_on:
      - backend
    
  backend:
    build: flask
    environment: { ... }
    volumes:
      - flask:/tmp/app_data
    restart: unless-stopped
    depends_on:
      - db

  db:
    image: mysql
    restart: unless-stopped
    volumes:
      - ./mysql:/docker-entrypoint-initdb.d
      - db:/var/lib/mysql
    environment: { ... }
    ports:
      - 3306:3306

volumes:
  flask:
  db:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取错误:django python for docker 中的未知 MySQL 服务器主机“db”(-2) - Getting error : Unknown MySQL server host 'db' (-2) in django python for docker MySQLdb未知的MySQL服务器主机 - MySQLdb Unknown MySQL server host 在Python中使用Flask时,“未知的MySQL服务器主机” - “Unknown MySQL server host” when using Flask in Python docker compose up generate (2005, &quot;Unknown MySQL server host &#39;db&#39; (-2)&quot;) 错误 - docker compose up generate (2005, "Unknown MySQL server host 'db' (-2)") error 未知主机 QEMU_IFLA 类型:50 intalling python on docker - Unknown host QEMU_IFLA type: 50 intalling python on docker Mariadb docker容器无法使用Python连接到主机上的MySQL服务器(111连接被拒绝) - Mariadb docker container Can't connect to MySQL server on host (111 Connection refused) with Python 无法使用SQL Alchemy未知的MySQL服务器主机进行连接 - Cannot Connect Using SQL Alchemy Unknown MySQL server host mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL 服务器主机 'db' (2) - mysql.connector.errors.DatabaseError: 2005 (HY000): Unknown MySQL server host 'db' (2) python gethostbyaddr未知主机mac - python gethostbyaddr unknown host mac django.db.utils.OperationalError: (2005, &quot;Unknown MySQL server host &#39;db&#39; (-2)&quot;) - django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM