Connecting Django to MySQL in Docker
When attempting to connect Django to MySQL in a Docker environment using Compose, you may encounter an "OperationalError: Can't connect to MySQL server" issue. This error typically arises due to incorrect container configurations or database settings.
Resolving the Issue
The correct parameters to establish a connection between Django and MySQL in Docker are:
To ensure that you're connected successfully, you can use the following command in your docker-compose.yml file:
web: image: onlybelter/django_py35 command: /bin/sh -c "python check_db.py --service-name mysql --ip db --port 3306" volumes: - .:/djcode
This command runs a script that checks if the MySQL port is open. If it is, the script will display a message indicating success.
Additional Tips
The above is the detailed content of How to Fix 'OperationalError: Can't connect to MySQL server' When Using Django and Docker?. For more information, please follow other related articles on the PHP Chinese website!