movePrimary

On this page本页内容

movePrimary

In a sharded cluster, movePrimary reassigns the primary shard which holds all un-sharded collections in the database. 分片集群中,movePrimary重新分配主分片,该分片包含数据库中所有未分片的集合。movePrimary first changes the primary shard in the cluster metadata, and then migrates all un-sharded collections to the specified shard. 首先更改集群元数据中的主分片,然后将所有未分片的集合迁移到指定的分片Use the command with the following form:使用以下格式的命令:

db.adminCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )

For example, the following command moves the primary shard from test to shard0001:例如,以下命令将主分片从test移动到shard0001

db.adminCommand( { movePrimary : "test", to : "shard0001" } )

When the command returns, the database's primary shard location has switched to the specified shard. 当命令返回时,数据库的主要分片位置已切换到指定的分片。To fully decommission a shard, use the removeShard command.要完全停用分片,请使用removeShard命令。

movePrimary is an administrative command that is only available for mongos instances.是一个仅对mongos实例可用的管理命令。

Warning警告

After starting movePrimary, do not perform any read or write operations against any unsharded collection in that database until the command completes. 启动movePrimary后,在命令完成之前,不要对该数据库中的任何未排序集合执行任何读或写操作。Read or write operations issued against those collections during the migration can result in unexpected behavior, including potential failure of the migration operation or loss of data.迁移期间针对这些集合发出的读或写操作可能会导致意外行为,包括迁移操作的潜在失败或数据丢失。

Considerations注意事项

mongos uses "majority" write concern for movePrimary.movePrimary使用"majority"写入关注点。

Issue movePrimary During A Maintenance Window维护窗口期间的发起movePrimary

movePrimary may require a significant time to complete depending on the size of the database and factors such as network health or machine resources. 这取决于数据库的大小和诸如网络健康或机器资源的因素。Read or write operations issued against the database during the migration can result in unexpected behavior, including potential failure of the migration operation or loss of data.迁移期间对数据库发出的读或写操作可能会导致意外行为,包括迁移操作的潜在失败或数据丢失。

Consider scheduling a maintenance window during which applications stop all reads and writes to the cluster. 考虑安排一个维护窗口,在此期间应用程序停止对集群的所有读取和写入。Issuing movePrimary during planned downtime mitigates the risk of encountering undefined behavior due to interleaving reads or writes to the unsharded collections in the database.在计划内停机期间发出movePrimary可降低由于对数据库中未排序集合进行交错读取或写入而遇到未定义行为的风险。

Using movePrimary To Move Unsharded Collections使用movePrimary移动未分片集合

For MongoDB 4.2 and previous, if using the movePrimary command on a database that contains an unsharded collection, you must perform the following additional steps.对于MongoDB 4.2及以前版本,如果在包含未分片集合的数据库上使用movePrimary命令,则必须执行以下附加步骤。

Note注意

MongoDB 4.4 does not require these additional steps when moving databases that contain unsharded collections.当移动包含未分片集合的数据库时,MongoDB 4.4不需要这些额外步骤。

  • For MongoDB 4.2, you must either:对于MongoDB 4.2,您必须:

    • Restart all mongos instances and all mongod shard members (including the secondary members);重新启动所有mongos实例和所有mongod分片成员(包括次要成员);
    • Use the flushRouterConfig command on all mongos instances and all mongod shard members (including the secondary members) before reading or writing any data to any unsharded collections that were moved.在所有mongos实例和所有mongod分片成员(包括次要成员)上使用flushRouterConfig命令,然后再将任何数据读取或写入移动的任何未排序集合。
  • For MongoDB 4.0 and earlier, you must either:对于MongoDB 4.0及更早版本,您必须:

    • Restart all mongos instances;重新启动所有mongos实例;
    • Use the flushRouterConfig command on all mongos instances before reading or writing any data to any unsharded collections that were moved.在所有mongos实例上使用flushRouterConfig命令,然后再将任何数据读取或写入任何已移动的未记录集合。

These steps ensure that all cluster nodes refresh their metadata cache, which includes the location of the primary shard. 这些步骤确保所有集群节点刷新其元数据缓存,其中包括主分片的位置。Otherwise, you may miss data on reads, and may not write data to the correct shard. 否则,您可能会在读取时丢失数据,并且可能无法将数据写入正确的分片。To recover, you must manually intervene.要恢复,必须手动干预。

Namespace Conflicts Prevent Migration命名空间冲突阻止迁移

movePrimary fails if the destination shard contains a conflicting collection namespace. 如果目标分片包含冲突的集合命名空间,movePrimary将失败。For Example:例如:

  1. An administrator issues movePrimary to change the primary shard for the hr database.管理员发出movePrimary以更改hr数据库的主分片。
  2. A user or application issues a write operation against an unsharded collection in hr while movePrimary is moving that collection. movePrimary移动未记录集合时,用户或应用程序在hr内对该集合发出写入操作。The write operation creates the collection in the original primary shard.写操作在原始主分片中创建集合。
  3. An administrator later issues movePrimary to restore the original primary shard for the hr database.管理员稍后发出movePrimary以恢复hr数据库的原始主分片。
  4. movePrimary fails due to the conflicting namespace left behind from the interleaving write operation.movePrimary由于交织写入操作留下的冲突命名空间而失败。

Destination Shard Must Rebuild Indexes目标分片必须重建索引

As part of the movePrimary operation, the destination shard must rebuild indexes on the migrated collections after becoming the primary shard. 作为movePrimary操作的一部分,目标分片必须在成为主分片后重建迁移集合的索引。This may require a significant amount of time depending on the number of indexes per collection and the amount of data to index.这可能需要大量时间,具体取决于每个集合的索引数量和要索引的数据量。

See Index Builds on Populated Collections for more information on the index build process.有关索引生成过程的详细信息,请参阅填充集合上的索引生成

Additional Information附加信息

See Remove Shards from an Existing Sharded Cluster for a complete procedure.有关完整过程,请参阅从现有分片集群中移除分片

←  moveChunkmergeChunks →