Skip to content

Commit 41b5e1b

Browse files
committed
DOCS-14619 clarify dropDatabases sharded cluster procedure
1 parent 4d37d43 commit 41b5e1b

File tree

2 files changed

+71
-35
lines changed

2 files changed

+71
-35
lines changed
Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,70 @@
1-
.. warning::
2-
3-
Starting in MongoDB 4.2:
4-
If you drop a database and create a new database with the same name, you
5-
must either:
6-
7-
- Restart all :binary:`~bin.mongos` instances and all
8-
:binary:`~bin.mongod` shard members (including the secondary
9-
members);
10-
11-
- Use the :dbcommand:`flushRouterConfig` command on all
12-
:binary:`~bin.mongos` instances and all :binary:`~bin.mongod`
13-
shard members (including the secondary members) before reading
14-
or writing to that database.
15-
16-
This ensures that the :binary:`~bin.mongos` and shard instances
17-
refresh their metadata cache, including the location of the
18-
:ref:`primary shard <primary-shard>` for the new database.
19-
20-
Otherwise, the you may miss data on reads, and may not write data to
21-
the correct shard. To recover, you must manually intervene.
22-
23-
In MongoDB 4.0 and earlier:
24-
If you drop a database and create a new database with the same name, you
25-
must either restart all :binary:`~bin.mongos` instances, or use the
26-
:dbcommand:`flushRouterConfig` command on all :binary:`~bin.mongos`
27-
instances before reading or writing to that database. This
28-
ensures that the :binary:`~bin.mongos` instances refresh their
29-
metadata cache, including the location of the :ref:`primary shard
30-
<primary-shard>` for the new database.
31-
32-
Otherwise, the you may miss data on reads, and may not write data to
33-
the correct shard. To recover, you must manually intervene.
34-
1+
If you intend to create a new database with the same name as the dropped
2+
database, you must follow these additional steps for using the
3+
:dbcommand:`dropDatabase` command, specific to your version of MongoDB:
4+
5+
- For **MongoDB 4.2**, you must:
6+
7+
#. Run the :dbcommand:`dropDatabase` command on a
8+
:binary:`~bin.mongos`.
9+
10+
#. Once the command successfully completes, run the
11+
:dbcommand:`dropDatabase` command once more on a
12+
:binary:`~bin.mongos`.
13+
14+
#. Use the :dbcommand:`flushRouterConfig` command on **all**
15+
:binary:`~bin.mongos` instances before reading or writing to that
16+
database.
17+
18+
- For **MongoDB 4.0 and earlier**, you must:
19+
20+
#. Run the :dbcommand:`dropDatabase` command on a
21+
:binary:`~bin.mongos`.
22+
23+
#. Connect to each shard's :term:`primary` and verify that the
24+
namespace has been dropped. If it has not, rerun the
25+
:dbcommand:`dropDatabase` command again directly from the
26+
:term:`primary`.
27+
28+
#. Connect to a :binary:`~bin.mongos`, switch to the
29+
:term:`config database`, and remove any reference to the removed
30+
namespace from the ``databases``, ``collections``, ``chunks``,
31+
``tags``, and ``locks`` collections:
32+
33+
.. code-block:: javascript
34+
35+
use config
36+
db.collections.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
37+
db.databases.remove( { _id: "DATABASE" }, {writeConcern: {w: 'majority' }} )
38+
db.chunks.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
39+
db.tags.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
40+
db.locks.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} )
41+
42+
Where ``DATABASE`` represents the namespace of the database you
43+
just dropped.
44+
45+
#. Connect to the :term:`primary` of each shard, and remove any
46+
reference to the removed namespace from the ``cache.databases``,
47+
``cache.collections``, and ``cache.chunks.DATABASE.COLLECTION``
48+
collections:
49+
50+
.. code-block:: javascript
51+
52+
db.getSiblingDB("config").cache.databases.remove({_id:"DATABASE"}, {writeConcern: {w: 'majority' }});
53+
db.getSiblingDB("config").cache.collections.remove({_id:/^DATABASE.*/}, {writeConcern: {w: 'majority' }});
54+
db.getSiblingDB("config").getCollectionNames().forEach(function(y) {
55+
if(y.indexOf("cache.chunks.DATABASE.") == 0)
56+
db.getSiblingDB("config").getCollection(y).drop()
57+
})
58+
59+
Where ``DATABASE`` represents the namespace of the database you
60+
just dropped.
61+
62+
#. Use the :dbcommand:`flushRouterConfig` command on **all**
63+
:binary:`~bin.mongos` instances before reading or writing to that
64+
database.
65+
66+
These steps ensure that all cluster nodes refresh their metadata cache,
67+
which includes the location of the :ref:`primary shard<primary-shard>`
68+
for the new database. Otherwise, you may miss data on reads, and may not
69+
write data to the correct shard. To recover, you must manually
70+
intervene.

source/reference/command/dropDatabase.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ User Management
8282
Replica Set and Sharded Clusters
8383
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8484

85-
.. versionchanged:: 3.6
85+
.. versionchanged:: 4.2
8686

8787
Replica Sets
8888
At minimum, :dbcommand:`dropDatabase` waits until all collections

0 commit comments

Comments
 (0)