django.db.utils.OperationalError: (2002, quot;Can#39;t connect to MySQL server on #39;db#39; (115)quot;)(django.db.utils.OperationalError: (2002, Cant connect to MySQL server on db (115)))
问题描述
This has already been asked, but none of the answers have helped me. This is my configuration. Im running docker-compose with two services, a web app in django and the database in mariadb. I can connect normally to my local db with this exact configuration, only changing the host in settings.py to localhost. When i run docker-compose up, the web service stops immediately after trying to connect to the database, and return this error.
django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
Dockerfile
FROM python:3.7
RUN mkdir /app
COPY requierments.txt /app/
WORKDIR /app
RUN pip install -r requierments.txt
COPY . /app/
docker-compose.yml
version: '3'
services:
db:
image: mariadb:10.2
expose:
- 3306
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: 'django_backend'
MYSQL_USER: 'django'
MYSQL_PORT: '3306'
MYSQL_PASSWORD: 'mysql1234pass'
MYSQL_ROOT_PASSWORD: 'password'
web:
build: .
image: backendblockchain_web
volumes:
- .:/app
ports:
- "8000:8000"
depends_on:
- db
links:
- db
command:
- /bin/bash
- -c
- |
sleep 10
python3 manage.py migrate
python3 manage.py runserver 0.0.0.0:8000
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django_backend',
'USER': 'django',
'PASSWORD': 'mysql1234pass',
'HOST': 'db',
'PORT': '3306'
}
}
your WEB containers starts before the DB is up and running , you need to wait for it using one of these methods or starting your DB first manually with docker-compose up db
这篇关于django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
