我發現了一個教程建立一個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
所以我的問題是如何解決這個問題。謝謝
你檢查了教程中的第一條評論嗎? 「...因爲它無法解析主機上的主機名mongo1-3,所以失敗。」解決方案也是詳細的,它對你有用嗎?我指的是這個網站:http://www.sohamkamani.com/blog/2016/06/30/docker-mongo-replica-set/ – alexbt
謝謝,我錯過了這個評論 – Bryan