mongo-replSet
mongodb 副本集配置
#docker-compose.yml
version: "3"
services:
mongo1:
build: .
hostname: mongo1
container_name: localmongo1
image: mongo:gao
expose:
- 27017
ports:
- 27011:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo2:
build: .
hostname: mongo2
container_name: localmongo2
image: mongo:gao
expose:
- 27017
ports:
- 27012:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo3:
build: .
hostname: mongo3
container_name: localmongo3
image: mongo:gao
expose:
- 27017
ports:
- 27013:27017
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
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
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
Dockerfile
FROM mongo:latest
1
2
2
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "mongo1:27017" }, # 不要使用容器的域名
{ _id : 1, host : "mongo2:27017" }, # 容器域名使用docker-compose 进行域名解析
{ _id : 2, host : "mongo3:27017" } # 若在docker-compose外连接mongo会失败
] # 这种情况要把所有服务都部署在同一个compose内
}
)
# 可使用下面的IP配置,其实这里的配置方法只是用于测试,实际上mongo副本集是跨物理机的,这里应该是
# 要配置IP的,只是个人模拟的时候会出问题而已
rs.initiate(
{
_id : 'rs0',
members: [
{ _id : 0, host : "172.16.4.132:27017" },
{ _id : 1, host : "172.16.4.132:27017" },
{ _id : 2, host : "172.16.4.132:27017" }
]
}
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
注意副本集这里,
编辑 (opens new window)
上次更新: 2023/02/24, 10:34:03