2
votes

I am Trying to implement sharding on our mongoDB data base , Here is the scenario

we have 1 server running config server

we have 2 servers running mongod instances called node1 and node2

We have 1 server running mongos

I have added the servers node1 and node2 as shard servers in the mongos shell by using the command

db.runCommand( { addshard : "serverhostname[:port]" } );

later I created database in node1 and enabled sharding for that database from Mongos

Now we have 2 shard servers and as per MongoDB documentation mongos should put some collections which are created node1 ( where the database actually resides ) and some collections on node 2 .

I created 300 collections from mongos but still all collections are going to only one server what is wrong in the above mentioned support

NOTE : - Since I am not in a position to upgrade the application layer I cant shard in collection level , I have a script which creates several collections dynamically for some requirement .

Please Provide your suggestions on how we can shard in database level so that collections gets equally distributed among all shard servers .

I am looking for some solution like

IF i create 300 collections automatically mongos should create around 150 or 100 collections in each of the two sharded servers .

Thanks in advance for stepping in and helping me fix this problem.

1

1 Answers

2
votes

Collections are not automatically distributed across shards until you enable sharding on them and then add data. Those sharded collections will then have data on both shards (split into chunks and balanced automatically).

Until you actually run the command on the collections, and have enough data in them so that they start splitting and migrating chunks, then they will all stay where they are.

That is because each database has a primary shard, which does not change. That is where the database will be created initially and where all of its non-sharded data will live. If you add another database, the mongos should create it on the second shard and alternate (or round robin if more than two) for each created database. In each case, until you shard the collection, it will stay solely on the primary shard for that database.