0

I have two PCs with IP addresses 10.1.1.52 and 10.1.1.11. I want to configure mongo replication (without sharding) with one primary and one secondary node running on 10.1.1.52 and one secondary node running on 10.1.1.11.

I have followed these steps:

Ran three config servers on 10.1.1.11 PC with the following commands::

mongod --configsvr --port 26050 --logpath /data/db/log.cfg0 --logappend --dbpath /data/db/cfg0 –fork
mongod --configsvr --port 26051 --logpath /data/db/log.cfg1 --logappend --dbpath /data/db/cfg1 –fork
mongod --configsvr --port 26052 --logpath /data/db/log.cfg2 --logappend --dbpath /data/db/cfg2 –fork

Following which I created a replica set ('a') and added three nodes to it by using the following commands (the first on 10.1.1.11 & the other two on 10.1.1.52):

mongod  --replSet a --dbpath /data/a/a0 --logpath /data/a/log.a0 --port 27000 --fork --logappend --smallfiles --oplogSize 50
mongod  --replSet a --dbpath /data/a/a1 --logpath /data/a/log.a1 --port 27001 --fork --logappend --smallfiles --oplogSize 50
mongod  --replSet a --dbpath /data/a/a2 --logpath /data/a/log.a2 --port 27002 --fork --logappend --smallfiles --oplogSize 50

I set up query routers on 10.1.1.11 with the following command:

mongos --configdb gaugedata-elastic:26050,gaugedata-elastic:26051,gaugedata-elastic:26052 --fork --logappend --logpath /data/db/log.mongos --port 26060

After that I entered the following command to connect from mongos and initiate the replica set:

mongo --port 27000
rs.initiate()

The problem that is being faced is after adding the nodes:

rs.add("10.1.1.52:27001")
rs.add("10.1.1.52:27002")

The nodes running on remote PC i.e. 10.1.1.52 are not reachable and unhealthy. However the node running on my PC i.e. 10.1.1.11 is automatically declared as SECONDARY.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Sidd
  • 11
  • 3

2 Answers2

1

It is necessary to check that the rule accepting ports is activated. Otherwise use the command sudo ufw allow <start port number>:<end port number> on ubuntu OS , to enable all ports between start port number and end port number.

0

OK.. There is few mistakes here. IF you don't need sharding, you don't need mongoS or config servers... Pure replica set (in this case) is just those two machines, both running ONLY mongod process...

So, start up mongod processes (in two different machines)

mongod --replSet=a --dbpath=/data/ --logpath=/data/mongod.log --port=27017 --fork --logappend --smallfiles --oplogSize=50

Then connect to one of the nodes with mongo:

mongo 10.1.1.11

Then you make replica set initial, with one command:

rs.initiate( { _id: "a", version: 1, members: [ { _id: 0, host : "10.1.1.11:27017", priority:2 }, { _id: 1, host : "10.1.1.52:27017", priority:1 } ] } )

And if you really want to have two different RS nodes in one physical machine, you can of course add one more with different port...

Here is working example with one machine:

#> mkdir /data 
#> mkdir /data2

#> mongod --replSet=a --dbpath=/data --logpath=/data/mongod.log --port=27001 --fork --logappend --smallfiles --oplogSize=50

#> mongod --replSet=a --dbpath=/data2 --logpath=/data2/mongod.log --port=27001 --fork --logappend --smallfiles --oplogSize=50

#> mongo --port=27000

MongoDB Enterprise > rs.initiate({_id:"a", version:1, members:[{_id:0,host:"127.0.0.1:27000",priority:2},{_id:1,host:"127.0.0.1:27001",priority:1}]})
{ "ok" : 1 }
MongoDB Enterprise a:OTHER>
MongoDB Enterprise a:PRIMARY> rs.status()
{
        "set" : "a",
        "date" : ISODate("2016-12-04T08:47:33.254Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "members" : [
                {
                        "_id" : 0,
                        "name" : "127.0.0.1:27000",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 451,
                        "optime" : {
                                "ts" : Timestamp(1480841055, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-12-04T08:44:15Z"),
                        "electionTime" : Timestamp(1480841054, 1),
                        "electionDate" : ISODate("2016-12-04T08:44:14Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "127.0.0.1:27001",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 209,
                        "optime" : {
                                "ts" : Timestamp(1480841055, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2016-12-04T08:44:15Z"),
                        "lastHeartbeat" : ISODate("2016-12-04T08:47:32.831Z"),
                        "lastHeartbeatRecv" : ISODate("2016-12-04T08:47:33.128Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "127.0.0.1:27000",
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
JJussi
  • 1,540
  • 12
  • 12
  • I did exactly as you said. When I connect to one of the nodes using *mongo 10.1.1.11* it says **This node was not started with the replSet option** Kindly let me know what should I do next. – Sidd Nov 21 '16 at 04:57
  • OK. I did **mongo --port 27000** and then used **rs.initiate( { _id: "a", version: 1, members: [ { _id: 0, host : "10.1.1.11:27000", priority:2 }, { _id: 1, host : "10.1.1.52:27001", priority:1 }, { _id: 2, host : "10.1.1.52:27002", priority:2 } ] } )** The outut was after rs.status() showed that id:1 and id:2 are still not reachable. Any idea ? – Sidd Nov 21 '16 at 05:37