Home > Database > Mysql Tutorial > Why Can\'t I Connect to My MySQL Database in Docker-Compose?

Why Can\'t I Connect to My MySQL Database in Docker-Compose?

Patricia Arquette
Release: 2024-10-29 08:16:30
Original
201 people have browsed it

Why Can't I Connect to My MySQL Database in Docker-Compose?

Cannot Connect to MySQL Database in Docker-Compose

When attempting to run Django with MariaDB in Docker through docker-compose, users may encounter the following error:

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

To resolve this issue, it is important to ensure that the correct port and host are used in the Django settings file. Specifically, the port should be set to 3306 instead of 3302, and the host should be set to 'db' rather than 'mariadb55' or '127.0.0.1'.

Here is an example of an updated Django settings file:

<code class="python">DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'test',
        'USER': 'belter',
        'HOST': 'db',  # Use 'db' instead of 'mariadb55' or '127.0.0.1'
        'PORT': '3306',   # Use 3306 instead of 3302
        'PASSWORD': 'belter_2017',
        'default-character-set': 'utf8',
        'OPTIONS': {
            'sql_mode': 'traditional',
        }
    }
}</code>
Copy after login

Additionally, it is important to ensure that the MariaDB container is accessible on port 3306, which can be verified by using the check_db.py script provided in the original response.

By following these steps, users should be able to connect to the MySQL database in Docker-compose and resolve the connection refused error.

The above is the detailed content of Why Can\'t I Connect to My MySQL Database in Docker-Compose?. 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