mongodbreplication

How to configure a replica set with MongoDB


I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.

Below you can see an overview of my goal.

I want configure Replication Set in MongoDB for that i tried like this

use local
db.dropDatabase()

config = { _id: "rs0", members:[
{_id: 0, host: 'localhost:27017'}]
}

rs.initiate(config)

i hope every thing is correct only but here its showing the following error message

{ "errmsg" : "server is not running with --replSet", "ok" : 0 }

Anything wrong i did here ?

Any ideas ?


Solution

  • You can actually follow the MongoDB Manual to setup your replica set. It's pretty clear. Here are some critical steps described below:

    1. Change your configuration file and add the following line.Don't mess it up with master/slave replication, because replica set is meant to be a replacement of master/slave replication. So you may want to remove those master/slave related config from your configuration files.

      replSet = [set name]
      

      EDIT: The replSet does not seem to exist anymore in recent versions of mongoDB, at least it is no longer documented. The following seems to have done the trick in my case.

      replication: 
        replSetName: "smm"
      
    2. Restart your mongod instance:

      systemctl restart mongodb
      // or
      service mongod restart
      
    3. go to local database and initiate your replica set. Don't pass anything to the initiate function, and mongodb will handle everything just well. Note it will use your hostname as the name of current instance and as I know it's not that easy to change. So you may want to change your host name before doing so.

      use local
      rs.initiate()
      

    That's it. Your set is good to go. If you have other member to join the set, you need to do the 1/2 steps, and go to your primary instance and type:

    rs.add("hostname:port")
    

    Only when you want to change configs of replica set, do you need to type:

    var conf = rs.conf();
    // change your conf here
    rs.reconfig(conf);
    

    Note this will lead to server offline a little bit time. If you are doing it online, be careful.