I can't log in to MySQL from Django when using docker-compose

When I tried to create a Django development environment with Docker, the only thing that didn't work was logging in to MySQL from Django.

This is what I have been suffering from for about 3 days. ..

django.db.utils.OperationalError: (2005, "Unknown MySQL server host 'mysql' (11001)")

After trying to isolate the problem, the IP name resolution of the mysql container seems to work, and the ports and expose parts are not particularly mistaken, so the problem is the behavior after entering the MySQL container.

I couldn't find any good information on the Japanese site, but I searched for various English documents and finally solved it, so I summarized it.

environment

Premise

Directory structure

proj/
  ├ docker/
  │   ├ nginx/
  │   │  └ default.conf
  │   └ python/
  │      ├ Dockerfile
  │      └ requirements.txt
  ├ django_proj (The following is omitted)
  ├ docker-compose.yml
  └ manage.py

Solutions

Pay attention to the mysql settings in docker-compose.yml! (That's it! Lol)

docker-compose.yml


version: "3.9"
   
services:
  python:
    build: ./docker/python
    command: gunicorn composeexample.wsgi:application --bind 0.0.0.0:8000 --reload
    volumes:
      - .:/code
    expose:
      - "8000"

  nginx:
    image: nginx:1.19
    volumes:
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - ./static:/etc/nginx/static
    ports:
      - "80:80"
    depends_on:
      - python

  mysql:
    image: mysql:5.6
    command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    volumes:
      - ./docker/db/data:/var/lib/mysql
    expose:
      - "3306"
    environment:
      - MYSQL_ROOT_USER=root
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_ROOT_HOST=%           #This one line was the point of this time

  phpmyadmin:
    image: phpmyadmin
    ports:
      - "8080:80"
    environment:
      - PMA_ARBITRARY=1

There was the following description on the MySQL official page.

MYSQL_ROOT_HOST: By default, MySQL creates the 'root'@'localhost' account. This account can only be connected to from inside the container as described in Connecting to MySQL Server from within the Container. To allow root connections from other hosts, set this environment variable. For example, the value 172.17.0.1, which is the default Docker gateway IP, allows connections from the host machine that runs the container. The option accepts only one entry, but wildcards are allowed (for example, MYSQL_ROOT_HOST=172...* or MYSQL_ROOT_HOST=%).

By default, the account is created with root @ localhost, so it seems that you can only log in as the root user from localhost, that is, the docker container.

So, if you add the description of MYSQL_ROOT_HOST =%, you will be able to log in as the root user from any IP address, so I think that you can also log in from a different container. (maybe)

reference

https://dev.mysql.com/doc/mysql-installation-excerpt/8.0/en/docker-mysql-more-topics.html#docker_var_mysql-root-host

Recommended Posts

I can't log in to MySQL from Django when using docker-compose
I can't log in on Heroku when I introduce Basic authentication.
Connect from Java to MySQL using Eclipse
I summarized the points to note when using resources and resources in combination
How to log in automatically when Ubuntu restarts
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
Whether to enable SSL when using JDBC with MySQL.
I started MySQL 5.7 with docker-compose and tried to connect
Things to keep in mind when using if statements
[Urgent recruitment] I can't convert from java.util.date to java.sql.date ...
I want to connect to Heroku MySQL from a client
A memorandum when I investigated how to convert from Epoch Time to Date type in Scala
I tried to make an application in 3 months from inexperienced
When there is no output to stdout in docker log
I want to find a relative path in a situation using Path
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
How to exclude auto-generated from Jacoco coverage when using Lombok
I tried using Dapr in Java to facilitate microservice development
When you want to change the MySQL password of docker-compose
I was addicted to using Java's Stream API in Scala
Things to keep in mind when using Sidekiq with Rails
Ubuntu unable to log in
When I try to put centos in VMware, pane is dead
02. I made an API to connect to MySQL (MyBatis) from Spring Boot
I tried connecting to MySQL using JDBC Template with Spring MVC
Summary of problems that I could not log in to firebase
Unable to get resources when using modules in Gradle and IntelliJ
From building to deploying Ruby on Jets in docker-compose environment <Part 2>
From building to deploying Ruby on Jets in docker-compose environment <Part 1>
Things to note when using Spring AOP in Jersery resource classes
What I did in the migration from Spring Boot 1.4 series to 2.0 series
I want to add devise in Rails, but I can't bundle install
What I did in the migration from Spring Boot 1.5 series to 2.0 series
How to solve the unknown error when using slf4j in Java
[Android] I want to get the listener from the button in ListView
What I thought about when I started migrating from Java to Kotlin
What I don't like when using interface of a function with default arguments in Kotlin from Java
Precautions when using checkboxes in Thymeleaf
I want to docker-compose up Next.js!
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
Log output to file in Java
Change DB from SQLite to MySQL
I tried using JWT in Java
Update MySQL from 5.7 to 8.0 with Docker
Enable log output to both file and console using log4j in Eclipse.
[Rails] I was addicted to the nginx settings when using Action Cable.
A story I was addicted to when testing the API using MockMVC
My.cnf configuration problem that I was addicted to when I was touching MySQL 8.0 like 5.7
[Rails] What I learned from a little stumbling block when using ancestry
[Note] I want to get in reverse order using afterLast with JdbcTemplate
Try to get data from database using MyBatis in Micronaut + Kotlin project
Things to keep in mind when using Apache PDFBox® with AWS Lambda
I want to get the IP address when connecting to Wi-Fi in Java
I want to display an error message when registering in the database
I tried to make a talk application in Java using AI "A3RT"
ERROR 2002 (HY000) when starting MySQL: Can't connect to local MySQL server through socket'/tmp/mysql.sock' (2)