4

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

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

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

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.

My code here:

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

 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:

 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', }, } } 

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?

2 Answers 2

4

In the "db" part, in your docker-compose :

ports: - "3307:3306" 
Sign up to request clarification or add additional context in comments.

2 Comments

thx for your answer @Nico, i still have the same error: django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
@Adiii seems to have found the solution
3

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.

Change the port in connection from 3307 to 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', }, } } 

9 Comments

Thx @Adiii it works!! Now i have this error: database | 2019-10-15 16:35:09 13 [Warning] Access denied for user 'root'@'172.19.0.1' (using password: NO) Should i do anything special to solve it? Thanks
What do you mean? I have the same pass in docker-compose and in settings
if the issue still persists try to add command: --default-authentication-plugin=mysql_native_password . i can verified the connection
var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'db', user : 'root', password : 'password', database : 'container_develop' }); in nodejs it worked for me
Thx man! It works but now i restarted docker with docker system prune --all, and now i'm trying to build it again with docker build . And i have the next error en dockerfile (received unexpected HTTP status: 503 Service Unavailable). Do you know why? Thanks man!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.