Showing posts with label Sharding. Show all posts
Showing posts with label Sharding. Show all posts

Tuesday, June 30, 2015

Sharding in MongoDB

Horizontal scaling
Method of storing data across multiple m/c
Shards stores user related data



  • Start Server service
mongod --configsvr --dbpath C:\data\confdb --port 27019

  • Start  Agent service
mongos --configdb localhost:27019

  • Start  Database server
mongod --dbpath C:\data\sh1 --port 27020

  • Start Client
C:\>mongo --host localhost --port 27017
MongoDB shell version: 2.6.5
connecting to: localhost:27017/test
mongos>


mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("55936ac576a153b4e1f67c45")
}
  shards:
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

  • Adding shard

mongos> sh.addShard("localhost:27020")
{ "shardAdded" : "shard0000", "ok" : 1 }

  • Check status again

mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("55936ac576a153b4e1f67c45")
}
  shards:
        {  "_id" : "shard0000",  "host" : "localhost:27020" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }

Enable sharding
mongos> use mydb1
switched to db mydb1
mongos> sh.enableSharding("mydb1")
{ "ok" : 1 }

mongos> db.createCollection("shTbl")
{ "ok" : 1 }
mongos> db.shTbl.insert({x:1})
WriteResult({ "nInserted" : 1 })
mongos> db.shTbl.find()
{ "_id" : ObjectId("55936e5fc7deef0077d13a9c"), "x" : 1 }
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("55936ac576a153b4e1f67c45")
}
  shards:
        {  "_id" : "shard0000",  "host" : "localhost:27020" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "mydb1",  "partitioned" : true,  "primary" : "shard0000" }

mongos> db.shTbl.ensureIndex({x:1})
{
        "raw" : {
                "localhost:27020" : {
                        "createdCollectionAutomatically" : false,
                        "numIndexesBefore" : 1,
                        "numIndexesAfter" : 2,
                        "ok" : 1
                }
        },
        "ok" : 1
}
mongos> db.shTbl.getIndexex()
2015-07-01T10:09:05.018+0530 TypeError: Property 'getIndexex' of object mydb1.shTbl is not a functi
mongos> db.shTbl.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mydb1.shTbl"
        },
        {
                "v" : 1,
                "key" : {
                        "x" : 1
                },
                "name" : "x_1",
                "ns" : "mydb1.shTbl"
        }
]
mongos> sh.shardCollection("mydb1.shTbl",{"x":1})
{ "collectionsharded" : "mydb1.shTbl", "ok" : 1 }
mongos>


mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("55936ac576a153b4e1f67c45")
}
  shards:
        {  "_id" : "shard0000",  "host" : "localhost:27020" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "mydb1",  "partitioned" : true,  "primary" : "shard0000" }
                mydb1.shTbl
                        shard key: { "x" : 1 }
                        chunks:
                                shard0000       1
                        { "x" : { "$minKey" : 1 } } -->> { "x" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1,
)

mongos>

mongos> db.shTbl.insert({x:2})
WriteResult({ "nInserted" : 1 })
mongos> db.shTbl.insert({x:3})
WriteResult({ "nInserted" : 1 })
mongos> db.shTbl.insert({x:4})
WriteResult({ "nInserted" : 1 })
mongos>
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
        "_id" : 1,
        "version" : 4,
        "minCompatibleVersion" : 4,
        "currentVersion" : 5,
        "clusterId" : ObjectId("55936ac576a153b4e1f67c45")
}
  shards:
        {  "_id" : "shard0000",  "host" : "localhost:27020" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "mydb1",  "partitioned" : true,  "primary" : "shard0000" }
                mydb1.shTbl
                        shard key: { "x" : 1 }
                        chunks:
                                shard0000       1
                        { "x" : { "$minKey" : 1 } } -->> { "x" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1,
)

mongos> db.shTbl.find()
{ "_id" : ObjectId("55936e5fc7deef0077d13a9c"), "x" : 1 }
{ "_id" : ObjectId("55936facc7deef0077d13a9d"), "x" : 2 }
{ "_id" : ObjectId("55936fb7c7deef0077d13a9e"), "x" : 3 }
{ "_id" : ObjectId("55936fbec7deef0077d13a9f"), "x" : 4 }
mongos>







Test the sharding by connecting to sharded client of port 27020


C:\>mongo --port 27020
MongoDB shell version: 2.6.5
connecting to: 127.0.0.1:27020/test


> show dbs
admin  (empty)
local  0.078GB
mydb1  0.078GB
> use mydb1
switched to db mydb1
> show collections
shTbl
system.indexes
> db.shTbl.find()
{ "_id" : ObjectId("55936e5fc7deef0077d13a9c"), "x" : 1 }
{ "_id" : ObjectId("55936facc7deef0077d13a9d"), "x" : 2 }
{ "_id" : ObjectId("55936fb7c7deef0077d13a9e"), "x" : 3 }
{ "_id" : ObjectId("55936fbec7deef0077d13a9f"), "x" : 4 }
>