Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

primary shards in mongodb

Tags:

I am trying to go through the documents for mongodb sharding and I am a bit confused about how the primary shards are assigned and the meaning of primary shards

As per the documentation it stats that

Every database has a primary shard that holds all the un-sharded collections for a database. The primary shard has no relation to the primary in a replica set.

does this mean that a primary shard is the shard (or the collection of servers) in the shard where the database was first created ?

Say I have 3 shards s0, s1 and s2. In each shard i have 3 servers

  • s0 Server 1 (Primary)
  • s0 Server 2 (Secondary)
  • s0 - Server 3 (Secondary)

  • s1 - Server 4 (Primary)

  • s1 - Server 5(Secondary)
  • s1 - Server 6(Secondary)

  • s2 - Server 7(Primary)

  • s2 - Server 8(Secondary)
  • s2 - Server 9(Secondary)

Now when I create a new database via a mongoclient and via mongos . Now say that request goes to the shard 1 (s1). Since create database is a write operation it would go to the server 4 in shard 1 and thus s1 becomes by primary shard ?

If on the other hand the request would have traversed to s2 shard then server 7 would being primary would have created the collection and then s2 would have been my primary shard ?

Is this understanding correct or I am going wrong some where ?

like image 497
user641887 Avatar asked Aug 09 '16 00:08

user641887


1 Answers

Yes, you are right! And you understanded concept.

As long as you don't shard individual collection, collection must have primary location, at one of the replica sets. So, think those individual shards as individual RS's. So, one DB is located to one shard and if you shard collection inside DB, collection is "balanced" to multiple shards. With TAG's you can decide where that collection is spread. Normal situation is spread it to all shards evenly.

And you can always make decision what is DB's primary location. After creating, you can "move" it, when DB is still empty or quite small, it's fast operation. movePrimary

like image 130
JJussi Avatar answered Oct 11 '22 13:10

JJussi