我找到了一个使用 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
所以我的问题是如何解决这个问题。谢谢