生产服务器Docker部署MySQL无法与Django连接问题求助
Alright, let's figure out why your Django app can't connect to the MySQL container in GitLab CI, and fix this step by step.
1. Fix Syntax Errors in Django Settings
First off, I notice a basic syntax mistake in your settings.py database config—your string values aren’t wrapped in quotes! Python will throw an error when trying to read mydb, root, etc., as unquoted identifiers. Update your config to:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydb', 'USER': 'root', 'PASSWORD': 'root', 'HOST': 'db', 'PORT': 3306, } }
2. Ensure Network Connectivity in GitLab CI
In GitLab CI, Docker containers need to be on the same network to resolve each other by service name (like db).
- If you’re using a single
docker-compose.ymlfor both your Django app and MySQL service, Docker automatically creates a shared network, sodbshould be resolvable. Double-check that your Django service is part of the same compose file. - If you’re starting containers separately, manually create a Docker network and attach both containers to it. For example, in your CI script:
docker network create myapp-network docker run --network myapp-network --name db ... mysql:5.7 docker run --network myapp-network --name web ... your-django-image
3. Fix MySQL User Permissions
By default, MySQL’s root user is restricted to local (localhost) connections only. Since your Django app is connecting from another container, you need to grant root access from any host:
- Add a
commandto your MySQL service indocker-compose.ymlto set this up on startup:db: image: mysql:5.7 restart: always env_file: - ./app/app/.env volumes: - ./mysql-data:/var/lib/mysql ports: - "3302:3306" command: --bind-address=0.0.0.0 --default-authentication-plugin=mysql_native_password - Alternatively, if you need to fix an existing container, run these commands:
docker exec -it <your-mysql-container-id> mysql -uroot -proot mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root'; mysql> FLUSH PRIVILEGES;
4. Wait for MySQL to Fully Start
In CI environments, Django often tries to connect before MySQL has finished initializing. Add a wait step to your GitLab CI job to ensure MySQL is ready:
- Use
nc(netcat) to poll the port until it’s open:until nc -z db 3306; do sleep 1; echo "Waiting for MySQL..."; done - Or use the
wait-for-itscript (you can include it in your Docker image or fetch it in the CI job):wait-for-it db:3306 --timeout=30 -- python manage.py migrate
5. Verify Environment Variables Are Loaded
Double-check that your .env variables are correctly passed to the MySQL container. Run this command in your CI pipeline (or locally) to confirm:
docker exec -it <your-mysql-container-id> sh -c 'echo $MYSQL_DATABASE && echo $MYSQL_ROOT_PASSWORD'
Make sure mydb and root are printed—if not, your env_file path might be incorrect in docker-compose.yml.
Start with fixing the syntax error first, then work through the other steps—this should get your connection working.
内容的提问来源于stack exchange,提问作者TNN

