Navigation
This version of the documentation is archived and no longer supported.

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.runCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )

For example, the following command moves the primary shard from test to shard0001:

db.runCommand( { 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.

Note

The destination shard must rebuild its indexes in the foreground when it becomes the primary shard. The collection locks while the indexes are building, preventing reads and writes until the indexes are finished. See Index Build Operations for details.

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

Considerations

Warning

If you use the movePrimary command to move un-sharded collections, you must either restart all mongos instances, or use the flushRouterConfig command on all mongos instances before reading or writing any data to any unsharded collections that were moved. This action ensures that the mongos is aware of the new shard for these collections.

If you do not update the mongos instances’ metadata cache after using movePrimary, the mongos may miss data on reads, and may not write data to the correct shard. To recover, you must manually intervene.

Avoid accessing an un-sharded collection during migration. movePrimary does not prevent reading and writing during its operation, and such actions yield undefined behavior.

movePrimary may take significant time to complete, and you should plan for this unavailability.

movePrimary will fail if the destination shard contains a conflicting collection name. This may occur if documents are written to an un-sharded collection while the collection is moved away, and later the original primary shard is restored.

Additional Information

See Remove Shards from an Existing Sharded Cluster for a complete procedure.

←   moveChunk isdbgrid  →