Mongodb – Could not add Replica Set to MongoDB Shard

dockermongodbmongodb-3.4sharding

I want to deploy a MongoDB Cluster for testing on 3 Virtual Machines using Docker.

On the first Virtual Machine I have the Router Instance and a Replica Set of the 3 Config Servers.

The second Virtual Machine is hosting a Replica Set of three MongoDB Server. They where successfully initiated and elected a Primary.

The third Virtual Machine is hosting a Replica Set of three MongoDB Server. They where successfully initiated and elected an Primary.

When I start my Container which contains the Router it connects successfully to the Replicaset of the Config Server.

If I now want to add my 2 Replica Sets as Shards with the following Command:

sh.addShard("rep1/rep1-node1:27018")

rep1 is the name of the Replica Set. rep1-node1 is the hostname for the Container with the Primary Node. (Host Name resolution works fine.)

After a short time I get the Following Error:

{
    "code" : 133,
    "ok" : 0,
    "errmsg" : "could not find host matching read preference { mode: \"primary\" } for set rep1"
}

I Have found another question here with the same topic but I don't get any errors with missing Authentication Key. None of my Mongo Instance uses any authentication because it's only a testing environment.

Adding normal Mongo Servers works without any problems. Only Replicasets won't work. I'm using MongoDB version 3.4.

Best Answer

So i have found my mistake. I had to add the Servernames and IP Addresses to the hosts file of the docker container in which the config Servers runs. After that i could add my Replica Set as a Shard. I will edit this answer this or next week and provide a link to a Blogpost in which i will describe how to make a MongoDB Sharding with Docker on Virtual Machines.