1

我發現了一個教程建立一個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(貓鼬),我得到MongoError: no primary found in replicaset

我認爲問題是getaddrinfo("mongo2") failed: Name or service not known所以我的問題是如何解決這個問題。謝謝

+0

你檢查了教程中的第一條評論嗎? 「...因爲它無法解析主機上的主機名mongo1-3,所以失敗。」解決方案也是詳細的,它對你有用嗎?我指的是這個網站:http://www.sohamkamani.com/blog/2016/06/30/docker-mongo-replica-set/ – alexbt

+0

謝謝,我錯過了這個評論 – Bryan

回答

2

對不起,對於最近的回覆,但我遇到了這個問題與不同的vnets和缺少主機名。如果我使用ips連接,那麼我的印象是這樣,然後集羣會使用ips來響應。我錯了。即使您使用ips連接,主機名也必須可用。

但是,如果你願意,你可以改變從主機名到IPS的實現(這是因爲雖然所有客戶端)

1)連接到蒙戈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 }

現在你應該能夠連接。這有一些注意事項,所以一定要考慮後果(如果更改等)

+0

謝謝。這有幫助。 – Neil