Home > Database > Mysql Tutorial > body text

Here are a few title options, following the question-answer format you requested: Option 1 (More direct): * Django in Docker Compose: Why Can\'t I Connect to My MySQL Database? Option 2 (More speci

Linda Hamilton
Release: 2024-10-28 08:24:02
Original
195 people have browsed it

Here are a few title options, following the question-answer format you requested:

Option 1 (More direct):

* Django in Docker Compose: Why Can't I Connect to My MySQL Database?

Option 2 (More specific):

*

Django in Docker Compose: Establishing MySQL Connection

Connecting Django to a MySQL database within a Docker environment can be challenging. This article delves into the issue experienced when trying to establish the connection, leading to the error: "Can't connect to MySQL server on 'mariadb55' (111 "Connection refused")."

Initially, the database was accessible through Docker Compose up db, and a local instance was connectable through Python manage.py runserver. However, the connection from within Django failed.

Upon further investigation, it was discovered that the issue stemmed from using an incorrect port and hostname in the Django settings.py configuration file.

Solution:

The solution was to ensure the correct port and hostname were set in the settings.py file. Specifically, the settings should use 'db' as the host and '3306' as the port.

Updated settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'belter',
        'HOST': 'db',
        'PORT': '3306',
        'PASSWORD': 'belter_2017',
        'default-character-set': 'utf8',
        'OPTIONS': {
            'sql_mode': 'traditional',
        }
    }
}
Copy after login

Additionally, an extra command was added to the Docker Compose configuration file to check if the MySQL port was open before attempting the connection from Django.

By applying these changes, the connection between Django and MySQL within the Docker environment was established successfully.

The above is the detailed content of Here are a few title options, following the question-answer format you requested: Option 1 (More direct): * Django in Docker Compose: Why Can\'t I Connect to My MySQL Database? Option 2 (More speci. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!