Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete database from mongodb with replica set

Tags:

mongodb

I have a MongoDB running with 3 member replica set. All the members are up & running.

I have one database with 4-5 collections and I want to delete that database.

What is the best way to do it. Can I just use db.dropDatabase() on primary? Do I need to stop secondary before I drop the database? After I drop the database will secondary members sync automatically to primary? What about the memory, will it get free after I delete the database?

like image 383
user3407386 Avatar asked Mar 11 '14 18:03

user3407386


2 Answers

Yes. Just drop the database using the command db.dropDatabase() in the primary and the changes will be propagated to secondaries as well. You don't need to shutdown the secondaries.

like image 103
Anand Jayabalan Avatar answered Nov 17 '22 07:11

Anand Jayabalan


as Anand Jayabalan said, you just need to drop the database on primary node. The replication do the rest reading the oplog for the secondaries.

From Reference links and official documentation.

The primary is the only member in the replica set that receives write operations. MongoDB applies write operations on the primary and then records the operations on the primary’s oplog. Secondary members replicate this log and apply the operations to their data sets.

Reference:

https://docs.mongodb.org/manual/core/replication-introduction/

https://docs.mongodb.org/manual/core/replica-set-primary/

like image 22
harryssuperman Avatar answered Nov 17 '22 07:11

harryssuperman