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")')
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>
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!