Docs HomeMongoDB Manual

Migrate a Sharded Cluster to Different Hardware将分片群集迁移到不同的硬件

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手册。

Config servers for sharded clusters are deployed as a replica set. 分片集群的配置服务器被部署为一个副本集The replica set config servers must run the WiredTiger storage engine.复制副本集配置服务器必须运行WiredTiger存储引擎

This procedure moves the components of the sharded cluster to a new hardware system without downtime for reads and writes.此过程将分片集群的组件移动到一个新的硬件系统,而不会因读写而停机。

Important

While the migration is in progress, do not attempt to change to the Sharded Cluster Metadata. 在迁移过程中,不要尝试更改为分片集群元数据Do not use any operation that modifies the cluster metadata in any way. For example, do not create or drop databases, create or drop collections, or use any sharding commands.不要使用任何以任何方式修改集群元数据的操作。例如,不要创建或删除数据库、创建或删除集合或使用任何分片命令。

Disable the Balancer禁用平衡器

Disable the balancer to stop chunk migration and do not perform any metadata write operations until the process finishes. 禁用平衡器以停止区块迁移,并且在进程完成之前不执行任何元数据写入操作。If a migration is in progress, the balancer will complete the in-progress migration before stopping.如果正在进行迁移,平衡器将在停止之前完成正在进行的迁移。

To disable the balancer, connect to one of the cluster's mongos instances and issue the following method: 要禁用平衡器,请连接到集群的一个mongos实例,并发出以下方法:[1]

sh.stopBalancer()

To check the balancer state, issue the sh.getBalancerState() method.要检查平衡器状态,请发出sh.getBalancerState()方法。

For more information, see Disable the Balancer.有关详细信息,请参阅禁用平衡器

[1] Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. 自动拆分命令仍然存在,但不执行操作。For details, see Balancing Policy Changes.In MongoDB versions earlier than 6.1, sh.stopBalancer() also disables auto-splitting for the sharded cluster.有关详细信息,请参阅平衡策略更改。在6.1之前的MongoDB版本中,sh.stopBalancer()还禁用了分片集群的自动拆分。

Migrate Each Config Server Separately分别迁移每个配置服务器

Config servers for sharded clusters can be deployed as a replica set (CSRS). 分片集群的配置服务器可以作为副本集(CSRS)进行部署。Using a replica set for the config servers improves consistency across the config servers, since MongoDB can take advantage of the standard replica set read and write protocols for the config data. 为配置服务器使用副本集可以提高配置服务器之间的一致性,因为MongoDB可以利用配置数据的标准副本集读写协议。In addition, using a replica set for config servers allows a sharded cluster to have more than 3 config servers since a replica set can have up to 50 members. 此外,为配置服务器使用副本集允许分片集群拥有3个以上的配置服务器,因为副本集最多可以有50个成员。To deploy config servers as a replica set, the config servers must run the WiredTiger Storage Engine.要将配置服务器部署为副本集,配置服务器必须运行WiredTiger存储引擎

The following restrictions apply to a replica set configuration when used for config servers:以下限制适用于用于配置服务器的副本集配置:

For each member of the config server replica set:对于配置服务器副本集的每个成员:

Important

Replace the secondary members before replacing the primary.在更换主构件之前,先更换辅助构件。

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

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

Restart the mongos Instances重新启动mongos实例

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设置中指定配置服务器副本集名称和至少一个副本集成员。The mongos instances for the sharded cluster must specify the same config server replica set name but can specify different members of the replica set.分片集群的mongos实例必须指定相同的配置服务器副本集名称,但可以指定副本集的不同成员。

If a mongos instance specifies a migrated replica set member in the --configdb or sharding.configDB setting, update the config server setting for the next time you restart the mongos instance.如果mongos实例在--configdbsharding.configDB设置中指定了迁移的副本集成员,请在下次重新启动mongos示例时更新配置服务器设置。

For more information, see Start a mongos for the Sharded Cluster.有关更多信息,请参阅为分片集群启动mongos

Migrate the Shards迁移分片

Migrate the shards one at a time. For each shard, follow the appropriate procedure in this section.一次迁移一个分片。对于每个分片,请遵循本节中的相应过程。

Migrate a Replica Set Shard迁移复制副本集分片

