Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing mongo replication on OSX

Tags:

mongodb

In two different terminals, I start up mongod with:

mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50 --logpath log.1 --replSet test
mongod --dbpath 2 --port 27002 --smallfiles --oplogSize 50 --logpath log.2 --replSet test

(data subdirectories 1 and 2 already created).

I go into a third window and run the mongo shell against one of the mongod instances

mongo --port 27001

In the shell, I setup a configuration as

cfg = { _id: "test", members: [ {_id:0, host: "localhost:27001"}, {_id:1, host:"localhost:27002"} ] }

then run:

use admin
rs.initiate(cfg)

The response from the shell is:

{
"startupStatus" : 4,
"info" : "hotest",
"errmsg" : "all members and seeds must be reachable to initiate set",
"ok" : 0
}

The logfile for instance on port 27001 reports:

Sun Nov 18 18:32:56 [rsStart] trying to contact macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] couldn't connect to macbookpro.local:27001: couldn't connect to server macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)

It appears that the rs.initiate(cfg) cannot connect to the mongod instance on port 27001. But when I run mongod without --replSet the mongo shell connects just fine.

It isn't clear where I am screwing up but I will bet it is obvious.

like image 290
Mike T Avatar asked Nov 19 '12 03:11

Mike T


People also ask

What is ReplicaSet in MongoDB?

A replica set is a group of mongod instances that maintain the same data set. A replica set contains several data bearing nodes and optionally one arbiter node. Of the data bearing nodes, one and only one member is deemed the primary node, while the other nodes are deemed secondary nodes.

How do you initiate a replica set?

To reset the configuration, make sure every node in your replica set is stopped. Then delete the "local" database for every node. Once you are confident all nodes are not running and the local database are gone, start the mongod process again (of course using the --replSet flag). Then run rs.


1 Answers

Found it!

It turns out that when one installs MongoDB on OSX with brew a configuration file is created at /usr/local/etc/mongo.conf. In this file there was the line:

# Only accept local connections bind_ip = 127.0.0.1

Well, if you look at the log the rsStart error was:

Sun Nov 18 18:32:56 [rsStart] trying to contact macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] couldn't connect to macbookpro.local:27001: couldn't connect to server macbookpro.local:27001
Sun Nov 18 18:32:56 [rsStart] replSet can't get local.system.replset config from self or any seed (yet)

and if I ping macbookpro.local the IP address returned was 192.168.41.1. 192.168.41.1 <> 127.0.0.1 so mongod was refusing to respond. Removing this line from the configuration allowed be to setup replication successfully.

sheesh. I am such an idiot. Looked for hours at this and kept thinking that the machine name when connected to the network was 127.0.0.1. Let this be a lesson to you---don't get old!

like image 75
Mike T Avatar answered Sep 28 '22 08:09

Mike T