-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
remove-shards-from-cluster.txt
218 lines (156 loc) · 6.64 KB
/
remove-shards-from-cluster.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
==============================================
Remove Shards from an Existing Sharded Cluster
==============================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
To remove a :term:`shard` you must ensure the shard's data is migrated
to the remaining shards in the cluster. This procedure describes how to
safely migrate data and how to remove a shard.
.. include:: /includes/fact-remove-shard-balance-order.rst
This procedure describes how to safely remove a *single* shard. *Do not*
use this procedure to migrate an entire cluster to new hardware. To
migrate an entire shard to new hardware, migrate individual shards as if
they were independent replica sets.
.. DOCS-94 will lead to a tutorial about cluster migrations. In the
mean time the above section will necessarily lack links.
To remove a shard, first connect to one of the cluster's
:binary:`~bin.mongos` instances using :binary:`~bin.mongo` shell. Then use the
sequence of tasks in this document to remove a shard from the cluster.
.. _remove-shard-ensure-balancer-is-enabled:
.. _remove-shard-ensure-balancer-is-active:
Ensure the Balancer Process is Enabled
--------------------------------------
To successfully migrate data from a shard, the :term:`balancer` process
**must** be enabled. Check the balancer state using the
:method:`sh.getBalancerState()` helper in the :binary:`~bin.mongo` shell.
For more information, see the section on :ref:`balancer operations
<sharding-balancing-disable-temporarily>`.
.. _remove-shard-determine-name-shard:
Determine the Name of the Shard to Remove
-----------------------------------------
To determine the name of the shard, connect to a :binary:`~bin.mongos`
instance with the :binary:`~bin.mongo` shell and either:
- Use the :dbcommand:`listShards` command, as in the following:
.. code-block:: javascript
db.adminCommand( { listShards: 1 } )
- Run either the :method:`sh.status()` or the
:method:`db.printShardingStatus()` method.
The ``shards._id`` field lists the name of each shard.
.. _remove-shard-remove-chunks:
Remove Chunks from the Shard
----------------------------
From the ``admin`` database, run the :dbcommand:`removeShard` command.
This begins "draining" chunks
from the shard you are removing to other shards in the cluster. For
example, for a shard named ``mongodb0``, run:
.. code-block:: javascript
use admin
db.runCommand( { removeShard: "mongodb0" } )
This operation returns immediately, with the following response:
.. code-block:: javascript
{
"msg" : "draining started successfully",
"state" : "started",
"shard" : "mongodb0",
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fiz",
"buzz"
],
"ok" : 1
}
The balancer begins migrating chunks from the shard named ``bristol01``
to other shards in the cluster. These migrations happens slowly to
avoid placing undue load on the overall cluster.
Depending on your network capacity and the amount of data, this
operation can take from a few minutes to several days to complete.
.. note::
Each database in a sharded cluster has a primary shard. If the shard you
want to remove is also the primary of one of the cluster's databases,
:dbcommand:`removeShard` lists the database in the ``dbsToMove`` field.
To finish removing the shard, you must either move the database
to a new shard after migrating all data from the shard or drop the
database, deleting the associated data files.
.. _remove-shard-check-migration-status:
Check the Status of the Migration
---------------------------------
To check the progress of the migration at any stage in the process, run
:dbcommand:`removeShard` from the ``admin`` database again. For example,
for a shard named ``mongodb0``, run:
.. code-block:: javascript
use admin
db.runCommand( { removeShard: "mongodb0" } )
The command returns output similar to the following:
.. code-block:: javascript
{
"msg" : "draining ongoing",
"state" : "ongoing",
"remaining" : {
"chunks" : NumberLong(2),
"dbs" : NumberLong(2)
},
"note" : "you need to drop or movePrimary these databases",
"dbsToMove" : [
"fizz",
"buzz"
],
"ok" : 1
}
In the output, the ``remaining`` document displays the remaining number
of chunks that MongoDB must migrate to other shards and the number of
MongoDB databases that have "primary" status on this shard.
Continue checking the status of the `removeShard` command until the
number of chunks remaining is ``0``. Always run the command on the
``admin`` database. If you are on a database other than ``admin``, you can
use :method:`sh._adminCommand` to run the command on ``admin``.
.. _remove-shard-move-unsharded-databases:
Move Databases to Another Primary Shard
---------------------------------------
If the shard is the :term:`primary shard` for one or more databases in
the cluster, then you must make that database use a different shard
as its primary shard. :dbcommand:`removeShard` lists any databases
that you need to move in the ``dbsToMove`` field in the command output.
If the shard is
not the primary shard for any databases, skip to the next task,
:ref:`remove-shard-finalize-migration`.
.. warning::
Do not perform this procedure until you have finished draining the
shard.
To move a database to another shard, use the :dbcommand:`movePrimary`
command.
.. important::
To ensure a smooth migration, refer to the considerations in the
:dbcommand:`movePrimary` command documentation before running
:dbcommand:`movePrimary`.
To migrate the ``fizz`` database from
``mongodb0`` to ``mongodb1``, issue the following command:
.. code-block:: javascript
db.runCommand( { movePrimary: "fizz", to: "mongodb1" })
This command does not return until MongoDB completes moving all data.
The response from this command will
resemble the following:
.. code-block:: javascript
{ "primary" : "mongodb1", "ok" : 1 }
.. _remove-shard-finalize-migration:
Finalize the Migration
----------------------
To clean up all metadata information and finalize the removal, run
:dbcommand:`removeShard` again. For example, for a shard named
``mongodb0``, run:
.. code-block:: javascript
use admin
db.runCommand( { removeShard: "mongodb0" } )
A success message appears at completion:
.. code-block:: javascript
{
"msg" : "removeshard completed successfully",
"state" : "completed",
"shard" : "mongodb0",
"ok" : 1
}
Once the value of the ``state`` field is "completed", you may safely
stop the instances comprising the ``mongodb0`` shard.