Bryan
Bryan

Reputation: 1519

Mongo replica set can't find primary

I found a tutorial to set up a mongo replica set using docker, my commands were

create network cluster

sudo docker network create curator-cluster

create a particular container named mongo1, map 27018 to 27017 inside and set name is rs0

sudo docker run \
-p 27018:27017 \
--name mongo1 \
--net curator-cluster \
mongo mongod --replSet rs0

my configuration,

config = {
"_id" : "rs0",
"members" : [{"_id" : 0, "host" : "mongo1:27017"},
{"_id" : 1, "host" : "mongo2:27017"},
{"_id" : 2, "host" : "mongo3:27017"}]
}

Eventually, I created 3 containers

5949826d5bb1        mongo                        "/entrypoint.sh mongo"   22 hours ago        Up 22 hours         0.0.0.0:27020->27017/tcp   mongo3
dcf37866dbb6        mongo                        "/entrypoint.sh mongo"   22 hours ago        Up 22 hours         0.0.0.0:27019->27017/tcp   mongo2
14202f76089f        mongo                        "/entrypoint.sh mongo"   22 hours ago        Up 22 hours         0.0.0.0:27018->27017/tcp   mongo1

The result of sudo docker exec -it mongo1 mongo is

MongoDB shell version: 3.2.9
connecting to: test
Server has startup warnings:
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten]
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten]
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2016-09-22T10:24:29.655+0000 I CONTROL  [initandlisten]
rs0:PRIMARY>

Look like I have a primary now, and tried to insert somethings on a container (say mongo1), MongoDBs can sync well.

Now I tried to connect to the set on bryan database with command (note 10.145.168.151 is my IP)

mongo --host rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020 bryan

my result is

MongoDB shell version: 2.6.9
connecting to: rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020/bryan
2016-09-23T16:46:18.819+0800 starting new replica set monitor for replica set rs0 with seeds 10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020
2016-09-23T16:46:18.819+0800 [ReplicaSetMonitorWatcher] starting
2016-09-23T16:46:18.819+0800 changing hosts to rs0/mongo1:27017,mongo2:27017,mongo3:27017 from rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020
2016-09-23T16:46:18.820+0800 getaddrinfo("mongo2") failed: Name or service not known
2016-09-23T16:46:18.821+0800 getaddrinfo("mongo1") failed: Name or service not known
2016-09-23T16:46:18.822+0800 getaddrinfo("mongo3") failed: Name or service not known
2016-09-23T16:46:18.822+0800 Error: connect failed to replica set rs0/10.145.168.151:27018,10.145.168.151:27019,10.145.168.151:27020 at src/mongo/shell/mongo.js:148
exception: connect failed

If I use Nodejs (mongoose), I get MongoError: no primary found in replicaset

I think the problem is getaddrinfo("mongo2") failed: Name or service not known so my question is how to fix this thing. Thank you

Upvotes: 4

Views: 6018

Answers (1)

elmalto
elmalto

Reputation: 1048

Sorry for the late reply, but I ran into this issue with different vnets and missing hostnames. I was under the impression if I connect using ips then the cluster would respond using ips. I was wrong. Even if you connect with ips the hostnames must be available.

But, if you want to, you can change the implementation from hostnames to ips (this is for all clients though)

1) connect to mongo cli

2) cfg = rs.conf()

you will see cfg.members[0].host as "hostname:27017" etc

3) for each entry do cfg.members[i].host = "ip(i):27017"

example: cfg.members[0].host = "10.0.0.1:27017"

4) rs.reconfig(cfg) you should get the response: { "ok" : 1 }

now you should be able to connect. This has caveats, so make sure to think about the consequences (what if ips change etc)

Upvotes: 4

Related Questions