![](/img/trans.png)
[英]django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)"
[英]Error using Django with Docker - “Can't connect to MySQL server on '127.0.0.1' (111)”)
我正在尝试将Docker与Django一起使用,但出现错误db_1 | error: database is uninitialized and password option is not specified db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
db_1 | error: database is uninitialized and password option is not specified db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
db_1 | error: database is uninitialized and password option is not specified db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
。 当我在没有Docker的情况下使用我的应用程序时,它可以工作。 有什么建议么?
我的Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
我的docker-compose.yml:
version: '3'
services:
db:
image: mysql
web:
build: .
command: python3 Project/myVirtual/backend/pri/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
我已经将docker-compose.yaml更改为:
version: '3'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
web:
build: .
command: python3 virtualPRI/PRI/backend/pri/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
但此刻,进程已阻塞
db_1 | 2017-06-13T05:16:16.457122Z 0 [Note] End of list of non-natively partitioned tables
有时我还是会得到web_1 | django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
web_1 | django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
manage:py
:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pri.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
127.0.0.1 (111)
表示MySQL拒绝您的连接请求。 我假设MySQL服务器和Django不在同一Docker实例中运行,对吗?
如果MySQL在其他地方运行,则应确保在Django的配置文件中正确配置了MySQL连接。
解决方案1:通过将MySQL的绑定地址从127.0.0.1
更改为0.0.0.0
,可以允许对MySQL的远程访问(但我不鼓励您这样做)。 并正确更新MySQL DB连接设置。
在Linux中,您可以从/etc/my.cnf
注释掉这些行(位置可以不同)
skip-networking
bind-address = 127.0.0.1
有关更多详细说明,另请参见https://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
解决方案2:( 受@programerq的评论启发),您可以在主机OS或另一个Docker实例中运行MySQL。 在MySQL势必127.0.0.1
默认情况下,你可以映射MySQLMachine:MySQLPort
到DjangoDocker:MySQLPort
当你开始你的Django泊坞实例。 这样,Django仍可以连接到MySQL的127.0.0.1
,而无需注意MySQL实际上正在其他地方运行。
之前,我在Docker集群项目中做了一些端口映射,您可以检查我运行的命令 ,或者检查Docker官方文档。
希望这些想法能有所帮助,欢呼。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.