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

On this page本页内容

The tutorial is specific to MongoDB 5.0. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.本教程特定于MongoDB 5.0。有关MongoDB的早期版本,请参阅相应版本的MongoDB手册。

Starting in MongoDB 3.2, config servers for sharded clusters can be deployed as a replica set. 从MongoDB 3.2开始,可以将分片集群的配置服务器部署为副本集The replica set config servers must run the WiredTiger storage engine. 副本集配置服务器必须运行WiredTiger存储引擎MongoDB 3.2 deprecates the use of three mirrored mongod instances for config servers.MongoDB 3.2不推荐将三个镜像mongod实例用于配置服务器。

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 4.2, sh.stopBalancer() also disables auto-splitting for the sharded cluster.从MongoDB 4.2开始,sh.stopBalancer()还禁用了分片集群的自动拆分。

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

Changed in version 3.4.在版本3.4中更改

Starting in MongoDB 3.2, config servers for sharded clusters can be deployed as a replica set (CSRS) instead of three mirrored config servers (SCCC). 从MongoDB 3.2开始,可以将分片集群的配置服务器部署为一个副本集(CSRS),而不是三个镜像配置服务器(SCCC)。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存储引擎

In version 3.4, MongoDB removes support for SCCC config servers.在3.4版中,MongoDB删除了对SCCC配置服务器的支持

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 binding to a non-localhost (e.g. publicly accessible) IP address, ensure you have secured 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.其中n是members数组中新成员的数组索引。

Warning警告
  • The rs.reconfig() shell method can force the current primary to step down, which causes an election. rs.reconfig()shell方法可以强制当前主节点下移,从而导致选举When the primary steps down, the 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的版本而异。
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实例

Changed in version 3.2.在版本3.2中更改

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.首先迁移非主要成员,然后最后迁移主要成员

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.status()

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传递给shstartBalancer()方法:[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 4.2, sh.startBalancer() also enables auto-splitting for the sharded cluster.从MongoDB 4.2开始,sh.startBalancer()还支持自动分割分片集群。
←  Restart a Sharded ClusterAdd Shards to a Cluster →