Docs HomeMongoDB Manual

Convert Sharded Cluster to Replica Set将分片群集转换为副本集

This tutorial describes the process for converting a sharded cluster to a non-sharded replica set. 本教程介绍了将分片集群转换为非分片副本集的过程。To convert a replica set into a sharded cluster Convert a Replica Set to a Sharded Cluster. 若要将副本集转换为分片群集,请参阅将副本集转化为分片群集See the Sharding documentation for more information on sharded clusters.有关分片集群的更多信息,请参阅分片文档。

Convert a Cluster with a Single Shard into a Replica Set将具有单个分片的群集转换为副本集

In the case of a sharded cluster with only one shard, that shard contains the full data set. Use the following procedure to convert that cluster into a non-sharded replica set:在只有一个分片的分片集群的情况下,该分片包含完整的数据集。使用以下过程将该集群转换为非分片副本集

  1. Reconfigure the application to connect to the primary member of the replica set hosting the single shard that system will be the new replica set.重新配置应用程序以连接到承载单个分片的副本集的主要成员,该系统将成为新的副本集。
  2. Remove the --shardsvr option from your mongod.mongod中删除--shardsvr选项。

    Tip

    Changing the --shardsvr option will change the port that mongod listens for incoming connections on.更改--shardsvr选项将更改mongod侦听传入连接的端口。

The single-shard cluster is now a non-sharded replica set that will accept read and write operations on the data set.单分片集群现在是一个非分片复制集,它将接受对数据集的读写操作。

You may now decommission the remaining sharding infrastructure.您现在可以停用剩余的分片基础设施。

Convert a Sharded Cluster into a Replica Set将分片群集转换为副本集

Use the following procedure to transition from a sharded cluster with more than one shard to an entirely new replica set.使用以下过程可以从具有多个分片的分片集群过渡到全新的副本集。

  1. With the sharded cluster running, deploy a new replica set in addition to your sharded cluster. 分片集群运行的情况下,除了您的分片集群之外,还部署一个新的副本集The replica set must have sufficient capacity to hold all of the data files from all of the current shards combined. 副本集必须有足够的容量来容纳来自所有当前分片的所有数据文件。Do not configure the application to connect to the new replica set until the data transfer is complete.在数据传输完成之前,不要将应用程序配置为连接到新的复制副本集。
  2. Stop all writes to the sharded cluster. 停止对分片集群的所有写入操作。You may reconfigure your application or stop all mongos instances. 您可以重新配置应用程序或停止所有mongos实例。If you stop all mongos instances, the applications will not be able to read from the database. 如果停止所有mongos实例,应用程序将无法从数据库中读取。If you stop all mongos instances, start a temporary mongos instance on that applications cannot access for the data migration procedure.如果停止所有mongos实例,请启动应用程序无法访问的mongos临时实例以进行数据迁移过程。
  3. Use mongodump and mongorestore to migrate the data from the mongos instance to the new replica set.使用mongodumpmongorestore将数据从mongos实例迁移到新的副本集。

    Note

    Not all collections on all databases are necessarily sharded. Do not solely migrate the sharded collections. 并非所有数据库上的所有集合都必须进行分片。不要只迁移分片集合。Ensure that all databases and all collections migrate correctly.确保所有数据库和所有集合都正确迁移。

  4. Reconfigure the application to use the non-sharded replica set instead of the mongos instance.重新配置应用程序以使用非分片副本集,而不是mongos实例。

    After you convert the sharded cluster to a replica set, update the connection string used by your applications to the connection string for your replica set. Then, restart your applications.将分片集群转换为副本集后,将应用程序使用的连接字符串更新为副本集的连接字符串。然后,重新启动应用程序。

The application will now use the un-sharded replica set for reads and writes. 应用程序现在将使用未分片的副本集进行读取和写入。You may now decommission the remaining unused sharded cluster infrastructure.您现在可以解除剩余未使用的分片集群基础设施的使用。