Mongodb

mongo 作為服務錯誤:無法確定伺服器的狀態

  • August 12, 2014

我正在嘗試將 mongodb 用作服務並從 heroku 服務開始,即dashboard.heroku.com/apps/scaleqamongo/resources。我嘗試了多個他們稱之為 mongolab、mongohq 的外掛。我創建數據庫,然後嘗試將該服務地址添加到我的應用程序中

db: 'mongodb://username:password@candidate.19.mongolayer.com:10292,candidate.18.mongolayer.com:10292/app25406086',

但它給了我錯誤

Error: Cannot determine state of server
Error: No replica set primary available for query with ReadPreference PRIMARY
   at ReplSet.checkoutReader (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/repl_set/repl_set.js:585:14)
   at Cursor.nextObject (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/cursor.js:723:48)
   at Cursor.nextObject (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/scope.js:22:20)
   at Collection.findOne (/prod/app/dashboard/node_modules/mongoose/node_modules/mongodb/lib/mongodb/collection/query.js:157:10)
   at /prod/app/dashboard/node_modules/connect-mongo/lib/connect-mongo.js:214:18
   at MongoStore._get_collection (/prod/app/dashboard/node_modules/connect-mongo/lib/connect-mongo.js:148:21)
   at MongoStore.get (/prod/app/dashboard/node_modules/connect-mongo/lib/connect-mongo.js:213:10)
   at Object.session [as handle] (/prod/app/dashboard/node_modules/express/node_modules/connect/lib/middleware/session.js:311:11)
   at next (/prod/app/dashboard/node_modules/express/node_modules/connect/lib/proto.js:193:15)
   at Object.methodOverride [as handle] (/prod/app/dashboard/node_modules/express/node_modules/connect/lib/middleware/methodOverride.js:48:5)

雖然我可以使用 shell 命令連接到數據庫

mongo candidate.19.mongolayer.com:10292/app25406086 -u username -p password

問題是什麼?為什麼我無法連接?

這是 Mongoose 驅動程序/ODM 的錯誤。

試試這個(取自文件):

var uri = "mongodb://username:password@candidate.19.mongolayer.com:10292,candidate.18.mongolayer.com:10292/app25406086"
var options = {
 replset: { rs_name: 'myReplicaSetName' }
}
mongoose.connect(uri, options);

引用自:https://serverfault.com/questions/596955