对于 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: The fully managed service for MongoDB deployments in the cloud

mongos uses "majority" write concern for movePrimary.

Only run movePrimary after you move unsharded collections using sh.moveCollection(). Starting in MongoDB 8.0, unmoved, unsharded collections are not available during the movePrimary process.

如果正在移动的数据库包含使用 MongoDB Search 的未分片集合,其搜索索引将在 movePrimary 完成后变为不可用。由于 movePrimary 会更新您移动的未分片集合的 UUID ,因此您必须手动重建这些集合上的 MongoDB Search 索引。

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 。

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

给本页内容打分