On this page
rs.reconfig()
在本页面
Definition
rs.
reconfig
(* configuration , force *)- 重新配置现有副本集,覆盖现有副本集配置。要运行该方法,必须连接到副本集的primary。
Parameter | Type | Description |
---|---|---|
configuration |
document | document,用于指定副本集的配置。 |
force |
document | 可选的。如果设置为{ force: true } ,即使大多数成员不可访问,这也会强制副本集接受新配置。请谨慎使用,因为这可能导致rollback情况。 |
要重新配置现有副本集,请首先使用rs.conf()检索当前配置,根据需要修改配置文档,然后将修改后的文档传递给rs.reconfig()。
rs.reconfig()提供围绕replSetReconfig命令的包装。
force
参数允许向非主节点发出重新配置命令。
Consideration
混合版本副本集
Warning
避免重新配置包含不同 MongoDB 版本成员的副本集,因为验证规则在 MongoDB 版本之间可能有所不同。
Availability
rs.reconfig() shell 方法可以在某些情况下触发当前的主数据库降级。当主服务器降级时,它将强制关闭所有 Client 端连接。主降压触发election选择新的primary。
假设默认为副本配置设置,集群选择新的主要数据库之前的中值时间通常不应超过 12 秒。这包括将主要对象标记为unavailable并调用并完成election所需的时间。您可以通过修改settings.electionTimeoutMillis复制配置选项来调整此时间段。网络延迟之类的因素可能会延长完成副本集选举所需的时间,进而影响群集在没有主数据库的情况下可以运行的时间。这些因素取决于您的特定群集体系结构。
在选择过程中,群集在选择新的主数据库之前不能接受写操作。
您的应用程序连接逻辑应包括对自动故障转移和后续选举的容忍度。
3.6 版中的新功能:MongoDB 3.6 驱动程序可以一次检测到主数据库的丢失并自动重试某些写入操作,从而提供了对自动故障转移和选举的其他内置处理。
为了进一步减少对生产集群的潜在影响,请仅在计划的维护期间重新配置。
{ force: true }
Warning
将rs.reconfig()与{ force: true }
结合使用会导致rollback的提交写入。使用此选项时请多加注意。
会员优先级和投票
在版本 3.2 中更改。
protocolVersion
更改protocolVersion之前,请确保至少有一个 oplog 条目(从当前协议版本生成)已从主服务器复制到所有辅助服务器。有关详细信息,请参见修改副本集协议版本。
Examples
名为rs0
的副本集具有以下配置:
{
"_id" : "rs0",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("58858acc1f5609ed986b641b")
}
}
以下操作序列更新第二个成员的members[n].priority。通过连接到主数据库的mongo shell 发出操作。
cfg = rs.conf();
cfg.members[1].priority = 2;
rs.reconfig(cfg);
第一条语句使用rs.conf()方法为副本集检索包含当前configuration的文档,并将该文档设置为局部变量
cfg
。第二条语句为members数组中的第二个文档设置members[n].priority值。有关其他设置,请参见副本集配置设置。
要访问数组中的成员配置文档,该语句使用数组索引和副本集成员的members[n]._id字段(而非**)。
- 最后一条语句使用修改后的
cfg
调用rs.reconfig()方法以初始化此新配置。成功重新配置后,副本集配置将类似于以下内容:
{
"_id" : "rs0",
"version" : 2,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "mongodb0.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "mongodb1.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "mongodb2.example.net:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("58858acc1f5609ed986b641b")
}
}