简体   繁体   English

MongoDb Replica Set write concern issues on Secondary IP, while Primary IP is Shutdown

[英]MongoDb Replica Set write concern issues on Secondary IP, while Primary IP is Shutdown

I am using three nodes for my MongoDB replica set, one as the primary node, one as a secondary node, and one as an arbitrator node.我为我的 MongoDB 副本集使用三个节点,一个作为主节点,一个作为辅助节点,一个作为仲裁节点。 I am using the following connection string in Golang to connect with MongoDB.我在 Golang 中使用以下连接字符串来连接 MongoDB。

opts = options.Client().ApplyURI("mongodb://" + ipPort + "," + secIpPort + "/?replicaSet=" + repSetName).SetConnectTimeout(5 * time.Second).SetAuth(serverAuth).SetWriteConcern(writeconcern.New(writeconcern.WMajority()))

Ip used to make connection are, ipPort= 192.168.1.237 secipPort= 192.168.1.239 Ip用来做连接,ipPort=192.168.1.237 secipPort=192.168.1.239

I have setted the Default RW Concern as following,我已将默认 RW 问题设置如下,

db.adminCommand({ "setDefaultRWConcern" : 1, "defaultWriteConcern" : { "w" : "majority", "j":true, "wtimeout" : 5000 }, writeConcern: { "w" : "majority", "j":true, "wtimeout" : 5000 }, })

Problem:问题:

When the MongoDb service of the first IP(192.168.1.237) of the connection string is shut down, the secondary IP(192.168.1.239) performs all the read operations well.当连接串的第一个IP(192.168.1.237)的MongoDb服务关闭时,副IP(192.168.1.239)很好的执行了所有的读操作。 But does not perform the write operations.How can I deal with this problem?但不执行写操作。我该如何处理这个问题? If the first IP of the connection string is down, the secondary IP/node should be able to perform both read and write operations.如果连接字符串的第一个 IP 挂了,从 IP/节点应该可以同时执行读写操作。

How can I deal with this problem?我该如何处理这个问题? If the first IP of the connection string is down, the secondary IP/node should be able to perform both read and write operations.如果连接字符串的第一个 IP 挂了,从 IP/节点应该可以同时执行读写操作。

The reason is writeConcern: { w: "majority" }原因是writeConcern: { w: "majority" }

In MongoDB you have two majorities.在 MongoDB 中,您有两个多数。 The voting majority and the write majority.表决多数和书面多数。 You can interrogate them with rs.status()你可以用rs.status()询问他们

In a PRIMARY-SECONDAY-ARBITER (PSA) Replica Set, it should look like this:在 PRIMARY-SECONDAY-ARBITER (PSA) 副本集中,它应该如下所示:

"majorityVoteCount" : 2
"writeMajorityCount" : 2
"votingMembersCount" : 3
"writableVotingMembersCount" : 2

If the SECONDARY goes down, then you have 2 voting member which is the majority.如果 SECONDARY 出现故障,那么您有 2 个投票成员占多数。 They can still elect a new PRIMARY.他们仍然可以选举一个新的 PRIMARY。

However, only 1 writing member is left, 1 out of 2 is not the majority.但是,只剩下 1 个写作成员,2 个中有 1 个不是多数。 Thus operation with { w: "majority" } are not possible unless the missing member comes back again.因此除非失踪成员再次回来,否则无法使用{ w: "majority" }进行操作。

Some background information can be found here: MongoClient to connect to multiple hosts to handle failover?一些背景信息可以在这里找到: MongoClient to connect to multiple hosts to handle failover?

When the primary node of the MongoDB replica set is down, the secondary node should take over as the new primary and continue to perform both read and write operations.当MongoDB副本集的primary节点宕机时,secondary节点应该接替成为新的primary,继续进行读写操作。 However, it seems that in your case, the secondary node is not able to perform write operations when the primary node is down.但是,在您的情况下,当主节点关闭时,辅助节点似乎无法执行写操作。

There are a few possible reasons for this:这有几个可能的原因:

The replica set configuration is not correct.副本集配置不正确。 Make sure that the replica set name and the list of nodes in the replica set match what is configured in MongoDB.确保副本集名称和副本集中的节点列表与 MongoDB 中配置的相匹配。

The secondary node is not able to reach a quorum of nodes to perform a write operation.辅助节点无法达到法定数量的节点来执行写入操作。 In your case, you are using an arbiter node that does not store data and can't vote on the write operation.在您的情况下,您使用的是不存储数据且无法对写入操作进行投票的仲裁节点。 You may want to consider removing the arbiter node from the replica set or adding another secondary node to ensure that a quorum of nodes is always available for write operations.您可能需要考虑从副本集中删除仲裁节点或添加另一个辅助节点以确保一定数量的节点始终可用于写入操作。

The write concern is not configured correctly.写入问题配置不正确。 In your code, you are setting the write concern to "majority" and "j=true".在您的代码中,您将写入关注设置为“多数”和“j=true”。 The write concern "j=true" means that a write operation only returns after the write is written to the on-disk journal. write concern "j=true" 表示写操作仅在写操作写入磁盘日志后返回。 Make sure that the journaling is enabled on the MongoDB and the disk space is enough.确保在 MongoDB 上启用了日志功能并且磁盘空间足够。

The.network connectivity between the nodes is not stable.节点之间的网络连接不稳定。 Make sure that the nodes can communicate with each other and that there are no firewalls or other.network issues that are preventing the nodes from communicating.确保节点可以相互通信,并且不存在阻止节点通信的防火墙或其他网络问题。

The secondary node is not up-to-date with the primary node.辅助节点与主节点不同步。 Make sure that the secondary node is catching up with the primary node's oplog and that the secondary node is in a healthy state.确保辅助节点正在赶上主节点的 oplog,并且辅助节点处于健康状态 state。

It's recommended to check the MongoDB logs and running the command "rs.status()" to check the state of the replica set and see if there are any error messages.建议查看MongoDB日志,运行命令“rs.status()”查看副本集的state是否有报错信息。 You can also try to connect to the secondary node directly and check the state of the replica set with the command "rs.status()" and check the state of the replica set and see if there are any error messages.也可以尝试直接连接secondary节点,通过命令“rs.status()”查看副本集的state,查看副本集的state,看是否有报错信息。

If the problem persists, you may want to consider adding another secondary node to ensure that a quorum of nodes is always available for write operations.如果问题仍然存在,您可能需要考虑添加另一个辅助节点以确保一定数量的节点始终可用于写入操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM