Home > other >  Is it safe to drop the local database in mongodb?
Is it safe to drop the local database in mongodb?

Time:01-30

Sometimes when we drop a database from mongodb, not all the data is removed from the local database if replication is enabled. I wanted to know if it is safe to drop the local database.

enter image description here

CodePudding user response:

By dropping the local database you "de-initialize" the Replica Set, i.e. afterwards you need to run rs.initiate() to get a running Replica Set.

However, you may drop the local database only when your node is running in Maintenance Mode!

CodePudding user response:

The local database in replicaSet or sharded cluster members contain metadata for replication process but it is not replicated itself , if you check the local database content you will see the main consumer is the rs.oplog collection which by default occupy 5% of your partition , so if you have big partition the oplog capped collection will ocupy more space , the good news are that you may resize the oplog manually after version 3.6 with the command:

db.adminCommand({replSetResizeOplog: 1, size: 990})

where you limit the oplog collection to 990MB ( 990MB is the minimmum allowed size of rs.oplog )

Dropping the local database is not generally recommended.

In your case it looks you have 400GB partition and mongo automatically capped the rs.oplog to 20GB .

If you try to drop the database when replicaSet mode is active you will get an error:

   rs1:PRIMARY> use local
   switched to db local
   rs1:PRIMARY> db.runCommand( { dropDatabase: 1 } )
   {
    "operationTime" : Timestamp(1643481374, 1),
    "ok" : 0,
    "errmsg" : "Cannot drop 'local' database while replication is active",
    "code" : 20,
    "codeName" : "IllegalOperation",
    "$clusterTime" : {
     "clusterTime" : Timestamp(1643481374, 1),
     "signature" : {
        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
        "keyId" : NumberLong(0)
           }
        }
    }
    rs1:PRIMARY> 

If you try dropping the rs.oplog collection only , it is also no possible in replication mode:

rs1:PRIMARY> db.oplog.rs.drop()
uncaught exception: Error: drop failed: {
"ok" : 0,
"errmsg" : "can't drop live oplog while replicating",
"$clusterTime" : {
    "clusterTime" : Timestamp(1643482576, 1),
    "signature" : {
        "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
        "keyId" : NumberLong(0)
    }
},
    "operationTime" : Timestamp(1643482576, 1)
 } :
 _getErrorWithCode@src/mongo/shell/utils.js:25:13
 DBCollection.prototype.drop@src/mongo/shell/collection.js:713:15
 @(shell):1:1
 rs1:PRIMARY> 

so if you still want to drop it you will need to restart the member as standalone ( without replication mode active ) to be able to drop it.

Following is the content of typical local database(v4.4 in example):

 > use local
 switched to db local
 > show collections
 oplog.rs
 replset.election
 replset.initialSyncId
 replset.minvalid
 replset.oplogTruncateAfterPoint
 startup_log
 system.replset
 system.rollback.id
 > 

and this is how you can drop it:

> use local
switched to db local
> db.runCommand( { dropDatabase: 1 } )
{ "dropped" : "local", "ok" : 1 }
> 

Bear in mind after dropping the collection all local replication info will be lost , if the member was SECONDARY before restarting in standalone mode there will be no issues since after restarting in replication mode the member will get its configuration from the PRIMARY so local database will be recreated with all its collections.

If the member was PRIMARY and no other seeding members available , the replication info will be lost and you will need to rs.initiate() the collection once again.

  •  Tags:  
  • Related