The tutorial is specific to MongoDB 8.0. For earlier versions of MongoDB, refer to the corresponding version of the MongoDB Manual.本教程专门针对MongoDB 8.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.不要使用任何以任何方式修改群集元数据的操作。例如,不要创建或删除数据库,创建或删除集合,或使用任何分片命令。
Before You Begin开始之前
Starting in MongoDB 8.0, you can use the 从MongoDB 8.0开始,您可以使用directShardOperations role to perform maintenance operations that require you to execute commands directly against a shard.directShardOperations角色执行维护操作,这些操作要求您直接对分片执行命令。
Warning
Running commands using the 使用directShardOperations role can cause your cluster to stop working correctly and may cause data corruption. directShardOperations角色运行命令可能会导致集群停止正常工作,并可能导致数据损坏。Only use the 仅将directShardOperations role for maintenance purposes or under the guidance of MongoDB support. directShardOperations角色用于维护目的或在MongoDB支持的指导下使用。Once you are done performing maintenance operations, stop using the 完成维护操作后,停止使用directShardOperations role.directShardOperations角色。
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] | sh.stopBalancer() also disables auto-splitting for the sharded cluster.sh.stopBalancer()还禁用了分片集群的自动拆分。 |
Migrate Each Config Server Separately分别迁移每个配置服务器
Config servers for sharded clusters can be deployed as a replica set (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. 分片集群的配置服务器可以部署为副本集(CSRS)。为配置服务器使用副本集可以提高配置服务器之间的一致性,因为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:当用于配置服务器时,以下限制适用于副本集配置:
Must have zero arbiters.必须没有仲裁者。Must have no delayed members.必须没有延迟成员。Must build indexes (i.e. no member should have必须构建索引(即,任何成员都不应将members[n].buildIndexessetting set to false).members[n].buildIndexes设置为false)。
For each member of the config server replica set:对于配置服务器副本集的每个成员:
Important
Replace the secondary members before replacing the primary.在更换主构件之前,先更换次构件。
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 for Self-Managed Deployments. 有关安全建议的完整列表,请参阅自我管理部署的安全检查表。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()添加新成员。
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 如果你运行的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.实例会自动识别配置服务器副本集成员中的更改,而无需重新启动。
Update the newly added config server's votes and priority settings.更新新添加的配置服务器的投票和优先级设置。
Ensure that the new member has reached确保新成员已达到SECONDARYstate. To check the state of the replica set members, runrs.status():SECONDARY状态。要检查副本集成员的状态,请运行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其中nis the array index of the new member in themembersarray.n是members数组中新成员的数组索引。
Warning警告
Thers.reconfig()shell method can force the current primary to step down, which causes an election.rs.reconfig()shell方法可以强制当前初选下台,从而导致选举。When the primary steps down, the当主服务器关闭时,mongodcloses all client connections. While this typically takes 10-20 seconds, try to make these changes during scheduled maintenance periods.mongod会关闭所有客户端连接。虽然这通常需要10-20秒,但请尝试在计划维护期间进行这些更改。Avoid reconfiguring replica sets that contain members of different MongoDB versions as validation rules may differ across MongoDB versions.避免重新配置包含不同MongoDB版本成员的副本集,因为不同MongoDB版本的验证规则可能不同。
Shut down the member to replace.关闭要替换的成员。
If replacing the primary member, step down the primary first before shutting down.如果更换主构件,请在关闭前先关闭主构件。
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实例
mongos InstancesWith 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设置中指定配置服务器副本集名称和至少一个副本集成员。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实例在--configdb或sharding.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。
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迁移副本集分片的成员
Shut down the关闭mongodprocess. To ensure a clean shutdown, use theshutdowncommand.mongod进程。为确保干净关机,请使用shutdown命令。Move the data directory (i.e., the将数据目录(即dbPath) to the new machine.dbPath)移动到新机器。Restart the在新位置重新启动mongodprocess at the new location.mongod进程。Connect to the replica set's current primary.连接到副本集的当前主副本。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例如,以下命令序列更新2in themembersarray: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.有关更新配置文档的更多信息,请参阅示例。To confirm the new configuration, issue要确认新配置,请发出rs.conf().rs.conf()。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.在迁移副本集的主副本时,该副本集必须选择一个新的主副本。此故障转移过程使副本集在选举期间无法执行读取或接受写入,选举通常会快速完成。如果可能,请在维护窗口期间计划迁移。
Step down the primary to allow the normal failover process.关闭主服务器以允许正常的故障转移过程。To step down the primary, connect to the primary and issue the either the要关闭主服务器,请连接到主服务器并发出replSetStepDowncommand or thers.stepDown()method.replSetStepDown命令或rs.stepDown()方法。The following example shows the以下示例显示了rs.stepDown()method:rs.stepDown()方法:rs.stepDown()Once the primary has stepped down and another member has become一旦主要成员退出,另一个成员变为PRIMARYstate. To migrate the stepped-down primary, follow the Migrate a Member of a Replica Set Shard procedurePRIMARY状态。要迁移降级的主服务器,请按照迁移副本集分片的成员过程进行操作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] | sh.startBalancer() also enables auto-splitting for the sharded cluster.sh.startBalancer()还启用了分片集群的自动拆分。 |