Home > Database > Mysql Tutorial > body text

Here\'s a question-based title that captures the essence of the article: Why Can\'t My Django App Connect to MySQL in Docker Using Docker-Compose? Here are some other variations: * Django to MySQL

Linda Hamilton
Release: 2024-10-29 05:34:31
Original
1063 people have browsed it

Here's a question-based title that captures the essence of the article:

Why Can't My Django App Connect to MySQL in Docker Using Docker-Compose? 

Here are some other variations:

* Django to MySQL in Docker: Troubleshooting Connection Errors with dock

Unable to Connect Django to MySQL in Docker Using Docker-Compose

Attempting to establish a connection between Django and MySQL within a Docker environment using docker-compose can result in the following error:

django.db.utils.OperationalError: (2003, 'Can\'t connect to MySQL
  server on \'mariadb55\' (111 "Connection refused")')
Copy after login

Solution

To resolve this issue, it is crucial to ensure that the port and host specified in the Django settings file align with the actual configuration within the docker-compose.yml file.

Steps to Fix the Issue:

  1. Set the Host to 'db' and Port to '3306' in settings.py:

    • 'HOST': 'db',
    • 'PORT': '3306',
  2. Adjust the docker-compose.yml File:

    • Ensure that the port exposed for the database service is 3306.
    • Use db as the host when linking the Django and database services.
  3. Activate a Check for Open Ports:

    • Add an extra command to docker-compose.yml to verify if the specified port is accessible.

Example docker-compose.yml File:

version: '3'

services:
  db:
    image: mariadb:5.5
    restart: always
    environment:
      - MYSQL_HOST=localhost
      - MYSQL_PORT=3306
      - MYSQL_ROOT_HOST=%
      - MYSQL_DATABASE=test
      - MYSQL_USER=belter
      - MYSQL_PASSWORD=belter_2017
      - MYSQL_ROOT_PASSWORD=123456_abc
    volumes:
      - /home/belter/mdbdata/mdb55:/var/lib/mysql
    ports:
      - "3302:3306"
  web:
    image: onlybelter/django_py35
    command: /bin/sh -c "python check_db.py --service-name mysql --ip db --port 3306"
    volumes:
      - .:/djcode
Copy after login

Additional Tips:

  • Use db as the host when connecting to MySQL from within the Django container (using the mysql -h db -p ... command).
  • Check if 3306 is available by running: nmap -p 3306 localhost.
  • Verify that the Django and database containers are running properly using: docker-compose ps.

By following these steps, you can establish a stable connection between Django and MySQL within a Docker environment using docker-compose.

The above is the detailed content of Here\'s a question-based title that captures the essence of the article: Why Can\'t My Django App Connect to MySQL in Docker Using Docker-Compose? Here are some other variations: * Django to MySQL. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template