How to setup a MongoDB replica set for the connector

Posted on

Question :

I want to keep MongoDB and Solr in sync. The mongodb-connector (python) is the tool I want to use to do that.
Now a replica set in mongodb is required (see ReadMe), but I don’t want (and can) setup a set/instance.

So can I have a replica set setup with only the primary and without secondaries? Means: A replica set setup with only one instance? And how does this

Meanwhile I found a solution:

  1. Add in the config-File following:
    • replSet=rs0 (name is variable) (necessary because starting mongod with replSet option didn’t work)
    • oplogSize=[MemSize]
  2. Start mongodb-instance:
    • sudo /etc/init.d/mongod start
    • mongo
  3. Set replicaset:
    • rs.initiate({"_id" : "rs0","version" : 1,"members" : [{"_id" : 0,"host" : "localhost:27017"}]})
    • Check with rs.status()

In mongodb 3.2 “replSet” is changed to “replSetName”

Answer :

You can start a single node replica set by starting the mongod with the replSet argument/config option, that is all that is required. It takes a name argument, which will then function as the name of the replica set in question. Once you have done that, you simply run rs.initiate() from the shell when connected to the node (this is only needed once).

This is described in detail here:

To stay with just a single node, simply do not follow the “Expanding the set” instructions.

Here’s what I did in a similar process for Elasticsearch on a Mac using Brew:

Note: as new daemons are started, switch to new terminals

ElasticSearch (after ‘brew install elasticsearch’):

$ elasticsearch #start es daemon on port 9200

MongoDB just outside the ‘data’ folder [or other database location] (and after ‘brew install mongodb’ and ‘pip install mongo-connector’ and ‘pip install elastic2-doc-manager’):

$ mongod --dbpath data --replSet "rs0" #create replica set on port 27017

$ mongo #initiate replica set

> rs.initiate({"_id" : "rs0","version" : 1,"members" : [{"_id" : 0,"host" : "localhost:27017"}]})

Adding to @scottlittle answer, how I successfully started the mongodb replica for my local mongodb database by using following commands:

First of all shutdown mongod

sudo mongod --shutdown

Then run the following commands to run a replica set:

sudo mongod --dbpath /var/lib/mongodb/ --replSet rs0

Make sure that /var/lib/mongodb/ is your mongodb databases path by using following command

grep dbPath /etc/mongod.conf

rs0 will be your replica set name.

Now login to mongo using mongo and run following command

rs.initiate({"_id" : "rs0","version" : 1,"members" : [{"_id" : 0,"host" : "localhost:27017"}]})

It will initiate replica set. Check the status using command if replica set has successfully initialized


It should print replica set name e.g. in this case is rs0

Leave a Reply

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