我尝试连接到在4个容器下运行的mongodb副本集:3个mongo作为集群,1个用于向其他人发出mongo命令。
在这里,我不知道如何使用mongodb网址将其连接到我的应用程序。
docker-compose.yml
是
mongo3:
image: mongo:latest
hostname: mongo3
expose:
- "27017"
- "28017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs"]
mongo2:
image: mongo:latest
hostname: mongo2
expose:
- "27017"
- "28017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs"]
mongo1:
image: mongo:latest
hostname: mongo1
expose:
- "27017"
- "28017"
links:
- mongo2:mongo2
- mongo3:mongo3
restart: always
entrypoint: [ "/usr/bin/mongod", "--replSet", "rs"]
mongosetup:
image: mongo:latest
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
volumes:
- ./scripts:/scripts
- ./data/runtime/db:/data/db
- ./data/dump:/dump
restart: always
entrypoint: [ "bash", "/scripts/mongo_setup.sh" ]
app:
image: papagari/swoop.chat:latest
environment:
- MONGO_URL=mongodb://mongo1:27017,mongo2:27017,mongo3:27017/swoop?replicaSet=rs
- ROOT_URL=https://localhost:3000
links:
- mongo1:mongo1
- mongo2:mongo2
- mongo3:mongo3
ports:
- 3000:3000
和/scripts/mongo_setup.sh
是
MONGODB1=`ping -c 1 mongo1 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB2=`ping -c 1 mongo2 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
MONGODB3=`ping -c 1 mongo3 | head -1 | cut -d "(" -f 2 | cut -d ")" -f 1`
echo "Waiting for startup.."
until curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1; do
printf '.'
sleep 1
done
echo curl http://${MONGODB1}:28017/serverStatus\?text\=1 2>&1 | grep uptime | head -1
echo "Started.."
sleep 10
echo SETUP.sh time now: `date +"%T" `
mongo --host ${MONGODB1}:27017 <<EOF
var cfg = {
"_id": "rs",
"version": 1,
"members": [
{
"_id": 0,
"host": "${MONGODB1}:27017",
"priority": 2
},
{
"_id": 1,
"host": "${MONGODB2}:27017",
"priority": 0
},
{
"_id": 2,
"host": "${MONGODB3}:27017",
"priority": 0
}
]
};
rs.initiate(cfg, { force: true });
rs.reconfig(cfg, { force: true });
db.getMongo().setReadPref('nearest');
EOF
现在我收到mongodb://mongo1:27017,mongo2:27017,mongo3:27017/swoop?replicaSet=rs
的错误
显然failed to connect to server
因为我使用的网址无效。我是第一次使用docker和mongo db副本设置的新手,试图在这里了解docker。它应该如何工作。
错误:MongoError: no primary found in replicaset
。
完成错误:
app_1 |
app_1 | /app/bundle/programs/server/node_modules/fibers/future.js:313
app_1 | throw(ex);
app_1 | ^
app_1 | MongoError: no primary found in replicaset
app_1 | at Object.Future.wait (/app/bundle/programs/server/node_modules/fibers/future.js:449:15)
app_1 | at new MongoConnection (packages/mongo/mongo_driver.js:216:27)
app_1 | at new MongoInternals.RemoteCollectionDriver (packages/mongo/remote_collection_driver.js:4:16)
app_1 | at Object.<anonymous> (packages/mongo/remote_collection_driver.js:38:10)
app_1 | at Object.defaultRemoteCollectionDriver (packages/underscore/underscore.js:750:1)
app_1 | at new Mongo.Collection (packages/mongo/collection.js:103:40)
app_1 | at AccountsServer.AccountsCommon (packages/accounts-base/accounts_common.js:23:18)
app_1 | at new AccountsServer (packages/accounts-base/accounts_server.js:18:5)
app_1 | at meteorInstall.node_modules.meteor.accounts-base.server_main.js (packages/accounts-base/server_main.js:9:12)
app_1 | at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
app_1 | - - - - -
app_1 | at /app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/replset.js:631:32
app_1 | at [object Object].<anonymous> (/app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/replset.js:421:24)
app_1 | at [object Object].g (events.js:260:16)
app_1 | at emitOne (events.js:77:13)
app_1 | at [object Object].emit (events.js:169:7)
app_1 | at /app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/server.js:283:14
app_1 | at /app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:436:18
app_1 | at nextTickCallbackWith0Args (node.js:420:9)
app_1 | at process._tickCallback (node.js:349:13)
我搜索了很多我得到的东西是本地机器或服务器机器而不是泊坞机链接。
更新
mongo1容器中rs.status()
的响应,看起来很好,主要和辅助都被正确初始化。
{
"set" : "rs",
"date" : ISODate("2016-10-19T06:31:10.861Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "172.17.0.4:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 62,
"optime" : {
"ts" : Timestamp(1476858630, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-19T06:30:30Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1476858629, 1),
"electionDate" : ISODate("2016-10-19T06:30:29Z"),
"configVersion" : 13918,
"self" : true
},
{
"_id" : 1,
"name" : "172.17.0.3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 51,
"optime" : {
"ts" : Timestamp(1476858630, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-19T06:30:30Z"),
"lastHeartbeat" : ISODate("2016-10-19T06:31:09.633Z"),
"lastHeartbeatRecv" : ISODate("2016-10-19T06:31:10.291Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.17.0.2:27017",
"configVersion" : 13918
},
{
"_id" : 2,
"name" : "172.17.0.2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 51,
"optime" : {
"ts" : Timestamp(1476858630, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-10-19T06:30:30Z"),
"lastHeartbeat" : ISODate("2016-10-19T06:31:09.633Z"),
"lastHeartbeatRecv" : ISODate("2016-10-19T06:31:09.283Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "172.17.0.4:27017",
"configVersion" : 13918
}
],
"ok" : 1
}
任何提示都会有所帮助。 TIA
答案 0 :(得分:1)
正如你在评论中所说,当你的replicaSet配置没问题时,可能你的app
正在与其他人一起开始,在配置replicaSet之前,它会中断。
您可以尝试在应用配置上使用depends_on,这将使app
容器在其他容器之后启动。但请注意,这可能不起作用,因为waits until the container is ready and not until the replicaSet is configured
如果这不起作用,我认为最简单的方法是在运行docker-compose后单独启动app
。