Docs HomeMongoDB Manual

Replace a Config Server更换配置服务器

Important

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:以下限制适用于用于配置服务器的副本集配置:

Procedure过程

1

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选项和其他选项。

Warning

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)>
2

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()添加新成员。

Warning

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 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. 如果您运行的MongoDB版本早于5.0,并且添加了一个votespriority设置大于零的次要成员,这可能会导致大多数投票成员都在线,但无法选出主要成员。To avoid such situations, consider adding the new secondary initially with priority :0 and votes :0. 为了避免出现这种情况,请考虑添加新的次级,初始priority :0votes :0Then, 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实例在不重新启动的情况下自动识别配置服务器副本集成员中的更改。

3

Update the newly added config server's votes and priority settings.更新新添加的配置服务器的投票和优先级设置。

  1. 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()
  2. 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 the members array.其中nmembers数组中新成员的数组索引。

Warning
  • The rs.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 mongod closes all client connections. 当primary关闭时,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版本而异。
4

Shut down the member to replace.关闭要更换的成员。

If replacing the primary member, step down the primary first before shutting down.如果更换主成员,请先关闭主成员,然后再关闭。

5

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.实例会自动识别配置服务器副本集成员中的更改,而无需重新启动。

6

If necessary, update mongos configuration or DNS entry.如有必要,请更新mongos配置或DNS条目。

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实例在--configdbsharding.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实例没有在--configdbsharding.configDB设置中指定已删除的副本集成员,则无需执行进一步的操作。

If, however, a mongos instance specified the removed member in the --configdb or configDB setting, either:但是,如果mongos实例在--configdbconfigDB设置中指定了已删除的成员,则:

  • Update the setting for the next time you restart the mongos, or下次重新启动mongos时更新设置,或者
  • 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条目,使相同的主机名指向新配置服务器。