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
实例可用的管理命令。
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.迁移期间针对这些集合发出的读或写操作可能会导致意外行为,包括迁移操作的潜在失败或数据丢失。
mongos
uses 对"majority"
write concern for movePrimary
.movePrimary
使用"majority"
写入关注点。
movePrimary
During A Maintenance WindowmovePrimary
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
可降低由于对数据库中未排序集合进行交错读取或写入而遇到未定义行为的风险。
movePrimary
To Move Unsharded CollectionsmovePrimary
移动未分片集合For MongoDB 4.2 and previous, if using the 对于MongoDB 4.2及以前版本,如果在包含未分片集合的数据库上使用movePrimary
command on a database that contains an unsharded collection, you must perform the following additional steps.movePrimary
命令,则必须执行以下附加步骤。
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,您必须:
mongos
instances and all mongod
shard members (including the secondary members);mongos
实例和所有mongod
分片成员(包括次要成员);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及更早版本,您必须:
mongos
instances;mongos
实例;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.要恢复,必须手动干预。
如果目标分片包含冲突的集合命名空间,movePrimary
fails if the destination shard contains a conflicting collection namespace. movePrimary
将失败。For Example:例如:
movePrimary
to change the primary shard for the hr
database.movePrimary
以更改hr
数据库的主分片。hr
while movePrimary
is moving that collection. movePrimary
移动未记录集合时,用户或应用程序在hr
内对该集合发出写入操作。movePrimary
to restore the original primary shard for the hr
database.movePrimary
以恢复hr
数据库的原始主分片。movePrimary
fails due to the conflicting namespace left behind from the interleaving write operation.movePrimary
由于交织写入操作留下的冲突命名空间而失败。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.有关索引生成过程的详细信息,请参阅填充集合上的索引生成。
See Remove Shards from an Existing Sharded Cluster for a complete procedure.有关完整过程,请参阅从现有分片集群中移除分片。