我找到了一个设置mongo replica set using docker的教程,我的命令是
创建网络群集
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(mongoose),我会得到MongoError: no primary found in replicaset
我认为问题是getaddrinfo("mongo2") failed: Name or service not known
所以我的问题是如何解决这个问题。谢谢
答案 0 :(得分:3)
很抱歉迟到的回复,但我遇到了这个问题,包含不同的vnets和缺少的主机名。如果我使用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改变等等)