movePrimaryIn a sharded cluster,在分片集群中,movePrimaryreassigns the primary shard which holds all un-sharded collections in the database.movePrimary会重新分配主分片,该主分片包含数据库中所有未分片的集合。movePrimaryfirst 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:movePrimary首先更改集群元数据中的主分片,然后将所有未分片的集合迁移到指定的分片。使用以下格式的命令:db.adminCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )For example, the following command moves the primary shard from例如,以下命令将主分片从testtoshard0001: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当命令返回时,数据库的主分片位置已切换到指定的分片。要完全停用分片,请使用removeShardcommand.removeShard命令。movePrimaryis an administrative command that is only available for是一个仅适用于mongosinstances.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.在迁移过程中对这些集合发出的读取或写入操作可能会导致意外行为,包括迁移操作的潜在失败或数据丢失。
Compatibility兼容性
This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:
- MongoDB Atlas
: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务
- MongoDB Enterprise
: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本 - MongoDB Community
: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本
Considerations注意事项
mongos uses 对"majority" write concern for movePrimary.movePrimary使用"majority"写关注。
Issue movePrimary During A Maintenance Window问题movePrimary在维护窗口期间
movePrimary During A Maintenance WindowmovePrimary may require a significant time to complete depending on the size of the database and factors such as network health or machine resources. 根据数据库的大小和网络健康或机器资源等因素,可能需要大量时间才能完成。During migration, attempts to write or perform any DDL operations to the unsharded collections on the database being moved fail with the error: 在迁移过程中,尝试向正在移动的数据库上的未记录集合写入或执行任何DDL操作失败,并出现错误:"movePrimary is in progress"."movePrimary is in progress"。
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可以降低由于对数据库中未记录的集合进行交错读取或写入而遇到未定义行为的风险。
Namespace Conflicts Prevent Migration命名空间冲突阻止迁移
movePrimary fails if the destination shard contains a conflicting collection namespace. For example:如果目标分片包含冲突的集合命名空间,则失败。例如:
An administrator issues管理员发出movePrimaryto change the primary shard for thehrdatabase.movePrimary命令以更改hr数据库的主分片。A user or application issues a write operation against an unsharded collection in当hrwhilemovePrimaryis moving that collection. The write operation creates the collection in the original primary shard.movePrimary正在移动未记录的集合时,用户或应用程序在hr中对该集合发出写入操作。写操作在原始主分片中创建集合。An administrator later issues管理员随后发出movePrimaryto restore the original primary shard for thehrdatabase.movePrimary命令,为hr数据库还原原始主分片。movePrimaryfails due to the conflicting namespace left behind from the interleaving write operation.由于交织写入操作留下的命名空间冲突而失败。
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. This may require a significant amount of time depending on the number of indexes per collection and the amount of data to index.movePrimary操作的一部分,目标分片在成为主分片后必须在迁移的集合上重建索引。这可能需要大量时间,具体取决于每个集合的索引数量和要索引的数据量。
See Index Builds on Populated Collections for more information on the index build process.有关索引构建过程的更多信息,请参阅基于填充集合的索引构建。
Moving Collections that have Change Streams移动具有变更流的集合
Starting in MongoDB 8.0, 从MongoDB 8.0开始,movePrimary doesn't invalidate Event collections that have change streams. The change streams can continue to read events from collections after the collections are moved to a new shard.movePrimary不会使具有更改流的无效事件集合。在集合被移动到新的分片后,更改流可以继续从集合中读取事件。
In earlier MongoDB versions, 在早期的MongoDB版本中,movePrimary invalidates collection change streams and the change streams cannot read events from the collections.movePrimary会使集合更改流无效,并且更改流无法从集合中读取事件。
In all MongoDB versions, 在所有MongoDB版本中,movePrimary updates the UUID for moved unsharded collections.movePrimary都会更新移动的未记录集合的UUID。
Additional Information附加信息
See Remove Shards from a Sharded Cluster for a complete procedure.有关完整程序,请参阅从分片群集中删除分片。