django.db.utils.OperationalError: (2002, "Can't con

2023-07-06Python开发问题
136

本文介绍了django.db.utils.OperationalError: (2002, "Can't 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)")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11