Replace a Config Server更换配置服务器
On this page本页内容
The following procedure applies to 6.0 config servers. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.以下过程适用于6.0配置服务器。有关MongoDB的早期版本,请参阅相应版本的MongoDB手册。
Overview概述
If the config server replica set becomes read only, i.e. does not have a primary, the sharded cluster cannot support operations that change the cluster metadata, such as chunk splits and migrations. 如果配置服务器副本集变为只读,即没有主副本集,则分片集群将无法支持更改集群元数据的操作,如区块分割和迁移。Although no chunks can be split or migrated, applications will be able to write data to the sharded cluster.尽管没有块可以被分割或迁移,但应用程序将能够将数据写入分片集群。
If one of the config servers is unavailable or inoperable, repair or replace it as soon as possible. 如果其中一个配置服务器不可用或不可操作,请尽快修复或更换。The following procedure replaces a member of a config server replica set with a new member.以下过程将配置服务器副本集的成员替换为新成员。
The tutorial is specific to MongoDB 6.0. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.本教程专门针对MongoDB 6.0。有关MongoDB的早期版本,请参阅相应版本的MongoDB手册。
Considerations注意事项
The following restrictions apply to a replica set configuration when used for config servers:以下限制适用于用于配置服务器的副本集配置:
Must have zero arbiters.必须没有仲裁器。Must have no delayed members.不得有延迟成员。Must build indexes (i.e. no member should have必须生成索引(即任何成员都不应将members[n].buildIndexes
setting set to false).members[n].buildIndexes
设置为false
)。
Procedure过程
Start the replacement config server.启动替换配置服务器。
Start a 启动一个mongod
instance, specifying the --configsvr
, --replSet
, --bind_ip
options, and other options as appropriate to your deployment.mongod
实例,根据您的部署指定--configsvr
、--replSet
、--bind_ip
选项和其他选项。
Before you bind your instance to a publicly-accessible IP address, you must secure your cluster from unauthorized access. 在将实例绑定到可公开访问的IP地址之前,必须保护群集不受未经授权的访问。For a complete list of security recommendations, see Security Checklist. 有关安全建议的完整列表,请参阅安全检查表。At minimum, consider enabling authentication and hardening network infrastructure.至少,考虑启用身份验证和强化网络基础设施。
mongod --configsvr --replSet <replicaSetName> --bind_ip localhost,<hostname(s)|ip address(es)>
Add the new config server to the replica set.将新的配置服务器添加到副本集中。
Connect 将mongosh
to the primary of the config server replica set and use rs.add()
to add the new member.mongosh
连接到配置服务器副本集的主副本,并使用rs.add()
添加新成员。
Before MongoDB 5.0, a newly added secondary still counts as a voting member even though it can neither serve reads nor become primary until its data is consistent. 在MongoDB 5.0之前,新添加的次要成员仍然算作投票成员,即使在数据一致之前,它既不能提供读取服务,也不能成为主要成员。If you are running a MongoDB version earlier than 5.0 and add a secondary with its 如果您运行的MongoDB版本早于5.0,并且添加了一个votes
and priority
settings greater than zero, this can lead to a case where a majority of the voting members are online but no primary can be elected. votes
和priority
设置大于零的次要成员,这可能会导致大多数投票成员都在线,但无法选出主要成员。To avoid such situations, consider adding the new secondary initially with 为了避免出现这种情况,请考虑添加新的次级,初始priority :0
and votes :0
. priority :0
,votes :0
。Then, run 然后,运行rs.status()
to ensure the member has transitioned into SECONDARY
state. rs.status()
以确保成员已转换到SECONDARY
状态。Finally, use 最后,使用rs.reconfig()
to update its priority and votes.rs.reconfig()
更新其优先级和投票。
rs.add( { host: "<hostnameNew>:<portNew>", priority: 0, votes: 0 } )
The initial sync process copies all the data from one member of the config server replica set to the new member without restarting.初始同步过程将配置服务器副本集的一个成员的所有数据复制到新成员,而不重新启动。
mongos
instances automatically recognize the change in the config server replica set members without restarting.mongos
实例在不重新启动的情况下自动识别配置服务器副本集成员中的更改。
Update the newly added config server's votes and priority settings.更新新添加的配置服务器的投票和优先级设置。
Ensure that the new member has reached请确保新成员已达到SECONDARY
state.SECONDARY
状态。To check the state of the replica set members, run要检查副本集成员的状态,请运行rs.status()
:rs.status()
:rs.status()
Reconfigure the replica set to update the votes and priority of the new member:重新配置副本集以更新新成员的投票和优先级:var cfg = rs.conf();
cfg.members[n].priority = 1; // Substitute the correct array index for the new member
cfg.members[n].votes = 1; // Substitute the correct array index for the new member
rs.reconfig(cfg)where其中n
is the array index of the new member in themembers
array.n
是members
数组中新成员的数组索引。
Thers.reconfig()
shell method can force the current primary to step down, which causes an election.rs.reconfig()
shell方法可以强制当前的primary退出,从而导致选举。When the primary steps down, the当primary关闭时,mongod
closes all client connections.mongod
会关闭所有客户端连接。While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.虽然这通常需要10-20秒,但请尝试在计划维护期间进行这些更改。Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同MongoDB版本成员的副本集,因为验证规则可能因MongoDB版本而异。
Remove the member to replace from the config server replica set.从配置服务器副本集中删除要替换的成员。
Upon completion of initial sync of the replacement config server, from a 在完成替换配置服务器的初始同步后,从连接到主服务器的mongosh
session that is connected to the primary, use rs.remove()
to remove the old member.mongosh
会话中,使用rs.remove()
删除旧成员。
rs.remove("<hostnameOld>:<portOld>")
mongos
instances automatically recognize the change in the config server replica set members without restarting.实例会自动识别配置服务器副本集成员中的更改,而无需重新启动。
If necessary, update mongos
configuration or DNS entry.如有必要,请更新mongos
配置或DNS条目。
mongos
configuration or DNS entry.With replica set config servers, the 对于副本集配置服务器,mongos
instances specify in the --configdb
or sharding.configDB
setting the config server replica set name and at least one of the replica set members.mongos
实例在--configdb
或sharding.configDB
设置中指定配置服务器副本集名称和至少一个副本集成员。
As such, if the 因此,如果mongos
instance does not specify the removed replica set member in the --configdb
or sharding.configDB
setting, no further action is necessary.mongos
实例没有在--configdb
或sharding.configDB
设置中指定已删除的副本集成员,则无需执行进一步的操作。
If, however, a 但是,如果mongos
instance specified the removed member in the --configdb
or configDB
setting, either:mongos
实例在--configdb
或configDB
设置中指定了已删除的成员,则:
Update the setting for the next time you restart the下次重新启动mongos
, ormongos
时更新设置,或者Modify the DNS entry that points to the system that provided the old config server, so that the same hostname points to the new config server.修改指向提供旧配置服务器的系统的DNS条目,使相同的主机名指向新配置服务器。