我有mongodb replica set
以下配置:
- M1 (192.168.77.3) 主(主机 App1)
- M2 (192.168.77.4) 辅助(主机 App2)
- M3 (192.168.77.5) 辅助(主机 App3)
- M4仲裁员
这是我连接到数据库的 Express 代码(我mongoose
用作 ODM):
const config = {
db: `mongodb://192.168.77.3,192.168.77.4,192.168.77.5/mydb`,
dbOptions: {
useMongoClient: true,
reconnectTries: Number.MAX_VALUE,
replicaSet: process.env.REPLICA_SET,
poolSize: 100,
keepAlive: 1,
connectTimeoutMS: 30000,
socketTimeoutMS: 300000,
w: 1
}
}
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
,App2
并且App3
仍在工作,但App1
CAN'T 无法连接到replica set
并显示错误消息:
MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
这是我的 mongodb 配置App1
:
storage:
dbPath: /var/lib/mongodb
journal:
enabled: true
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
net:
port: 27017
bindIp: [127.0.0.1, 192.168.77.3]
replication:
oplogSizeMB: 3000
replSetName: my_rs
我的配置有什么问题吗?