我有mongodb replica set
以下配置:
- M1 初级
- M2二级
- M3二级
- M4仲裁员
这是我连接到数据库的 Express 代码(我mongoose
用作 ODM):
const config = {
db: `mongodb://${process.env.DB_ADDRESS_1}/${process.env.DB_NAME},
${process.env.DB_ADDRESS_2}/${process.env.DB_NAME},
${process.env.DB_ADDRESS_3}/${process.env.DB_NAME}`,
dbOptions: {
server: {
socketOptions: {
keepAlive: 1
},
poolSize: 5,
readPreference: 'nearest'
},
replSet: {
rs_name: process.env.REPLICA_SET,
poolSize: 5,
readPreference: 'nearest',
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS: 0
}
},
db: {
w: 1,
numberOfRetries: 2
}
}
}
mongoose.connect(config.db, config.dbOptions, (error) => {
if (error) {
console.log('Error on connecting to th db: ', error)
console.log('\x1b[31m', '*** PLEASE CONNECT TO DATABASE BEFORE RUN SERVER', '\x1b[0m')
process.exit(1)
}
callback()
})
该应用程序按预期工作。当 M1 关闭时,M2 或 M3 都被选为primary
,但我的express
应用程序仍然无法连接到replica set
.
我的配置有什么问题吗?