|
1 |
| -For MongoDB 4.2 and previous, when executing the |
2 |
| -:dbcommand:`dropDatabase` command, you must perform the following |
3 |
| -additional steps if you intend to create a new database with the same |
4 |
| -name as the dropped database. |
| 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: |
5 | 4 |
|
6 |
| -.. note:: |
| 5 | +- For **MongoDB 4.4 and later**, you must: |
7 | 6 |
|
8 |
| - MongoDB 4.4 does not require these additional steps when dropping and |
9 |
| - recreating a database with the same name. |
| 7 | + #. Run the :dbcommand:`dropDatabase` command on a |
| 8 | + :binary:`~bin.mongos`. |
10 | 9 |
|
11 |
| -- For **MongoDB 4.2**, you must either: |
| 10 | + #. Once the command successfully completes, run the |
| 11 | + :dbcommand:`dropDatabase` command once more on a |
| 12 | + :binary:`~bin.mongos`. |
12 | 13 |
|
13 |
| - - Restart all :binary:`~bin.mongos` instances and all |
14 |
| - :binary:`~bin.mongod` shard members (including the secondary |
15 |
| - members); |
| 14 | +- For **MongoDB 4.2**, you must: |
16 | 15 |
|
17 |
| - - Use the :dbcommand:`flushRouterConfig` command on all |
18 |
| - :binary:`~bin.mongos` instances and all :binary:`~bin.mongod` |
19 |
| - shard members (including the secondary members) before reading |
20 |
| - or writing to that database. |
| 16 | + #. Run the :dbcommand:`dropDatabase` command on a |
| 17 | + :binary:`~bin.mongos`. |
21 | 18 |
|
22 |
| -- For **MongoDB 4.0 and earlier**, you must either: |
| 19 | + #. Once the command successfully completes, run the |
| 20 | + :dbcommand:`dropDatabase` command once more on a |
| 21 | + :binary:`~bin.mongos`. |
23 | 22 |
|
24 |
| - - Restart all :binary:`~bin.mongos` instances; |
| 23 | + #. Use the :dbcommand:`flushRouterConfig` command on **all** |
| 24 | + :binary:`~bin.mongos` instances before reading or writing to that |
| 25 | + database. |
25 | 26 |
|
26 |
| - - Use the :dbcommand:`flushRouterConfig` command on all |
27 |
| - :binary:`~bin.mongos` instances before reading or writing to that |
28 |
| - database. |
| 27 | +- For **MongoDB 4.0 and earlier**, you must: |
| 28 | + |
| 29 | + #. Run the :dbcommand:`dropDatabase` command on a |
| 30 | + :binary:`~bin.mongos`. |
| 31 | + |
| 32 | + #. Connect to each shard's :term:`primary` and verify that the |
| 33 | + namespace has been dropped. If it has not, rerun the |
| 34 | + :dbcommand:`dropDatabase` command again directly from the |
| 35 | + :term:`primary`. |
| 36 | + |
| 37 | + #. Connect to a :binary:`~bin.mongos`, switch to the |
| 38 | + :term:`config database`, and remove any reference to the removed |
| 39 | + namespace from the ``databases``, ``collections``, ``chunks``, |
| 40 | + ``tags``, and ``locks`` collections: |
| 41 | + |
| 42 | + .. code-block:: javascript |
| 43 | +
|
| 44 | + use config |
| 45 | + db.collections.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) |
| 46 | + db.databases.remove( { _id: "DATABASE" }, {writeConcern: {w: 'majority' }} ) |
| 47 | + db.chunks.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) |
| 48 | + db.tags.remove( { ns: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) |
| 49 | + db.locks.remove( { _id: /^DATABASE\./ }, {writeConcern: {w: 'majority' }} ) |
| 50 | +
|
| 51 | + Where ``DATABASE`` represents the namespace of the database you |
| 52 | + just dropped. |
| 53 | + |
| 54 | + #. Connect to the :term:`primary` of each shard, and remove any |
| 55 | + reference to the removed namespace from the ``cache.databases``, |
| 56 | + ``cache.collections``, and ``cache.chunks.DATABASE.COLLECTION`` |
| 57 | + collections: |
| 58 | + |
| 59 | + .. code-block:: javascript |
| 60 | +
|
| 61 | + db.getSiblingDB("config").cache.databases.remove({_id:"DATABASE"}, {writeConcern: {w: 'majority' }}); |
| 62 | + db.getSiblingDB("config").cache.collections.remove({_id:/^DATABASE.*/}, {writeConcern: {w: 'majority' }}); |
| 63 | + db.getSiblingDB("config").getCollectionNames().forEach(function(y) { |
| 64 | + if(y.indexOf("cache.chunks.DATABASE.") == 0) |
| 65 | + db.getSiblingDB("config").getCollection(y).drop() |
| 66 | + }) |
| 67 | +
|
| 68 | + Where ``DATABASE`` represents the namespace of the database you |
| 69 | + just dropped. |
| 70 | + |
| 71 | + #. Use the :dbcommand:`flushRouterConfig` command on **all** |
| 72 | + :binary:`~bin.mongos` instances before reading or writing to that |
| 73 | + database. |
29 | 74 |
|
30 | 75 | These steps ensure that all cluster nodes refresh their metadata cache,
|
31 | 76 | which includes the location of the :ref:`primary shard<primary-shard>`
|
|
0 commit comments