daniilyar
daniilyar

Reputation: 2652

MongoDB sharding: host does not belong to replica set

I am a Mongo newbie. I am trying to sping up a MongoDB cluster with both sharding and replication. Cluster schema which I want to implement is: https://github.com/ansible/ansible-examples/raw/master/mongodb/images/site.png I am using server IP as replication set name. I.e. I am building replication sets with commands below:

rs.initiate()
rs.add("10.148.28.51:27118")
rs.add("10.148.28.52:27118")
rs.add("10.148.28.53:27118")

Replication is being configured correctly so when I am executing rs.status() on PRIMARY host 10.148.28.51 I am getting "10.148.28.51" as repl.set name: https://gist.github.com/daniilyar/630bc6fe7723ed06f243

But when I am trying to add shards at mongos instance it gives me 2 opposite errors (depending on what addShard() syntax variation I use):

mongos> sh.addShard("10.148.28.51:27118")
{
"ok" : 0,
"errmsg" : "host is part of set 10.148.28.51, use replica set url format     <setname>/<server1>,<server2>,...."
}
mongos> sh.addShard("10.148.28.51/10.148.28.51:27118") 
{
"ok" : 0,
"errmsg" : "in seed list 10.148.28.51/10.148.28.51:27118, host 10.148.28.51:27118 does not belong to replica set 10.148.28.51"
}

How do I add shard if Mongo tells that "host X is in replica set Y" and that "host X does not belong to replica set Y" in the same time?

Any help would be greatly appreciated

Upvotes: 0

Views: 2815

Answers (3)

satya ashok dowluri
satya ashok dowluri

Reputation: 31

So the error when you are trying to add a shard is,

host is part of set <replicationset>; use replica set url format <setname>/<server1>,<server2>, ...

Let's try to resolve it from the root cause.
It's mostly because of the latest MongoDB version.
Make sure to create the shard-0, shard-1 directories in the mongodb directory before doing the below sharding.
So, if I added my shards below (in the latest mogodb version, you must mention the replication set). If you don't mention the replication set, you might get an error saying "Cannot start a shardsvr as a standalone server"

- mongod --shardsvr --replSet shard-rs --port 27017 --bind_ip 127.0.0.1 --dbpath /opt/homebrew/var/mongodb/shard-0/ --oplogSize 128
- mongod --shardsvr --replSet shard-rs --port 27018 --bind_ip 127.0.0.1 --dbpath /opt/homebrew/var/mongodb/shard-1/ --oplogSize 128
- connect to `mongosh --port 27017` and intiate replication set
            
            rs.initiate(
                {
                _id: "shard-rs",
                members: [
                    { _id: 0, host: "127.0.0.1:27017" },
                    { _id: 1, host: "127.0.0.1:27018" }
                    ] 
                }
            )
            

Now, we need to add the shards like:

sh.addShard("shard-rs/127.0.0.1:27017,127.0.0.1:27018")

Hope this helps.

Upvotes: 0

daniilyar
daniilyar

Reputation: 2652

Thank you for good explanation, now I understand. If you use in rs.add(IP:port), Mongo adds replica set member with name ip-X-Y-Z-R:. It seems to be Mongo's default behavior. So in my case solution was to use command:

sh.addShard("10.148.28.51/**ip-10-148-28-51**:27118") 

instead of:

sh.addShard("10.148.28.51/**10.148.28.51**:27118") 

Upvotes: 0

nomDePlum
nomDePlum

Reputation: 101

From your description sounds like you need to tweak the way your are using the rs.add(..) command. You state you are using the IP address as the name of the replica set but this is not how rs.add(...) interprets the argument.

The argument you pass is the hostname (or IP) and port of the mongod instance you are looking to add to the replica set notthe replica set name. You set-up this configuration when connected via mongo to the primary. The replSet name is set when the primary is started:

mongod --replSet "rs1"

sets the as name of rs1.

I'd have a read over: http://docs.mongodb.org/manual/tutorial/convert-replica-set-to-replicated-shard-cluster/ as it covers pretty much what you appear to be trying to do.

I'd also consider what you are trying to achieve as it sounds (from your description) like you may end up with a single replicated shard (!!!) when you most probably are looking to create multiple shards each of which have their data replicated.

References:

rs.add command - http://docs.mongodb.org/manual/reference/method/rs.add/

rs.addShard command - http://docs.mongodb.org/manual/reference/method/sh.addShard/ Sharded Cluster - http://docs.mongodb.org/manual/core/sharded-cluster-components/

Upvotes: 1

Related Questions