如何将 Docker 容器中的 MongoDB 数据挂载到 Windows 10 上的本地存储?

huangapple go评论70阅读模式
英文:

How to mount mongodb data from docker container to localhost storage on Windows 10?

问题

当我尝试使用以下命令为Mongo创建卷时:

docker run -p 27015:27017 -v C:\myPath\mongodump:/data/db mongo

我遇到了错误:

2020-01-03T10:08:58.002+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=bab5c142014f
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] db version v4.2.2
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] modules: none
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] build environment:
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten]     distarch: x86_64
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: "*" } }
2020-01-03T10:08:58.018+0000 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=478M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2020-01-03T10:08:58.613+0000 E  STORAGE  [initandlisten] WiredTiger error (1) [1578046138:613590][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1578046138:613590][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
2020-01-03T10:08:58.628+0000 E  STORAGE  [initandlisten] WiredTiger error (17) [1578046138:628759][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1578046138:628759][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists
2020-01-03T10:08:58.632+0000 I  STORAGE  [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.1
2020-01-03T10:08:58.637+0000 E  STORAGE  [initandlisten] WiredTiger error (1) [1578046138:637909][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1578046138:637909][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
2020-01-03T10:08:58.650+0000 E  STORAGE  [initandlisten] WiredTiger error (17) [1578046138:650974][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1578046138:650974][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists
2020-01-03T10:08:58.655+0000 I  STORAGE  [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.2
2020-01-03T10:08:58.660+0000 E  STORAGE  [initandlisten] WiredTiger error (1) [1578046138:660725][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1578046138:660725][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
2020-01-03T10:08:58.663+0000 W  STORAGE  [initandlisten] Failed to start up WiredTiger under any compatibility version.
2020-01-03T10:08:58.663+0000 F  STORAGE  [initandlisten] Reason: 1: Operation not permitted
2020-01-03T10:08:58.663+0000 F  -        [initandlisten] Fatal Assertion 28595
<details>
<summary>英文:</summary>
When I try to create volume for mongo using below command:
**docker run -p 27015:27017 -v C:\myPath\mongodump:/data/db mongo** 
I get the error:
2020-01-03T10:08:58.002+0000 I  CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols &#39;none&#39;
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=bab5c142014f
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] db version v4.2.2
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] git version: a0bbbff6ada159e19298d37946ac8dc4b497eadf
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] allocator: tcmalloc
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] modules: none
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] build environment:
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten]     distmod: ubuntu1804
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten]     distarch: x86_64
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten]     target_arch: x86_64
2020-01-03T10:08:58.006+0000 I  CONTROL  [initandlisten] options: { net: { bindIp: &quot;*&quot; } }
2020-01-03T10:08:58.018+0000 I  STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=478M,cache_overflow=(file_max=0M),session_max=33000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000,close_scan_interval=10,close_handle_minimum=250),statistics_log=(wait=0),verbose=[recovery_progress,checkpoint_progress],
2020-01-03T10:08:58.613+0000 E  STORAGE  [initandlisten] WiredTiger error (1) [1578046138:613590][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1578046138:613590][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
2020-01-03T10:08:58.628+0000 E  STORAGE  [initandlisten] WiredTiger error (17) [1578046138:628759][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1578046138:628759][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists
2020-01-03T10:08:58.632+0000 I  STORAGE  [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.1
2020-01-03T10:08:58.637+0000 E  STORAGE  [initandlisten] WiredTiger error (1) [1578046138:637909][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1578046138:637909][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
2020-01-03T10:08:58.650+0000 E  STORAGE  [initandlisten] WiredTiger error (17) [1578046138:650974][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists Raw: [1578046138:650974][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: File exists
2020-01-03T10:08:58.655+0000 I  STORAGE  [initandlisten] WiredTiger message unexpected file WiredTiger.wt found, renamed to WiredTiger.wt.2
2020-01-03T10:08:58.660+0000 E  STORAGE  [initandlisten] WiredTiger error (1) [1578046138:660725][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted Raw: [1578046138:660725][1:0x7f596d6b6b00], connection: __posix_open_file, 667: /data/db/WiredTiger.wt: handle-open: open: Operation not permitted
2020-01-03T10:08:58.663+0000 W  STORAGE  [initandlisten] Failed to start up WiredTiger under any compatibility version.
2020-01-03T10:08:58.663+0000 F  STORAGE  [initandlisten] Reason: 1: Operation not permitted
2020-01-03T10:08:58.663+0000 F  -        [initandlisten] Fatal Assertion 28595 at src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp 789
2020-01-03T10:08:58.663+0000 F  -        [initandlisten]
***aborting after fassert() failure
Related document says that it is because the Docker container is not compatible with the memory mapped files used by MongoDB. info: https://hub.docker.com/_/mongo .
Is there anyway to make mongodb volume mounting possible in windows?Or any other work around to just take mongo dump to local storage automatically (How to write it in docker-compose file)?
</details>
# 答案1
**得分**: 3
docker只能通过Linux文件系统访问。
要解决这个问题,您需要单独创建卷。
如下所示。请参阅此[链接][1]。
```shell
docker volume create --name=mongodata
docker run -d -p 27015:27017 -v mongodata:/data/db mongo
英文:

docker can only be accessed through the linux file system.

To solve this problem, you have to create volume separately.

like below. and please see this link

docker volume create --name=mongodata
docker run -d -p 27015:27017 -v mongodata:/data/db mongo

答案2

得分: 1

如果有人也遇到相同的问题,但是使用Docker Compose,以下解决方案修复了我的问题:

version: '3.3'
services:
  mongodb:
    image: mongo
    container_name: mongodb
    volumes:
      - mongodata:/data/db
    ports:
      - 27017:27017
volumes:
  mongodata:

参考链接

这也适用于版本2.3的Compose文件版本。

英文:

@r_zelazny
If anyone is also having the same error but with docker compose, the following solution fixed my problem

version: &#39;3.3&#39;
services:
mongodb:
image: mongo
container_name: mongodb
volumes:
- mongodata:/data/db
ports:
- 27017:27017
volumes:
mongodata:

Reference

This also works with version 2.3 compose file version

答案3

得分: 0

这在Windows上对我有效

  1. 创建文件夹 C:/mongodb/db

  2. 运行以下代码

version: '3.1'

services:

  mongodb:
    image: mongo
    ports:
      - 27018:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: user
      MONGO_INITDB_ROOT_PASSWORD: pass
    volumes:
      - C:/mongodb/db:/data/db 

所有数据库数据将存储在 C:/mongodb/db,使数据在Windows上持久化

谢谢!

英文:

This work for me on windows

  1. Create the folder &#39;C:/mongodb/db&#39;

  2. Run the next code with
    docker stack deploy -c stack.yml test


services:
mongodb:
image: mongo
ports:
- 27018:27017
environment:
MONGO_INITDB_ROOT_USERNAME: user
MONGO_INITDB_ROOT_PASSWORD: pass
volumes:
- C:/mongodb/db:/data/db 

All data from the database will be stored in &#39;C:/mongodb/db&#39; making the data persistent for windows

Regards!!

huangapple
  • 本文由 发表于 2020年1月3日 20:36:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578766.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定