我有一個使用Django和MySQL的應用程序。應用程序不會從Django應用程序連接到MySQL容器,但可以使用Sequel Pro以相同的憑據訪問它。Django無法通過docker-compose連接到mysql
這是我的搬運工,compose.yml
version: '2'
services:
db:
image: mysql:5.7.18
ports:
- "3307:3306"
volumes:
- "./mysql:/var/lib/mysql"
command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment:
- MYSQL_ROOT_PASSWORD=pass
- MYSQL_DATABASE=django
- MYSQL_USER=django
- MYSQL_PASSWORD=pass
web:
build:
context: .
depends_on:
- db
# links:
# - db
volumes:
- "./app:/src"
command: bash -c "sleep 3 && python3 src/manage.py runserver 8000"
ports:
- "8001:8000"
environment:
- DJANGO_SETTINGS_MODULE=app.production
- DB_NAME=django
- DB_USER=django
- DB_PASS=pass
- DB_HOST=db
- DB_PORT=3306
這是我的Django settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.getenv('DB_NAME', 'django'),
'USER': os.getenv('DB_USER', 'django'),
'PASSWORD': os.getenv('DB_PASS', 'pass'),
'HOST': os.getenv('DB_HOST', 'db'),
'PORT': os.getenv('DB_PORT', '3306'),
'OPTIONS': {
'sql_mode': 'STRICT_TRANS_TABLES',
}
}
}
這是錯誤,我不知道如何解決這個
web_1 | Performing system checks...
web_1 |
web_1 | Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7efeb8563950>
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python3.5/dist-
packages/django/db/backends/base/base.py", line 213, in ensure_connection
web_1 | self.connect()
web_1 | File "/usr/local/lib/python3.5/dist-
packages/django/db/backends/base/base.py", line 189, in connect
web_1 | self.connection = self.get_new_connection(conn_params)
web_1 | File "/usr/local/lib/python3.5/dist-
packages/django/db/backends/mysql/base.py", line 274, in get_new_connection
web_1 | conn = Database.connect(**conn_params)
web_1 | File "/usr/local/lib/python3.5/dist-
packages/MySQLdb/__init__.py", line 86, in Connect
web_1 | return Connection(*args, **kwargs)
web_1 | File "/usr/local/lib/python3.5/dist-packages/MySQLdb/connections.py", line 204, in __init__
web_1 | super(Connection, self).__init__(*args, **kwargs2)
web_1 | _mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on 'db' (110)")
請指教我不知道我做錯了什麼。
更新:我剛剛看到我的容器不能發送請求sentry.io太
Sentry responded with an error: <urlopen error [Errno -3] Temporary failure in name resolution> (url: https://sentry.io/api/210137/store/)
所以這大約是碼頭工人的網絡?我怎麼調查這個問題,感謝
嘗試做一個「泊塢窗EXEC」到Django的容器中,然後嘗試做一些DNS故障排除:這是什麼'挖分貝。 @ 127.0.0.11'返回? – programmerq
'dig google.com','dig db','dig db。 @ 127.0.0.11'連接超時;沒有服務器可以達到 –