[英]Express can't connect to mongodb replica set when new primary get elected
I have a mongodb replica set
with the following config: 我有一个具有以下配置的
mongodb replica set
:
Here is my Express code to connect to db (I use mongoose
as the ODM): 这是我连接到数据库的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()
})
The app works as expected. 该应用程序可以正常运行。 When M1 get down, either M2 or M3 get elected to be the
primary
, BUT my express
app still CAN'T connect to the replica set
. 当M1掉线时,无论M2还是M3被选
primary
,但我的express
应用仍然无法连接到replica set
。
Is there any wrong in my config? 我的配置有问题吗?
Connection URL should be: 连接网址应为:
`mongodb://${process.env.DB_ADDRESS_1},${process.env.DB_ADDRESS_2},${process.env.DB_ADDRESS_3}/${process.env.DB_NAME}`
So, there is a list of nodes and after that comes information about the database. 因此,有一个节点列表,然后是有关数据库的信息。
And your setup has one flaw too... When you have three node replica set, you SHOULD NOT have arbitrator as forth! 而且您的设置也有一个缺陷...当您设置了三个节点副本时,您不应设置仲裁者! This because, vote count must be odd not even .
这是因为,投票数必须不是奇数 。
Your MongoDB URI connection string is not following the proper format. 您的MongoDB URI连接字符串未遵循正确的格式。 The database name should be appended after the server list, and not appended on every server.
数据库名称应附加在服务器列表之后,而不是附加在每台服务器上。 Try:
尝试:
mongodb://${process.env.DB_ADDRESS_1},
${process.env.DB_ADDRESS_2},
${process.env.DB_ADDRESS_3}
/${process.env.DB_NAME}?replicaSet=${process.env.REPLICA_SET}
See https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options for an example. 有关示例,请参见https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options 。
On another note, you don't need an arbiter on the replica set. 另外,副本集上不需要仲裁器。 It's not recommended to have an even number of nodes in a replica set, since it requires a majority to elect a primary.
不建议在副本集中有偶数个节点,因为它需要多数才能选举一个主节点。
With 4 nodes, if 2 of the nodes are down, the remaining nodes are not able to elect a primary (which is also the case when you have a 3-node replica set). 对于4个节点,如果其中2个节点关闭,则其余节点将不能选择主节点(当您具有3节点副本集时也是如此)。 Hence, the additional arbiter doesn't add any value to the replica set.
因此,附加仲裁程序不会向副本集添加任何值。 See https://docs.mongodb.com/manual/core/replica-set-architectures/#consider-fault-tolerance for more information.
有关更多信息,请参见https://docs.mongodb.com/manual/core/replica-set-architectures/#consider-fault-tolerance 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.