Skip to content

Commit e1108bb

Browse files
committed
DOCS-14619 clarify dropDatabases sharded cluster procedure
1 parent de09440 commit e1108bb

File tree

2 files changed

+56
-11
lines changed

2 files changed

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

source/reference/command/dropDatabase.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ User Management
7777
Replica Set and Sharded Clusters
7878
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7979

80-
.. versionchanged:: 3.6
80+
.. versionchanged:: 4.0
8181

8282
Replica Sets
8383
At minimum, the method waits until all collection drops in

0 commit comments

Comments
 (0)