如何在Windows版Docker中创建MySQL数据库?恢复Schema报错求解
Hey there, let's tackle your two questions step by step—first creating a MySQL container on Windows Docker (Hyper-V), then troubleshooting that schema restore error that works on Mac but not your Windows setup.
一、Creating a MySQL Database in Windows Docker (Hyper-V)
You have two reliable methods here, depending on whether you prefer direct commands or docker-compose:
Method 1: Use docker run Directly
Run this command in PowerShell or Command Prompt (replace placeholders with your values):
docker run -d ` --name mysql-db ` -e MYSQL_ROOT_PASSWORD=your_secure_root_password ` -e MYSQL_DATABASE=your_target_db_name ` -p 3306:3306 ` mysql:5.7 # Match the exact MySQL version you used on Mac!
-d: Runs the container in the background--name: Assigns a friendly name to your container for easy management- Environment variables: Sets the root password and creates a default database automatically
-p: Maps port 3306 on your Windows machine to the container's 3306 port
Method 2: Use docker-compose.yml
Since you mentioned using docker-compose.yml, here's a tested config tailored for your setup:
version: '3' services: mysql: image: mysql:5.7 # Critical: Use the SAME version as your Mac setup container_name: mysql-db environment: MYSQL_ROOT_PASSWORD: your_secure_root_password MYSQL_DATABASE: your_target_db_name # The db where you'll restore your schema ports: - "3306:3306" volumes: - ./Schema:/docker-entrypoint-initdb.d # Mounts your local Schema folder to MySQL's init directory restart: always
To start the container:
docker-compose up -d
Note: For Windows Docker (Hyper-V), you must enable file sharing for the drive containing your Schema folder. Go to Docker Settings > Resources > File Sharing, check the box for the relevant drive, and apply changes.
二、Troubleshooting Schema Restore Errors (Works on Mac, Fails on Windows)
Since this works on Mac, the issue is almost certainly tied to Windows/Hyper-V-specific quirks. Let's go through the most likely fixes:
1. Fix File Mounting Issues
Hyper-V handles local file mounts differently than Docker for Mac:
- Verify Shared Drives: Ensure the drive with your
Schema/test.sqlis enabled in Docker's File Sharing settings (as noted above). - Check Path Format: Windows uses backslashes (
\) but Docker expects forward slashes (/). Indocker-compose.yml, use relative paths like./Schemaor absolute paths like/host_mnt/c/Users/YourUsername/Schema(replace with your actual path). - Validate Mount Access: Manually check if the container can see your SQL file:
Ifdocker exec -it mysql-db bash ls /docker-entrypoint-initdb.dtest.sqldoesn't show up, your mount path is incorrect—double-check the path and shared drive settings.
2. Fix Line Ending Differences
Mac uses LF line endings, while Windows uses CRLF. MySQL can throw syntax errors when parsing CRLF-formatted SQL files.
- Open
test.sqlin VS Code or Notepad++ - Switch line endings to LF (in VS Code, look for the "CRLF" indicator in the bottom-right corner and click to change to "LF")
- Save the file and re-run your restore process
3. Ensure MySQL Version Consistency
This is a common gotcha! If you're using a different MySQL image version on Windows vs. Mac (e.g., 8.0 on Windows, 5.7 on Mac), schema syntax differences (like authentication plugins) will cause failures.
- Check your Mac's
docker-compose.ymlor rundocker imageson Mac to get the exact MySQL version - Update your Windows
docker-compose.ymlto use that exact version (e.g.,mysql:5.7.34instead of justmysql:5.7)
4. Manually Execute the SQL to Get Exact Errors
If the above fixes don't work, run the SQL manually to get precise error messages:
# Copy the SQL file into the container if mounting is failing docker cp ./Schema/test.sql mysql-db:/tmp/test.sql # Enter the container and run the SQL docker exec -it mysql-db bash mysql -uroot -pyour_root_password your_target_db_name < /tmp/test.sql
The error output here will tell you exactly what's breaking (e.g., syntax error, missing permissions) so you can fix it directly.
内容的提问来源于stack exchange,提问作者Kyle Sletten

