如何在Windows 10上挂载Docker MySQL数据/套接字卷?

ws51t4hk  于 2022-11-21  发布在  Mysql
关注(0)|答案(3)|浏览(217)

我试图简单地在windows10上运行最新的docker mysql映像,并从主机挂载一个数据目录。
我在跑

docker run --rm --name=mysql --mount type=bind,src="C:\Docker Projects\mysql\mysql_data",dst=/var/lib/mysql mysql/mysql-server

但它却在抱怨一个套接字不存在。

2020-01-20T17:23:22.758269Z 0 [ERROR] [MY-010270] [Server] Can't start server : Bind on unix socket: Operation not permitted
2020-01-20T17:23:22.758416Z 0 [ERROR] [MY-010258] [Server] Do you already have another mysqld server running on socket: /var/lib/mysql/mysql.sock ?

我可以看到一个mysql mysql.sock.lock被创建了。我也尝试过从主机创建一个mysql.sock文件。
我想这是告诉我,我需要指定套接字文件在哪里为windows https://dev.mysql.com/doc/refman/5.7/en/deploy-mysql-nonlinux-docker.html,但我似乎不能得到正确的命令语法?

jyztefdp

jyztefdp1#

你是对的。问题是mysql.sock文件被放到了/var/lib/mysql中,而这个文件被挂载到了一个不支持Unix域套接字的Windows主机系统目录中。
启动服务器时只需使用开关--socket=/tmp/mysql.sock

k4ymrczo

k4ymrczo2#

如果您需要此文件用于docker-compose,则可以使用以下docker-compose.ymlmy.cnf文件。
在my.cnf文件中,套接字位置从socket=/var/lib/mysql/mysql.sock更改为socket=/tmp/mysql.sock

停靠-撰写.yml

version: "3.7"
services:
  mysql:
    image: mysql/mysql-server:5.7.27
    volumes:
      - "./my.cnf:/etc/my.cnf"

我的.cnf

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/tmp/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
xcitsw88

xcitsw883#

2022更新如果您希望使用Docker Hyper-V为Docker(Windows)MySQL创建持久数据库
1.在您希望存放DB数据的任何位置创建一个文件夹,例如D:\DockerDB
1.现在,在Hyper-v模式下使用Docker。
1.转到设置-〉资源-〉文件共享-〉添加您的DB数据文件夹在这里。(D:\DockerDB)和应用和重启。x1c 0d1x
在您的docker-compose.yml文件中,按如下方式添加卷

mysqldb:
    container_name: mysqldb
    image: mysql
    volumes:
      - D:/DockerDB:/var/lib/mysql

现在,您的数据将保存在本地机器的DB中。

相关问题