MongoDB – How to Reset Mongo Replica Set

The following instructions would help you to reset MongoDB replica set settings and start over with replication all over again. These are especially helpful for developers starting to learnĀ MongoDB replication.

  • Shut down the mongodb server
  • Start the mongodb server in the standalone mode, e.g., without –replSet <replicaSetName>.
  • Login to Mongodb; You can use admin database
  • Make sure that user has readWrite permission to local database. In case, it is not, use following command to grant the role to the user. The following command gives readWrite permission on local database to admin user.
    db.grantRolesToUser("admin", [{role: "readWrite", db: "local"}]);
    
  • Switch to local database
  • Execute the command to make the cllection such as system.replset empty
    db.system.replset.remove({});
    
  • Make sure that system.replset in local database is empty by executing the following command:
    db.system.replset.find();
    
  • Start the MongoDB database with replSet option all over again; The following command can be used:
    sudo mongod --dbpath /path/to/mongo/db --replSet rs0
  • Login to Mongodb using admin user and execute following command to initialize the replication set:
    rs.initiate();
    
  • Switch over to local database and execute following command to make sure that there exists an entry for the replication.
    db.system.replset.find();
    
Ajitesh Kumar
Follow me

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. For latest updates and blogs, follow us on Twitter. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking. Check out my other blog, Revive-n-Thrive.com
Posted in MongoDB, NoSQL. Tagged with , .

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *