3

我找到了一个使用 docker 设置 mongo 副本集的教程,我的命令是

创建网络集群

sudo docker network create curator-cluster

创建一个名为 的特定容器mongo1,将 27018 映射到 27017 内部并设置名称为rs0

sudo docker run \
-p 27018:27017 \
--name mongo1 \
--net curator-cluster \
mongo mongod --replSet rs0

我的配置,

config = {
"_id" : "rs0",
"members" : [{"_id" : 0, "host" : "mongo1:27017"},
{"_id" : 1, "host" : "mongo2:27017"},
{"_id" : 2, "host" : "mongo3:27017"}]
}

最终,我创建了 3 个容器

5949826d5bb1        mongo                        "/entrypoint.sh mongo"   22 hours ago        Up 22 hours         0.0.0.0:27020->27017/tcp   mongo3
dcf37866dbb6        mongo                        "/entrypoint.sh mongo"   22 hours ago        Up 22 hours         0.0.0.0:27019->27017/tcp   mongo2
14202f76089f        mongo                        "/entrypoint.sh mongo"   22 hours ago        Up 22 hours         0.0.0.0:27018->27017/tcp   mongo1

结果sudo docker exec -it mongo1 mongo

MongoDB shell version: 3.2.9
connecting to: test
Server has startup warnings:
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten]
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten]
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten]
rs0:PRIMARY>

看起来我现在有一个主要的,并试图在容器上插入一些东西(比如 mongo1),MongoDB 可以很好地同步。

现在我尝试使用命令连接到bryan数据库上的集合(注意10.145.168.151是我的 IP)

mongo --host rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020 bryan

我的结果是

MongoDB shell version: 2.6.9
connecting to: rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020/bryan
2016-09-23T16:46:18.819+0800 starting new replica set monitor for replica set rs0 with seeds 10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020
2016-09-23T16:46:18.819+0800 [ReplicaSetMonitorWatcher] starting
2016-09-23T16:46:18.819+0800 changing hosts to rs0/mongo1:27017,mongo2:27017,mongo3:27017 from rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020
2016-09-23T16:46:18.820+0800 getaddrinfo("mongo2") failed: Name or service not known
2016-09-23T16:46:18.821+0800 getaddrinfo("mongo1") failed: Name or service not known
2016-09-23T16:46:18.822+0800 getaddrinfo("mongo3") failed: Name or service not known
2016-09-23T16:46:18.822+0800 Error: connect failed to replica set rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020 at src/mongo/shell/mongo.js:148
exception: connect failed

如果我使用 Nodejs(猫鼬),我会得到MongoError: no primary found in replicaset

我认为问题出在getaddrinfo("mongo2") failed: Name or service not known所以我的问题是如何解决这个问题。谢谢

4

1 回答 1

4

很抱歉回复晚了,但我遇到了这个问题,有不同的 vnet 和缺少主机名。我的印象是,如果我使用 ips 连接,那么集群将使用 ips 响应。我错了。即使您使用 ips 连接,主机名也必须可用。

但是,如果您愿意,您可以将实现从主机名更改为 ips(但这适用于所有客户端)

1) 连接到 mongo cli

2)cfg = rs.conf()

你会看到cfg.members[0].host"hostname:27017"

3)为每个条目做cfg.members[i].host = "ip(i):27017"

例子: cfg.members[0].host = "10.0.0.1:27017"

4)rs.reconfig(cfg) 你应该得到回应: { "ok" : 1 }

现在您应该可以连接了。这有一些警告,所以一定要考虑后果(如果 ips 发生变化等)

于 2016-12-09T19:16:53.760 回答