You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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.sql is enabled in Docker's File Sharing settings (as noted above).
  • Check Path Format: Windows uses backslashes (\) but Docker expects forward slashes (/). In docker-compose.yml, use relative paths like ./Schema or 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:
    docker exec -it mysql-db bash
    ls /docker-entrypoint-initdb.d
    
    If test.sql doesn'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.sql in 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.yml or run docker images on Mac to get the exact MySQL version
  • Update your Windows docker-compose.yml to use that exact version (e.g., mysql:5.7.34 instead of just mysql: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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:09:58