To migrate a sharded cluster, migrate each member separately. 要迁移分片集群,请分别迁移每个成员。First migrate the non-primary members, and then migrate the primary last.首先迁移非primary成员,然后最后迁移primary成员。

If the replica set has two voting members, add an arbiter to the replica set to ensure the set keeps a majority of its votes available during the migration. 如果副本集有两个投票成员,请向副本集添加一个仲裁器,以确保该集在迁移过程中保持大部分投票可用。You can remove the arbiter after completing the migration.您可以在完成迁移后删除仲裁器。

Migrate a Member of a Replica Set Shard迁移副本集分片的成员

  1. Shut down the mongod process. 关闭mongod进程。To ensure a clean shutdown, use the shutdown command.要确保干净关机,请使用shutdown命令。
  2. Move the data directory (i.e., the dbPath) to the new machine.将数据目录(即dbPath)移动到新机器。
  3. Restart the mongod process at the new location.在新位置重新启动mongod进程。
  4. Connect to the replica set's current primary.连接到复制副本集的当前主服务器。
  5. If the hostname of the member has changed, use rs.reconfig() to update the replica set configuration document with the new hostname.如果成员的主机名已更改,请使用rs.reconfig()用新主机名更新副本集配置文档。

    For example, the following sequence of commands updates the hostname for the instance at position 2 in the members array:例如,以下命令序列将更新members数组中位置2处实例的主机名:

    cfg = rs.conf()
    cfg.members[2].host = "pocatello.example.net:27018"
    rs.reconfig(cfg)

    For more information on updating the configuration document, see Examples.有关更新配置文档的更多信息,请参阅示例

  6. To confirm the new configuration, issue rs.conf().要确认新配置,请发出rs.conf()
  7. Wait for the member to recover. To check the member's state, issue rs.status().等待成员恢复。若要检查成员的状态,请发出rs.conf()

Migrate the Primary in a Replica Set Shard迁移副本集中的主分片

While migrating the replica set's primary, the set must elect a new primary. 迁移复制副本集的主副本集时,该集必须选择一个新的主副本。This failover process which renders the replica set unavailable to perform reads or accept writes for the duration of the election, which typically completes quickly. 此故障转移过程使复制副本集在选择期间无法执行读取或接受写入,而选择通常会很快完成。If possible, plan the migration during a maintenance window.如果可能,请在维护窗口期间计划迁移。

  1. Step down the primary to allow the normal failover process. 逐步关闭主服务器以允许正常的故障切换过程。To step down the primary, connect to the primary and issue the either the replSetStepDown command or the rs.stepDown() method. 要逐步关闭主服务器,请连接到主服务器并发出replSetStepDown命令或rs.stepDown()方法。The following example shows the rs.stepDown() method:以下示例显示了rs.stepDown()方法:

    rs.stepDown()
  2. Once the primary has stepped down and another member has become PRIMARY state. 一旦主成员退出并且另一个成员变为PRIMARY状态。To migrate the stepped-down primary, follow the Migrate a Member of a Replica Set Shard procedure要迁移逐步减少的主,请按照迁移副本集分片的成员过程进行操作

    You can check the output of rs.status() to confirm the change in status.您可以检查rs.status()的输出以确认状态的更改。

Re-Enable the Balancer重新启用平衡器

To complete the migration, re-enable the balancer to resume chunk migrations.要完成迁移,请重新启用平衡器以恢复区块迁移

Connect to one of the cluster's mongos instances and pass true to the sh.startBalancer() method: 连接到集群的一个mongos实例,并将true传递给sh.startBalancer()方法:[2]

sh.startBalancer()

To check the balancer state, issue the sh.getBalancerState() method.要检查平衡器状态,请发出sh.getBalancerState()方法。

For more information, see Enable the Balancer.有关详细信息,请参阅启用平衡器

[2] Starting in MongoDB 6.1, automatic chunk splitting is not performed. 从MongoDB 6.1开始,不执行自动区块分割。This is because of balancing policy improvements. 这是因为平衡政策的改进。Auto-splitting commands still exist, but do not perform an operation. For details, see Balancing Policy Changes.自动拆分命令仍然存在,但不执行操作。有关详细信息,请参阅平衡策略更改In MongoDB versions earlier than 6.1, sh.startBalancer() also enables auto-splitting for the sharded cluster.在6.1之前的MongoDB版本中,sh.startBalancer()还支持对分片集群进行自动拆分。