I am trying to use Docker with Django but I get error - 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)")
. When I am using my app without Docker it works. Any suggestions?
我试图使用Docker与Django但我得到错误 - db_1 |错误:数据库未初始化且未指定密码选项db_1 |您需要指定MYSQL_ROOT_PASSWORD,MYSQL_ALLOW_EMPTY_PASSWORD和MYSQL_RANDOM_ROOT_PASSWORD之一django.db.utils.OperationalError :( 2003,“无法连接到'127.0.0.1上的MySQL服务器'(111)”)。当我使用没有Docker的应用程序时,它可以工作。有什么建议么?
My Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
My 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
I have changed docker-compose.yaml to:
我已将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
but at this moment proces has blocked on
但此时进程已经阻止了
db_1 | 2017-06-13T05:16:16.457122Z 0 [Note] End of list of non-natively partitioned tables
or sometimes I still get 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,“无法连接到'127.0.0.1上的MySQL服务器'(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)
1 个解决方案
#1
1
127.0.0.1 (111)
means MySQL refuses your connect request. I assume MySQL server and Django are not running in the same Docker instance, are they?
127.0.0.1(111)表示MySQL拒绝您的连接请求。我假设MySQL服务器和Django没有在同一个Docker实例中运行,是吗?
If MySQL is running somewhere else, you should make sure you configured the MySQL connection correctly in Django's config file.
如果MySQL在其他地方运行,你应该确保在Django的配置文件中正确配置了MySQL连接。
Solution 1: You can allow remote access to MySQL (but I don't encourage you do this), by changing the binding address of MySQL from 127.0.0.1
to 0.0.0.0
. and update the MySQL DB connection settings properly.
解决方案1:您可以通过将MySQL的绑定地址从127.0.0.1更改为0.0.0.0来允许远程访问MySQL(但我不建议您这样做)。并正确更新MySQL DB连接设置。
In Linux you can comment out these lines from /etc/my.cnf
(The location can be different)
在Linux中你可以从/etc/my.cnf注释掉这些行(位置可以不同)
skip-networking
bind-address = 127.0.0.1
For more detailed instruction, see also https://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
有关更详细的说明,另请参阅https://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
Solution 2: (Inspired by the comment of @programerq) You can run MySQL in host OS or in another Docker instance. As MySQL is bound to 127.0.0.1
by default, you can map MySQLMachine:MySQLPort
to DjangoDocker:MySQLPort
when you start your Django Docker instance. So that Django can still connect to 127.0.0.1
to MySQL without noticing that MySQL is actually running somewhere else.
解决方案2 :(受@programerq的评论启发)您可以在主机操作系统或其他Docker实例中运行MySQL。由于默认情况下MySQL绑定到127.0.0.1,因此当您启动Django Docker实例时,可以将MySQLMachine:MySQLPort映射到DjangoDocker:MySQLPort。因此,Django仍然可以连接到127.0.0.1到MySQL而不会注意到MySQL实际上正在其他地方运行。
I did some port mapping in a Docker cluster project before, you can check the commands I ran, or check the official Docker document.
我之前在Docker集群项目中做了一些端口映射,你可以查看我运行的命令,或查看官方的Docker文档。
Hope these ideas can be helpful, cheers.
希望这些想法可以帮助,欢呼。
#1
1
127.0.0.1 (111)
means MySQL refuses your connect request. I assume MySQL server and Django are not running in the same Docker instance, are they?
127.0.0.1(111)表示MySQL拒绝您的连接请求。我假设MySQL服务器和Django没有在同一个Docker实例中运行,是吗?
If MySQL is running somewhere else, you should make sure you configured the MySQL connection correctly in Django's config file.
如果MySQL在其他地方运行,你应该确保在Django的配置文件中正确配置了MySQL连接。
Solution 1: You can allow remote access to MySQL (but I don't encourage you do this), by changing the binding address of MySQL from 127.0.0.1
to 0.0.0.0
. and update the MySQL DB connection settings properly.
解决方案1:您可以通过将MySQL的绑定地址从127.0.0.1更改为0.0.0.0来允许远程访问MySQL(但我不建议您这样做)。并正确更新MySQL DB连接设置。
In Linux you can comment out these lines from /etc/my.cnf
(The location can be different)
在Linux中你可以从/etc/my.cnf注释掉这些行(位置可以不同)
skip-networking
bind-address = 127.0.0.1
For more detailed instruction, see also https://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
有关更详细的说明,另请参阅https://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
Solution 2: (Inspired by the comment of @programerq) You can run MySQL in host OS or in another Docker instance. As MySQL is bound to 127.0.0.1
by default, you can map MySQLMachine:MySQLPort
to DjangoDocker:MySQLPort
when you start your Django Docker instance. So that Django can still connect to 127.0.0.1
to MySQL without noticing that MySQL is actually running somewhere else.
解决方案2 :(受@programerq的评论启发)您可以在主机操作系统或其他Docker实例中运行MySQL。由于默认情况下MySQL绑定到127.0.0.1,因此当您启动Django Docker实例时,可以将MySQLMachine:MySQLPort映射到DjangoDocker:MySQLPort。因此,Django仍然可以连接到127.0.0.1到MySQL而不会注意到MySQL实际上正在其他地方运行。
I did some port mapping in a Docker cluster project before, you can check the commands I ran, or check the official Docker document.
我之前在Docker集群项目中做了一些端口映射,你可以查看我运行的命令,或查看官方的Docker文档。
Hope these ideas can be helpful, cheers.
希望这些想法可以帮助,欢呼。