Docker部署MySQL与phpMyAdmin连接失败,MySQL容器反复重启求助
Hey there, let’s tackle this frustrating issue where your MySQL container keeps restarting and phpMyAdmin can’t connect. That Killed error in your logs is a critical hint—here’s what’s likely going wrong and how to fix it:
Common Causes & Solutions
1. Out-of-Memory (OOM) Killer is Terminating MySQL
The most frequent reason for that "Killed" message is that your host machine or container doesn’t have enough memory to run MySQL. The system’s OOM Killer kicks in and terminates the process when it exceeds memory limits.
Fixes:
- Check your host’s memory usage with
free -h—if you’re low on available RAM, close other resource-heavy apps or upgrade your host. - Add memory limits to your MySQL container in
docker-compose.ymlto prevent it from hogging too much resources:services: mysql: # ... other configs deploy: resources: limits: memory: 512M # Adjust based on your host's capacity reservations: memory: 256M # Guarantees this much memory for the container
2. Permission Issues on Mounted Data Volumes
If you’re mounting a local directory as a MySQL data volume, incorrect file permissions can prevent MySQL from writing to its data directory, leading to a crash.
Fixes:
- On your host machine, set the correct ownership for the data directory (MySQL uses UID 999 by default):
chown -R 999:999 ./mysql-data - If you don’t want to modify host permissions, you can temporarily run the container as root for testing (not recommended for production):
services: mysql: # ... other configs user: "root"
3. Conflicting Startup Parameters or Outdated Images
Your log mentions --daemonize --skip-networking—some newer MySQL official images handle background execution internally, so adding these parameters can cause conflicts. Using an untested or outdated image might also lead to unexpected crashes.
Fixes:
- Use a stable, official MySQL image (e.g.,
mysql:8.0) instead of generic or outdated ones. - Avoid modifying the default entrypoint parameters unless you’re sure you need to. The official image’s entrypoint already manages MySQL’s startup correctly.
Example Working docker-compose.yml
Here’s a polished version of your compose file that addresses the above issues:
version: '3.3' services: mysql: image: mysql:8.0 container_name: mysql_db restart: unless-stopped environment: MYSQL_ROOT_PASSWORD: your_secure_root_password MYSQL_DATABASE: your_default_db MYSQL_USER: your_db_user MYSQL_PASSWORD: your_user_password volumes: - ./mysql-data:/var/lib/mysql ports: - "3306:3306" deploy: resources: limits: memory: 512M reservations: memory: 256M phpmyadmin: image: phpmyadmin/phpmyadmin container_name: phpmyadmin restart: unless-stopped environment: PMA_HOST: mysql # Uses the MySQL service name for internal Docker DNS PMA_PORT: 3306 PMA_USER: root PMA_PASSWORD: your_secure_root_password ports: - "8080:80" depends_on: - mysql
How to Test the Fix
- Stop and remove existing containers (note:
-vdeletes volumes—backup data first if needed):docker-compose down -v - Start the containers again:
docker-compose up -d - Check the MySQL container status and logs:
docker ps # Should show "Up" status for MySQL docker-compose logs mysql # Verify no more "Killed" errors
Once MySQL is running, phpMyAdmin should be able to connect using the service name mysql as the host.
内容的提问来源于stack exchange,提问作者aeemk

