Docker-Compose无法连接到MySQL [英] Docker-Compose can't connect to MySQL

查看:116
本文介绍了Docker-Compose无法连接到MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用docker将Django项目与MySQL连接起来.

I'm trying to connect Django project with MySQL using docker.

启动docker-compose时出现问题.它说下一个错误:

I have the problem when upping docker-compose. It says the next error:

super(连接,自我).初始化(* args,** kwargs2)django.db.utils.OperationalError:(2002年,无法连接到'db'上的MySQL服务器(115)")

super(Connection, self).init(*args, **kwargs2) django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")

我正在使用端口3307,是否应该在本地创建第一个新数据库模式?或因为我的默认端口是3306而怎么办,但是如果我尝试使用它,它说那很忙.

I'm using port 3307, should i create first new database schema in my local? Or how can I do it because my default port is 3306 but if i try to use it, it says that is busy.

我的代码在这里:

Dockerfile

Dockerfile


FROM python:3.7
ENV PYTHONUNBUFFERED 1
ENV LANG=C.UTF-8
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN apt-get update
RUN pip install -r requirements.txt
ADD . /code/


Docker-compose

Docker-compose


    version: '2'

services:
  app:
    container_name: container_app
    build:
      context: .
      dockerfile: Dockerfile
    restart: always
    command: bash -c "python3 manage.py migrate && python manage.py shell < backend/scripts/setup.py && python3 manage.py runserver 0.0.0.0:8000"
    links:
      - db
    depends_on:
      - db
    ports:
      - "8000:8000"
  db:
    container_name: container_database
    image: mariadb
    restart: always
    environment:
      MYSQL_ROOT_HOST: 'host.docker.internal'
      MYSQL_DATABASE: 'container_develop'
      MYSQL_USER: 'root'
      MYSQL_PASSWORD: 'password'
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - "3307:3307"

settings.py:

settings.py:


DATABASES = {
    'default' : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database_develop',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': 3307,
        'CHARSET': 'utf8',
        'COLLATION': 'utf8_bin',
        'OPTIONS': {
            'use_unicode' : True,
            'init_command': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
        },
    }
}

问题是我什么地方都没有数据库,所以我在本地使用它,是否必须将数据库上传到服务器,然后使用服务器提供给我的端口?有什么办法可以从docker本地使用它吗?还是将其安装到Docker?因此,我可以将该docker分享给朋友,他可以使用相同的数据库?

The thing is that I do not have the database anywhere so I'm using it locally, do I have to upload the db to a server and then use the port that server provides to me? Is there any way to use it locally from docker? Or installing it to docker? So I can share that docker to a friend and he can use the same DB?

推荐答案

@Nico回答了我从容器外部修复您的服务可访问性的问题,这意味着如果尝试从主机访问它,则它应该可以工作.

The @Nico answer my fixed your service accessibility from outside of the container, mean if try to access it from the host it should work.

但是在服务之间通信期间将出现错误.您不应该使用服务中的发布端口来服务通信,或者换句话说,您必须使用服务中的容器端口来服务通信.

But the error will arise during service to service communication. You should not use publish port in service to service communication or another word you must use container port in service to service communication.

将连接端口从 3307 更改为 3306

DATABASES = {
    'default' : {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'database_develop',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': 'db',
        'PORT': 3306,
        'CHARSET': 'utf8',
        'COLLATION': 'utf8_bin',
        'OPTIONS': {
            'use_unicode' : True,
            'init_command': 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED',
        },
    }
}

这篇关于Docker-Compose无法连接到MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