简体   繁体   English

django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")

[英]django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")

I'm trying run django with mysql in docker containers:我正在尝试在 docker 容器中使用 mysql 运行 django:

version: '2'
services:

  db:
    image: mysql:latest
    volumes:
      - ./db:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_USER: root
      MYSQL_PASSWORD: mypassword
      MYSQL_DATABASE: django
    expose:
      - "3306"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
     - db

but after docker-compose up --build I got error:但是在docker-compose up --build之后我得到了错误:

django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)") django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'db' (-2)")

Most likely, the container with mysql did not have time to start.很可能,带有 mysql 的容器没有时间启动。 But I did specify depends_on: - db in the web但我确实指定depends_on: - db web中的db

Who understands what might be the matter?谁知道可能是什么问题?

According to the official docker-compose documentation , depends_on means that this service depends on another one and it cannot run without it, but this doesn't mean the other service is ready to process requests. 根据 depends_on -compose官方文档depends_on表示该服务依赖于另一个服务,没有该服务就无法运行,但这并不意味着另一个服务已准备好处理请求。

So, when you launch your deployment, docker-compose assures that when launching you web service, db is running too (if you try to kill the db container, you'll notice in the docker compose logs that web gets killed too). 因此,在启动部署时,docker-compose会确保在启动web服务时, db也正在运行(如果尝试db容器,您会在docker compose日志中注意到web将终止)。 Since MySQL image needs some time to boot up the first time, this results in the database not being instantly able to process queries. 由于MySQL映像需要一些时间才能首次启动,因此这导致数据库无法立即处理查询。

To solve this problem, docker-compose has a keyword, restart , that specify your container behaviour when something goes wrong and your process dies (such as not having the db ready to process requests). 为了解决此问题,docker-compose具有关键字restart ,用于在出现问题且进程终止时(例如,没有准备好处理请求的db )指定容器的行为。

To wrap up, your docker-compose.yaml file should be: 最后,您docker-compose.yaml文件应为:

version: '2'
services:

  db:
    image: mysql:latest
    volumes:
      - ./db:/var/lib/mysql
    restart: always
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_USER: root
      MYSQL_PASSWORD: mypassword
      MYSQL_DATABASE: django
    expose:
      - "3306"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    restart: always
    ports:
      - "8000:8000"
    depends_on:
     - db

You can find more about the restart keyword in the official documentation 您可以在官方文档中找到有关restart关键字的更多信息。

depends_on in docker-compose means that a service depends on another and it cannot run without it, but this doesn't mean the other service is ready and listening for requests. depends_on -compose中的depends_on表示服务依赖于另一个服务,没有它就无法运行,但这并不意味着另一个服务已准备就绪且正在侦听请求。

What I like to do is add a wait-for-it in my command to the container needing another service to be up and ready to process request. 我想做的是在命令中添加一个wait-for-it到需要其他服务并准备好处理请求的容器中。

In your case involving the db and web , after dropping in the wait-for-it.sh in my code dir, I'd modify the command to the web container to: 在涉及dbweb的情况下,在我的代码目录中放入wait-it.sh之后,我将对Web容器的命令修改为:

./wait-for-it.sh -t 300 db:3306 && python manage.py runserver 0.0.0.0:8000

This will simply tell web to wait on the availability of host db on port 3306 thus synchronizing the spin-up of the two interdependent services 这只会告诉web等待端口3306上的主机db的可用性,从而同步两个相互依赖的服务的启动

If I'm not wrong You should use links in your docker-compose , as 如果我没看错,则应在docker-compose使用links ,如下所示

version: '2'
services:

  db:
    image: mysql:latest
    volumes:
      - ./db:/var/lib/mysql
    ports:
      - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: mypassword
      MYSQL_USER: root
      MYSQL_PASSWORD: mypassword
      MYSQL_DATABASE: django
    expose:
      - "3306"

  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
     - db
    links: - db

In my case (downgrading) I had some dangling named volumes declared in the volumes section of the Compose file and anonymous volumes attached to containers.就我而言(降级),我在 Compose 文件的volumes部分声明了一些悬空的命名卷,并将匿名卷附加到容器。 Executing执行

docker-compose down -v

resolved the issue.解决了这个问题。 Beware that this actually removes named volumes declared in the volumes .请注意,这实际上会删除在 volumes 中声明的命名volumes

In my case I did not have the VPN connection ready to reach the external network.就我而言,我没有准备好连接外部网络的 VPN 连接。 I was not aware that the "Network Settings" under Linux switch the VPN connection to off after every computer restart.我不知道 Linux 下的“网络设置”会在每次计算机重新启动后off VPN 连接。

And then I got this similar error:然后我得到了这个类似的错误:

$docker-compose up
Recreating MYCONTAINER ... done
Attaching to MYCONTAINER
MYDOCKERCOMPOSESERVICE_1  | 2022-01-14 10:56:30,045 [INFO]: Connecting to DB ...
MYDOCKERCOMPOSESERVICE_1  | Traceback (most recent call last):
MYDOCKERCOMPOSESERVICE_1  |   File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
MYDOCKERCOMPOSESERVICE_1  |     return _run_code(code, main_globals, None,
MYDOCKERCOMPOSESERVICE_1  |   File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
MYDOCKERCOMPOSESERVICE_1  |     exec(code, run_globals)
MYDOCKERCOMPOSESERVICE_1  |   File "/MYDOCKERCOMPOSESERVICE/main.py", line 265, in <module>
MYDOCKERCOMPOSESERVICE_1  |     main()
MYDOCKERCOMPOSESERVICE_1  |   File "/MYDOCKERCOMPOSESERVICE/main.py", line 244, in main
MYDOCKERCOMPOSESERVICE_1  |     MYSERVER_conn = create_connection(MYSERVER_CONNECTION)
MYDOCKERCOMPOSESERVICE_1  |   File "/MYDOCKERCOMPOSESERVICE/main.py", line 65, in create_connection
MYDOCKERCOMPOSESERVICE_1  |     conn = MySQLdb.connect(cursorclass=cursors.DictCursor, charset="utf8",
MYDOCKERCOMPOSESERVICE_1  |   File "/usr/local/lib/python3.9/dist-packages/MySQLdb/__init__.py", line 123, in Connect
MYDOCKERCOMPOSESERVICE_1  |     return Connection(*args, **kwargs)
MYDOCKERCOMPOSESERVICE_1  |   File "/usr/local/lib/python3.9/dist-packages/MySQLdb/connections.py", line 185, in __init__
MYDOCKERCOMPOSESERVICE_1  |     super().__init__(*args, **kwargs2)
MYDOCKERCOMPOSESERVICE_1  | MySQLdb._exceptions.OperationalError: (2005, "Unknown MySQL server host 'MYSERVER' (-3)")
MYDOCKERCOMPOSESERVICE_1  | (2005, "Unknown MySQL server host 'MYSERVER' (-3)")
MYCONTAINER exited with code 1

Here -3, in the question -2, I guess these are just positions in the arguments of the connection object.这里-3,在问题-2中,我猜这些只是连接对象的参数中的位置。 In my case, a connection with the db was impossible, I tested it in the bash of the container again, using mysql on the command line.在我的情况下,与数据库的连接是不可能的,我再次在容器的 bash 中测试它,在命令行上使用 mysql。

Solution: Just switch VPN on again, then the connection can be created:解决方案:再次打开VPN,即可建立连接:

在此处输入图像描述

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

相关问题 什么是 django.db.utils.OperationalError:(2000,'未知的 MySQL 错误') - What is django.db.utils.OperationalError: (2000, 'Unknown MySQL error') django.db.utils.OperationalError:(2002,“无法连接到'db'(115)上的 MySQL 服务器”) - django.db.utils.OperationalError: (2002, “Can't connect to MySQL server on 'db' (115)”) django.db.utils.OperationalError: 没有这样的表 Django 2 - django.db.utils.OperationalError: no such table Django 2 Django:django.db.utils.OperationalError: 没有这样的列 - Django:django.db.utils.OperationalError: no such column django.db.utils.OperationalError:无法连接到服务器:没有这样的文件或目录 - django.db.utils.OperationalError: could not connect to server: No such file or directory django.db.utils.OperationalError 无法连接到服务器 - django.db.utils.OperationalError Could not connect to server django.db.utils.OperationalError:没有这样的列: - django.db.utils.OperationalError: no such column: django.db.utils.OperationalError的解决方案 - solution for django.db.utils.OperationalError 迁移错误(django.db.utils.OperationalError) - Migration error(django.db.utils.OperationalError) django.db.utils.OperationalError: 没有这样的表 - django.db.utils.OperationalError: no such table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM