Docker-Compose can#39;t connect to 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Docker-Compose 无法连接到 MySQL


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 更改自动增量起始编号? 2021-01-01