/
movePrimary.txt
68 lines (48 loc) · 2.17 KB
/
movePrimary.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
===========
movePrimary
===========
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
.. dbcommand:: movePrimary
In a :term:`sharded cluster`, :dbcommand:`movePrimary` reassigns the
:term:`primary shard` which holds all un-sharded collections in
the database. :dbcommand:`movePrimary` first changes the primary shard in
the cluster metadata, and then migrates all un-sharded collections to the
specified :term:`shard`. Use the command with the following form:
.. code-block:: javascript
db.runCommand( { movePrimary: <databaseName>, to: <newPrimaryShard> } )
For example, the following command moves the primary shard from ``test`` to
``shard0001``:
.. code-block:: javascript
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 :dbcommand:`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 :doc:`/core/index-creation` for details.
:dbcommand:`movePrimary` is an administrative command that is only available
for :binary:`~bin.mongos` instances.
Considerations
--------------
.. include:: /includes/warning-movePrimary.rst
Avoid accessing an un-sharded collection during migration.
:dbcommand:`movePrimary` does not prevent reading and writing during its
operation, and such actions yield undefined behavior.
:dbcommand:`movePrimary` may take significant time to complete, and
you should plan for this unavailability.
:dbcommand:`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 :doc:`/tutorial/remove-shards-from-cluster` for a complete
procedure.