0

I setup a cluster on aws that looks like this

- mycluster
--- shard0
----- node0 : P
----- node1 : S
----- node2 : S
--- shard1
----- node0: P
----- node1: S
----- node2: S
--- shard2
----- node0: P
----- node1: S
----- node2: S

I want to connect to it via a connection string URI.

My connection string currently look like this:

mongodb://host0:portX,...,host9:portZ/test-database?replicaSet=mycluster

but for some reason I get the error not reachable when I attempt to connect with it.

However, I can connect to a single shard just fine.

mongodb://host0:portX,...,host3:portZ/test-database?replicaSet=shard0

What am I missing here that is preventing me from connecting to the whole cluster?

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
Eltorrooo
  • 157
  • 2
  • 15

1 Answers1

5

In a shard you connect to the shard router, i.e. the mongos process. You don't connect to every individual shard. Only mongos does that.

BanksySan
  • 27,362
  • 33
  • 117
  • 216
  • 2
    I would like to add that it can wreak havoc on data consistency when connecting to shards directly. – Markus W Mahlberg Apr 08 '16 at 16:09
  • 1
    @MarkusWMahlberg I didn't even realise you could connect to one from the client. – BanksySan Apr 08 '16 at 16:11
  • 1
    You can, since each shard is unaware that it is a shard. It is a standalone or replset from its own perspective. Now imagine you mix up the key ranges... – Markus W Mahlberg Apr 08 '16 at 16:18
  • @MarkusWMahlberg Odd that replica set members know that they are a replica set member, but shards don't. The more you know... – BanksySan Apr 08 '16 at 16:29
  • Think of it. A replica set member has to be aware that it is part of a replset in order for voting and health checks. But it does not have to know that it is part of a sharded cluster in order to hold data. Which makes the sharding logic indefinitely less complex. – Markus W Mahlberg Apr 08 '16 at 16:37
  • Aye, I can see why. I had assumed purely on the basis that only a mongos should connect to it. That being said, there might well be (and probably are) ledgitimate admin reasons to connect to a shard. – BanksySan Apr 08 '16 at 16:38
  • Of course there are. Adding replica set members, for example. – Markus W Mahlberg Apr 08 '16 at 16:42
  • What happen if the server which host the "mongos" goes down? Is it better to setup a mongos on each server, so we have 3 mongos? – Elteroooo Apr 08 '16 at 17:31
  • @ElTero Aye, there's a high level discussion [here](https://www.kchodorow.com/blog/2010/08/09/sharding-and-replica-sets-illustrated/) – BanksySan Apr 08 '16 at 17:41
  • @BanksySan assume one replicaSet has the data then you connect to the mongos, all my queries fail – Elteroooo Apr 08 '16 at 17:53
  • @ElTero I'm afraid I don't know what else to suggest. – BanksySan Apr 08 '16 at 19:57