Docker-Compose 无法连接到 MySQL

Docker-Compose can#39;t connect to MySQL(Docker-Compose 无法连接到 MySQL)
本文介绍了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(Connection, self).init(*args, **kwargs2)django.db.utils.OperationalError: (2002, "无法连接到 MySQL 服务器上的 'db' (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


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 分享给一个朋友,他可以使用同一个 DB?

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)