MongoDB Sharding


Here A, B and C are shards respectively running on port no 20000, 20001 and 20002.
mongod --shardsvr --dbpath E:\Mongo\A --port 20000
mongod --shardsvr --dbpath E:\Mongo\B --port 20001
mongod --shardsvr --dbpath E:\Mongo\C --port 20002

Configuration server is running on port no 20003.
mongod --configsvr --dbpath E:\Mongo\config --port 20003

Mongos router is on port no 20005.
mongos --configdb 10.15.1.208:20003 --port 20005

Now connect to the mongos router on port no 20005
mongo --port 20005

Now type the following commands:
use admin
db.runCommand({addShard:"10.15.1.208:20000"})
db.runCommand({addShard:"10.15.1.208:20001"})
db.runCommand({addShard:"10.15.1.208:20002"})

Now enable sharding on database “mydb”.
db.runCommand( { enablesharding : "mydb" } )

Now provide the sharding key for sharding on collection named “mycoll”.
db.runCommand( { shardcollection : "mydb.mycoll", key : { _id : 1 } } )

Type this to get the sharding information.
db.runCommand( { listshards : 1 } )

Now use test database and try to insert some records in the “mycoll” collection.
use mydb
for(i=0;i<800000;i++) { db.mycoll.insert({"name":"hemant kumar","nos allocated is":i}) }

Once done go and check the size of each database (data is distributed).

Comments

Post a Comment

Popular posts from this blog

Index MySQL datadase table in Solr

Hibernate Sharding Example

Shallow Copy