简体   繁体   English

Django 在初始化后失去与 MySQL 数据库的连接 (Docker)

[英]Django loses connection to MySQL db after initialization (Docker)

I'm starting to work with Docker, I'm running a Django app in a container, and a MySQL db in another one.我开始使用 Docker,我在一个容器中运行一个 Django 应用程序,在另一个容器中运行一个 MySQL 数据库。 Both of them are connected through a docker-compose.yml file.它们都通过docker-compose.yml文件连接。

After building the containers, Django migrations run successfully and the application starts properly.构建容器后,Django 迁移成功运行,应用程序正常启动。

However, if I connect to the Django container and execute a command (ie python manage.py createsuperuser ) an OperationalError is raised saying that it is not possible to connect to the database.但是,如果我连接到 Django 容器并执行命令(即python manage.py createsuperuser ),则会引发OperationalError说无法连接到数据库。 What can be the problem?可能是什么问题?

The picture below shows that tables are created in the database (at some point Django does have access to the db)下图显示在数据库中创建了表(在某些时候 Django 确实可以访问数据库)

Django 可以访问数据库并运行迁移

Accessing the 4rs_backend container (Django) with docker exec -it <container id> sh and executing python manage.py createsuperuser raises:使用docker docker exec -it <container id> sh -it docker exec -it <container id> sh访问4rs_backend容器(Django)并执行python manage.py createsuperuser引发:

在此处输入图片说明

docker-compose.yml: docker-compose.yml:

version: '3'
services:
   db:
    image: mysql:5.7
    ports:
      - "7000:3306"
    environment:
      MYSQL_PORT: 3306
      MYSQL_USER: admin_test
      MYSQL_ROOT_PASSWORD: '*****'
      MYSQL_PASSWORD: '*****'
      MYSQL_DATABASE: forrealstate
    container_name: mysql_db
  api:
    build: backend
    command: bash -c "BUILD_TYPE='DOCKER' python manage.py makemigrations && BUILD_TYPE='DOCKER' python manage.py migrate && BUILD_TYPE='DOCKER' python manage.py runserver 0.0.0.0:8000"
    container_name: 4rs_backend
    volumes:
      - ./backend:/backend
    ports:
      - "8000:8000"
    depends_on:
      - db

DATABSE config in Settings.py: Settings.py 中的 DATABSE 配置:

BUILD_TYPE = os.environ.get('BUILD_TYPE')
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'forrealstate',
        'USER': 'admin_test' if BUILD_TYPE == 'DOCKER' else 'root',
        'PASSWORD': '********',
        'HOST': 'db' if BUILD_TYPE == 'DOCKER' else '127.0.0.1',
        'PORT': '3306',
        'ATOMIC_REQUESTS': True,
    }
}

I hope you can help me!我希望你可以帮助我!

Kind regards亲切的问候

EDIT: Answer编辑:回答

Fixed by adding BUILT_TYPE: 'DOCKER' as an environment variable in the api service.通过在 api 服务中添加BUILT_TYPE: 'DOCKER'作为环境变量来修复。

the DB connection depends on an environment variable BUILD_TYPE .数据库连接取决于环境变量BUILD_TYPE In the api container it is set before executing the command.api容器中,它是在执行命令之前设置的。 When you attach to the container, this variable is not set, so django uses the default 127.0.0.1 host.当你附加到容器时,这个变量没有设置,所以 django 使用默认的 127.0.0.1 主机。

Fix: prepend BUILD_TYPE='DOCKER' to all manage.py commands, eg:修复:在所有manage.py命令前添加BUILD_TYPE='DOCKER' ,例如:

BUILD_TYPE='DOCKER' python manage.py createsuperuser

Alternatively, set this variable globally in the container (that's what I would do)或者,在容器中全局设置这个变量(这就是我要做的)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM