我能够连接到我的主数据库没问题,但是当我尝试连接到我的副本集时,我收到了TTL错误。我已尽力包含所有相关代码示例,但请询问您是否需要查看未包含的内容。这让我开始香蕉。数据库位于mongoHQ。
mongoHQ = "mongodb://<user>:<password>@candidate
.14.mongolayer.com:10120/dbName,mongodb://<user>:<password>@candidate
.15.mongolayer.com:10120"
failingDB = "mongodb://<user>:<password>@candidate
.14.mongolayer.com:10120/dbName"
workingDB = "mongodb://<user>:<password>@candidate
.15.mongolayer.com:10120/dbName"
# DB Options
opts =
mongos: true
server:
auto_reconnect: true
# Connect to DB
mongoose.connect mongoHQ, opts
# express/mongo session storage
app.use express.session(
secret: "Secrets are for children"
cookie:
maxAge: process.env.SESSION_TTL * 3600000
httpOnly: false
store: new mongoStore(
url: mongoHQ
collection: "sessions"
, ->
console.log "We're connected to the session store"
return
)
)
# Error: Error setting TTL index on collection : sessions
# * Connecting to "workingDB" works as expected.
# * Connecting to "failingDB" throws the same TTL Error
# * candidate.14 is the primary set, candidate.15 is the replica set
答案 0 :(得分:1)
也许有点晚了,但我今天在MongoHQ和Mongoose上遇到了类似的错误。我通过删除选项mongos: true
解决了这个问题(至少目前,手指交叉)。我认为副本集并不真正需要该选项(仅在使用mongos服务器时):
http://mongoosejs.com/docs/connections.html#replicaset_connections
http://support.mongohq.com/languages/mongoose.html
此外,最好在尝试设置MongoStore之前等待建立连接,例如:
mongoose.connect(mongoHQ);
var db = mongoose.connection;
db.on('error', function () {
// Panic
});
db.on('connected', function() {
var app = express();
app.use(express.session(sessionOptions));
// etc...
app.listen(config.port, function() {
console.log('Listening on port ', config.port);
});
});
答案 1 :(得分:-1)
我只需删除对URI
中副本集的引用即可解决此问题mongoHQ = "mongodb://<user>:<password>@candidate.15.mongolayer.com:10120/dbName";