3

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" https://docs.mongodb.com/v3.2/tutorial/upgrade-config-servers-to-replica-set/

3 Answers 3

2
+50

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:

http://docs.mongodb.org/manual/tutorial/convert-standalone-to-replica-set/

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

4
  • That didn't work. Meanwhile, I found a solution which is similar to yours: (edit will come soon.)
    – MUmla
    Nov 27, 2013 at 10:22
  • Odd, because I have done this maybe.......30 times when testing without a problem, and those are the official docs too
    – Adam C
    Nov 27, 2013 at 10:40
  • (See my solution in the question above.)
    – MUmla
    Nov 29, 2013 at 10:02
  • your solution is identical to the one I provided, all you did was fill in variable names - if you do not specify an initial replica set name, it is filled in by default as the current process. Similarly oplogSize is optional - if you do not specify it, it will default to 5% of free space. Similarly, none of the other commands are optional and are not required to provision an oplog or set up a single node replica set.
    – Adam C
    Nov 29, 2013 at 20:16
2

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"}]})
0

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

rs.status().set

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.