求助:WordPress Docker无法连接MySQL Docker,求排查解决
Hey friend, let's dig into why your WordPress container can't connect to MySQL. I'll walk through the most common pitfalls based on your docker-compose snippet and help you fix them step by step.
1. Double-Check Network & Hostname Setup
First off, in Docker Compose, services in the same file share a default internal network. That means your WordPress container should use the service name db as the database hostname—not localhost or 127.0.0.1 (those point to the WordPress container itself, not the MySQL one). If you haven’t already, make sure your WordPress service’s WORDPRESS_DB_HOST is set to db:3306.
2. Fix Incomplete MySQL Environment Variables
Looking at your snippet, the MYSQL_PASSWORD line cuts off (djdj...). Make sure this is a full, valid password with no typos or ellipses. Also, confirm that MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD match exactly what you’re using in your WordPress configuration.
3. Resolve MySQL 8.0 Authentication Plugin Compatibility
MySQL 8.0 uses caching_sha2_password by default, but some older WordPress database drivers don’t support this. To fix it, add a command to your MySQL service to force the legacy authentication plugin:
db: image: mysql:8.0 container_name: eve_db volumes: - ./database/data:/var/lib/mysql - ./database/initdb.d:/docker-entrypoint-initdb.d restart: always environment: MYSQL_ROOT_PASSWORD: skdjd MYSQL_DATABASE: djdjd MYSQL_USER: djdjd MYSQL_PASSWORD: your_complete_password_here # Replace with your full password command: --default-authentication-plugin=mysql_native_password
4. Verify Database User Permissions
Sometimes the user you created (djdjd) doesn’t have the right permissions to connect from the WordPress container. Since you’ve mounted the initdb.d volume, create a script there to grant proper access:
Make a file ./database/initdb.d/grant-permissions.sql with:
GRANT ALL PRIVILEGES ON djdjd.* TO 'djdjd'@'%' IDENTIFIED BY 'your_password_here'; FLUSH PRIVILEGES;
This lets the user connect from any host (including other containers in the Docker network).
5. Clear Leftover Database Data
If you’ve started the containers before, leftover data in ./database/data might be causing conflicts. Stop the containers, delete the data volume, then restart:
docker-compose down rm -rf ./database/data docker-compose up -d
Note: This will erase all existing database data—only do this if you don’t have important data saved.
Example Full Docker Compose with WordPress
Just in case you haven’t included the WordPress service yet, here’s a complete working setup:
version: '2' services: db: image: mysql:8.0 container_name: eve_db volumes: - ./database/data:/var/lib/mysql - ./database/initdb.d:/docker-entrypoint-initdb.d restart: always environment: MYSQL_ROOT_PASSWORD: skdjd MYSQL_DATABASE: djdjd MYSQL_USER: djdjd MYSQL_PASSWORD: your_full_password_here command: --default-authentication-plugin=mysql_native_password wordpress: image: wordpress:latest container_name: eve_wordpress volumes: - ./wordpress/wp-content:/var/www/html/wp-content restart: always ports: - "8080:80" environment: WORDPRESS_DB_HOST: db:3306 WORDPRESS_DB_NAME: djdjd WORDPRESS_DB_USER: djdjd WORDPRESS_DB_PASSWORD: your_full_password_here depends_on: - db
Start with checking the hostname and password, then move to the authentication plugin if needed. Let me know if you hit any other snags!
内容的提问来源于stack exchange,提问作者BTR Naidu

