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

Vagrant共享存储环境下Docker Compose部署MySQL持久化卷异常求助

Fixing MySQL Container Permissions & Non-Empty Data Directory Issues in Vagrant Shared Folders

Hey there, let's tackle these two MySQL container problems you're facing—they're super common when using shared folders between Windows hosts and Linux VMs with Docker, so we've got this sorted!

First, Let's Break Down the Root Causes

Scenario 1: Permission Denied Error

The core issue here is a user ID mismatch. Your VM's vagrant user has a UID/GID of 1000, but the mysql user inside the Docker container uses a default UID of 999. Even though the shared folder has wide-open 777 permissions, Linux enforces access based on UID/GID, not just permission bits. The container's mysql user simply doesn't have the right to write to a directory owned by UID 1000.

Scenario 2: Non-Empty Data Directory Error

When you added :rw to the volume mount, you fixed the permission barrier—but now the container could see leftover files from your earlier failed initialization attempts. MySQL requires the data directory to be completely empty when running its first-time setup, so it aborts when it finds existing files.

Step-by-Step Solutions

1. Clean Up the Leftover Data Files

First, let's clear out any partial files from the shared data folder to fix the non-empty directory error:

# Stop any running containers first
docker-compose down

# Delete all files inside the mysql data folder
rm -rf /vagrant/test/data/mysql/*

2. Update Docker Compose to Match User Permissions

Modify your docker-compose-file.yml to run the MySQL container using the same UID/GID as your VM's vagrant user. This ensures the container's process has full access to the shared folder:

version: '3'
services:
  mysqldb:
    image: mysql:8
    container_name: mysql_test
    env_file:
      - ./env/mysql.env
    command: --default-authentication-plugin=mysql_native_password
    restart: 'always'
    ports:
      - "6620:3306"
    volumes:
      - ./data/mysql:/var/lib/mysql
    user: "1000:1000"  # Add this line to match your VM's vagrant user UID/GID

3. (Optional) Pre-Create the Data Directory

If the data/mysql directory doesn't exist yet, create it manually to avoid any auto-creation permission quirks:

mkdir -p /vagrant/test/data/mysql

4. Start the Container

Now fire up the container again—it should initialize successfully this time:

docker-compose up -d

Why This Works

  • Setting user: "1000:1000" tells Docker to run the MySQL process using the same user identity as your VM's vagrant account. Since the shared folder is owned by this UID, the container gets full read/write access automatically.
  • Clearing the data directory removes any partial setup files from failed attempts, letting MySQL run its initialization process from scratch without conflicts.

内容的提问来源于stack exchange,提问作者sanjeeth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 13:52:40