Error when setting up replica set on Mongodb


#1

I’m trying to set up a replica set on my Mongodb so that I can use oplog tailing.

I’m using these instructions:


I’ve added this code to /etc/mongod.conf:

replication:
  replSetName: rs0
  oplogSizeMB: 100

Then run the mongo shell:

mongo

In the shell:

use local
rs.initiate()

I see this error in the mongo console:

{
	"ok" : 0,
	"errmsg" : "This node was not started with the replSet option",
	"code" : 76,
	"codeName" : "NoReplicationEnabled"
}

Any idea what might be wrong? My mongo.conf shows:

net:
  port: 27017
  bindIp: 127.0.0.1

so I’m running the expected port, I think. I have tried restarting mongodb with sudo service mongod start. What have I missed? Thanks!


#2

In your config files at startup, you pass to mongod the replSet (e.g. “rs0”) and the keyfile (read mongo docs)
On master, in mongo shell, you add all the replica nodes, it takes care of communicating with the slaves
Make sure slaves are blank otherwise replication fails


#3

I’m not sure about the configuration file, but a correct command line will look like, for example:

mongod --dbpath=/var/db --bind_ip_all --replSet replSetName --smallfiles --oplogSize 100

That seems consistent. However, what it seems that you do differently is use local. I would run the replSetInitiate command against admin, not local, which I think is your issue.


#4

Agreed, not sure why you need use local there


#5

Thank you for the comments guys. I think I have figured it out, a combination of something not specified in the docs and a mistake on my part. Here are my full steps to get it working for a Meteor app deployed with Phusion Passenger on a Digital Ocean droplet.

  1. Set up replica set

SSH to the server and in the terminal, type:

sudo nano /etc/mongod.conf

In this doc, add:

replication:
  replSetName: rs0
  oplogSizeMB: 100

Now add your hostname as an alias. First identify your hostname by typing:

hostname

This returns, say, ‘myhostname’. Now type:

nano /etc/hosts

And to this file add:

127.0.0.1 myhostname

Restart mongodb:

sudo service mongod stop
sudo service mongod start

Initiate the replica set:
mongo

Then in the mongo shell:

use local
rs.initiate()
  1. Configure Meteor (through Passenger)

Exit mongo shell. Type:

sudo nano /etc/nginx/sites-enabled/myappname.conf

(Whatever is your Passenger configuration file)

Add this to the file below MONGO_URL

passenger_env_var MONGO_OPLOG_URL mongodb://localhost:27017/local;

Restart nginx for the Passenger changes to take effect:

sudo service nginx restart

Your friends in the wilderness are the mongo and nginx logs:

nano /var/log/mongodb/mongod.log
nano /var/log/nginx/error.log