Vagrant共享存储挂载Docker MySQL容器持久化卷失败问题求助
Hey there, I see you're stuck getting MySQL to run with a Vagrant shared folder as its persistent data volume. Let's break down what's causing the issues and walk through solutions for both of your scenarios.
Why the Errors Occur
Scenario 1: Empty /vagrant/mysql-data Directory
The error Operating system error number 95 (Operation not supported) comes down to Linux AIO (Asynchronous I/O) incompatibility. VirtualBox's shared folder filesystem (vboxsf) doesn't support native AIO, but MySQL 8.0 uses this feature by default for better performance. This mismatch crashes the database during initialization.
Scenario 2: Non-Empty /vagrant/mysql-data Directory
MySQL's entrypoint script has a safety check: if the data directory has existing files, it aborts the initialization process (since it assumes you intended to start fresh). Even if those files are valid MySQL data, the script triggers this error to prevent accidental overwrites.
Solutions to Try
1. Disable Native AIO for Shared Folder Compatibility
If you need to stick with the Vagrant shared folder, disable InnoDB's native AIO support to work around the vboxsf limitation. Update your docker run command with the --innodb-use-native-aio=0 flag:
docker run -d --name=test-mysql4 -e MYSQL_ROOT_PASSWORD=mypassword -p 6604:3306 -v /vagrant/mysql-data:/var/lib/mysql mysql --innodb-use-native-aio=0
This tells InnoDB to use simulated AIO instead of the system's native implementation, which plays nicely with shared folders.
2. Use a Local Directory in the Vagrant VM (Recommended)
Shared folders are not ideal for database storage—they have slower I/O and frequent compatibility quirks. A better approach is to use a local directory inside the Vagrant guest:
- First, create a dedicated directory in the guest machine:
sudo mkdir -p /opt/mysql-data sudo chown -R vagrant:vagrant /opt/mysql-data - Mount this local directory to your MySQL container:
docker run -d --name=test-mysql4 -e MYSQL_ROOT_PASSWORD=mypassword -p 6604:3306 -v /opt/mysql-data:/var/lib/mysql mysql
If you need access to the data from your Windows host, set up a periodic sync (e.g., using rsync) between /opt/mysql-data and the shared folder, or use Vagrant's built-in sync options for specific files.
3. Reuse Existing Data in the Shared Folder
If you have valid MySQL data in /vagrant/mysql-data and want to reuse it, combine the AIO fix with skipping the entrypoint's initialization check. Run the container directly with mysqld to load the existing data:
docker run -d --name=test-mysql4 -e MYSQL_ROOT_PASSWORD=mypassword -p 6604:3306 -v /vagrant/mysql-data:/var/lib/mysql mysql mysqld --innodb-use-native-aio=0
This bypasses the initialization abort and loads your existing database files.
内容的提问来源于stack exchange,提问作者sanjeeth

