Laradock环境下Docker容器连接局域网MySQL数据库故障求助
Hey there! Let's work through this MySQL connection issue together—this is a pretty common snag when moving legacy PHP apps into Docker on Windows, especially with Laradock. Let's break down the steps to diagnose and fix it:
1. Verify Basic Network Connectivity First
First, we need to confirm if your Docker container can even reach the Ubuntu MySQL server. Here's how:
- SSH into your Laradock workspace container (run
docker-compose exec workspace bashfrom your Laradock directory). - Try pinging your Ubuntu server's local LAN IP (not localhost or Docker internal IPs):
ping <UBUNTU_MYSQL_SERVER_IP>- If this fails, the problem is at the network level (either Docker's network setup, Windows firewall, or Ubuntu's network config).
- If it works, move on to checking MySQL permissions.
2. Fix MySQL Server Permissions (Most Common Culprit)
MySQL by default restricts connections to localhost only. You need to explicitly allow your Docker container's IP (or subnet) to access the database:
- Log into MySQL on your Ubuntu server:
mysql -u root -p - Grant access to your app's database user from any IP (or lock it down to Docker's subnet for security):
-- Allow access from any IP (quick test, not ideal for production) GRANT ALL PRIVILEGES ON your_legacy_db.* TO 'your_db_user'@'%' IDENTIFIED BY 'your_db_password'; -- Or restrict to Docker's default bridge subnet (more secure) GRANT ALL PRIVILEGES ON your_legacy_db.* TO 'your_db_user'@'172.17.0.0/16' IDENTIFIED BY 'your_db_password'; - Flush privileges to apply changes:
FLUSH PRIVILEGES; - Also, make sure Ubuntu's firewall (ufw) allows incoming traffic on port 3306:
sudo ufw allow 3306 # Or restrict to Docker's subnet for added security sudo ufw allow from 172.17.0.0/16 to any port 3306
3. Check Laradock & App Configuration
- Don't use
localhostor127.0.0.1in your PHP app's DB settings: These point to the Docker container itself, not your Ubuntu server. Use the Ubuntu server's actual LAN IP instead. - Verify Laradock's network setup: Laradock uses a custom bridge network (
laradock_default) by default, which should allow outbound connections. If you're using WSL2 as your Docker backend, ensure WSL2 has access to your local LAN (you can test this by pinging the Ubuntu IP from within WSL2 directly).
4. Test the Connection Directly from the Container
To rule out PHP app config issues, test the MySQL connection using the MySQL client inside your Laradock workspace:
mysql -h <UBUNTU_MYSQL_SERVER_IP> -u your_db_user -p
- If this connects successfully, the problem is in your PHP app's database configuration (double-check credentials, host IP, etc.).
- If it fails, go back to troubleshooting network connectivity or MySQL permissions.
5. Windows-Specific Checks
- Ensure your Windows 10 host can ping the Ubuntu server and connect to port 3306 (use
telnet <UBUNTU_IP> 3306orTest-NetConnection <UBUNTU_IP> -Port 3306in PowerShell). If the host can't connect, fix Windows firewall rules or LAN network issues first. - If using WSL2 backend: WSL2 has its own virtual network. Sometimes you need to ensure WSL2 is set to use the same network as your Windows host (check Docker Desktop settings under "Resources > WSL Integration" to make sure your distro is enabled).
内容的提问来源于stack exchange,提问作者Michael
相关产品推荐
相关产品推荐

