I'm trying to set up a a test environment on my pc including sharding. I've added 2 shardservers, a configsvr and a router instance. When i'm trying to add a shard I get following errormsg: "Can't use localhost as a shard since all shards need to communicate. Either use all shards and configdbs in localhost or all in actual IPs. host: localhost:27017 isLocalHost:1""

I searched online and I found a few topics about it but nowhere they explain how exactly and since I'm new to mongoDB it's quiet hard to understand what's going on. I looked here for example. But since I never used anything else than localhost I find it pretty weird.

This is how I set up:

2 shardsvr instances:

    mongod --shardsvr --replSet rs1 --port 27017 --dbpath D:\data\db
    mongod --shardsvr --replSet rs2 --port 27018 --dbpath D:\data2\db

1 configsvr instance:

    mongod --configsvr --replSet rs0 --port 27019 --dbpath D:\datacfg\db

I initialised the 3 replica sets (another question, do they need to be all different replicasets? or not?).

Then I added a mongos instance:

    mongos --configdb rs0/localhost:27019 --port 27020

And then when I'm trying to connect to mongos & addshards it goes wrong:

    mongo --port 27020


Since I'm never specifying a host I assume that he always uses localhost? no? I don't know how to check anyway. If you need more info tell me.

Thanks in advance

2 Answers 2


Yes, you cannot mix IP addresses and localhost. Those started mongod processes are now using your network device ip-address. If you don't know your machines ip-address, you can check address with ifconfig or ip address command.

Then if you connect mongos using that ip-address instead of localhost. Remember check that your firewall have all those ports open!

  • Thanks for your answer, but wasn't my case. I've added an answer what was the solution for me.
    – JC97
    Nov 2, 2017 at 16:33
  • Yes of course you need to initiate every replica set... That is first thing what you do when you start first node of the replica set.
    – JJussi
    Nov 2, 2017 at 20:16
  • Yeah, I did that as well, but without the optional parameter. If I use the config parameter, it's solved. I assumed he automatically knew that I was on localhost not any other IP. Apperently not.
    – JC97
    Nov 2, 2017 at 22:42

The solution was to add parameters to the rs.initiate().

For example:

rs.initiate({_id : "rs0", version: 1, members: [ {_id : 0, "host" : "localhost:27019"} ] })

And that for each node I start. You need to adjust the _id according to the replSet param you're giving with them & also need do adjust the portnumber at the back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.