对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

movePrimary(数据库命令)

movePrimary

In a sharded cluster, movePrimary reassigns the primary shard which holds all un-sharded collections in the database. 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> } )

例如,以下命令将主分片从test移动到shard0001

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

当命令返回时,数据库的主分片位置已切换到指定的分片。 要完全停用分片,请使用removeShard命令。

movePrimary is an administrative command that is only available for mongos instances.

警告

After starting movePrimary, do not perform any read or write operations against any unsharded collection in that database until the command completes. 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.

此命令可用于以下环境中托管的部署:

  • MongoDB Atlas:用于云中 MongoDB 部署的完全托管服务

mongos uses "majority" write concern for movePrimary.

仅在使用movePrimary 移动未分片的集合后才运行sh.moveCollection() 。从MongoDB.8 0开始,未移动、未分片的集合在movePrimary 进程中不可用。

If the database being moved contains unsharded collections that use Atlas Search, their search indexes become unavailable after movePrimary completes. Because movePrimary updates the UUID for unsharded collections that you move, you must manually rebuild the Atlas Search indexes on those collections.

movePrimary 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: "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 如果目标分片包含有冲突的collection命名空间,则会失败。例如:

  1. 管理员发出movePrimary来更改hr数据库的主分片。

  2. movePrimary移动hr中的未分片集合时,用户或应用程序对该集合发出写操作。 写入操作会在原始主分片中创建collection。

  3. 管理员稍后发出movePrimary来恢复hr数据库的原始主分片。

  4. movePrimary 由于交错写入操作留下的命名空间冲突而失败。

作为movePrimary操作的一部分,目标分片必须在成为主分片后对迁移的collection重建索引。这可能需要大量时间,具体取决于每个collection的索引数量和要索引的数据量。

有关索引构建过程的更多信息,请参阅在已填充collection上构建索引。

从MongoDB8.0 开始,movePrimary 不会使具有 变更流 事件集合 失效。在将集合移动到新的分片后,变更流可以继续从集合中读取事件。

在早期MongoDB版本中, movePrimary会使集合变更流无效,并且变更流无法从集合中读取事件。

在所有MongoDB版本中, movePrimary会更新移动的未分片集合的UUID

有关完整过程,请参阅从分片集群中删除分片

给本页内容打分