Creating a Mongo replicaset using docker_ Mongo replicaset + Nodejs + Docker Compose

参考 https://www.youtube.com/watch?v=mlw7vWISaF4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
version: '3'
services:
mongo-rs0-1:
image: "mongo-start"
build: ./mongo-rs0-1
ports:
- '27017:27017'
volumes:
- ./mongo-rs0-1/data:/data/db
- ./mongo-rs0-1/keyfile:/usr/src/configs/keyfile
- ./mongo-rs0-1/log:/usr/src/configs/logs
- ./mongo-rs0-1/backup:/usr/src/configs/backup
depends_on:
- 'mongo-rs0-2'
- 'mongo-rs0-3'

mongo-rs0-2:
image: 'mongo'
build: ./mongo-rs0-2
ports:
- '27018:27017'
volumes:
- ./mongo-rs0-2/data:/data/db
- ./mongo-rs0-2/keyfile:/usr/src/configs/keyfile
- ./mongo-rs0-2/log:/usr/src/configs/logs

mongo-rs0-3:
image: 'mongo'
build: ./mongo-rs0-3
ports:
- '27019:27017'
volumes:
- ./mongo-rs0-3/data:/data/db
- ./mongo-rs0-3/keyfile:/usr/src/configs/keyfile
- ./mongo-rs0-3/log:/usr/src/configs/logs

setup-rs:
image: "setup-rs"
build: ./setup
volumes:
- ./setup/replicaSet.js:/usr/src/configs/replicaSet.js
- ./setup/setup.sh:/usr/src/configs/setup.sh
depends_on:
- 'mongo-rs0-1'

有几个问题

容器setup-rs执行总是失败,导致副本集无法创建,只能进入容器mongo-rs0-1,执行replicaSet.js里
的内容创建副本集

1
2
3
4
5
6
7
8
9
10
11
rsconf = {
_id: "rs0",
members: [
{_id: 0, host: "mongo-rs0-1:27017"},
{_id: 1, host: "mongo-rs0-2:27017"},
{_id: 2, host: "mongo-rs0-3:27017"}
]
}

rs.initiate(rsconf);
rs.conf();

之后创建用户。
因为在本地你可以不使用keyfile。如果使用要注意权限问题