Sharding:Shard Cluster 增加 shard 节点  


一 现有环境

--1.1 查看Sharding Cluster 状态


 

mongos> sh.status();
---Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
        {  "_id" : "shard0000",  "host" : "redhatB.example.com:5281" }
        {  "_id" : "shard0001",  "host" : "redhatB.example.com:5282" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard0000" }
        {  "_id" : "francs",  "partitioned" : true,  "primary" : "shard0000" }
                francs.test_1 chunks:
                                shard0000       2
                                shard0001       1
                        { "id" : { $minKey : 1 } } -->> { "id" : 1 } on : shard0000 Timestamp(2000, 1) 
                        { "id" : 1 } -->> { "id" : 11828 } on : shard0000 Timestamp(1000, 3) 
                        { "id" : 11828 } -->> { "id" : { $maxKey : 1 } } on : shard0001 Timestamp(2000, 0) 
        {  "_id" : "records",  "partitioned" : false,  "primary" : "shard0000" }

      备注:两单节点的 shard。

 

--1.2 查看当前 test_1 集合状态


 

mongos> db.test_1.stats();
{
        "sharded" : true,
        "ns" : "francs.test_1",
        "count" : 50000,
        "numExtents" : 10,
        "size" : 1800024,
        "storageSize" : 5849088,
        "totalIndexSize" : 3131408,
        "indexSizes" : {
                "_id_" : 1684256,
                "id_1" : 1447152
        },
        "avgObjSize" : 36.00048,
        "nindexes" : 2,
        "nchunks" : 4,
        "shards" : {
                "shard0000" : {
                        "ns" : "francs.test_1",
                        "count" : 34350,
                        "size" : 1236600,
                        "avgObjSize" : 36,
                        "storageSize" : 3055616,
                        "numExtents" : 5,
                        "nindexes" : 2,
                        "lastExtentSize" : 2359296,
                        "paddingFactor" : 1,
                        "systemFlags" : 1,
                        "userFlags" : 0,
                        "totalIndexSize" : 2125760,
                        "indexSizes" : {
                                "_id_" : 1144640,
                                "id_1" : 981120
                        },
                        "ok" : 1
                },
                "shard0001" : {
                        "ns" : "francs.test_1",
                        "count" : 15650,
                        "size" : 563424,
                        "avgObjSize" : 36.00153354632588,
                        "storageSize" : 2793472,
                        "numExtents" : 5,
                        "nindexes" : 2,
                        "lastExtentSize" : 2097152,
                        "paddingFactor" : 1,
                        "systemFlags" : 1,
                        "userFlags" : 0,
                        "totalIndexSize" : 1005648,
                        "indexSizes" : {
                                "_id_" : 539616,
                                "id_1" : 466032
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}

     备注:集合 test_1 的数据分布在两 shard 节点。

 

 

二 新增 shard3 节点

--2.1 创建 shard3 数据目录和配置文件

 [shard@redhatB shard]$ mkdir -p /shard/shard3
[shard@redhatB shard]$ touch /shard/shard3/shard3_5283.conf 
vim /shard/shard3/shard3_5283.conf
fork = true
port = 5283
dbpath = /shard/shard3
logpath = /shard/shard3/shard3.log
logappend = true
journal = true

   

--2.2 启动 shard3 节点

 [shard@redhatB shard]$ mongod -f /shard/shard3/shard3_5283.conf
forked process: 23241
all output going to: /shard/shard3/shard3.log
child process started successfully, parent exiting

 

--2.3 新增 shard 节点

 [shard@redhatB ~]$ mongo 127.0.0.1:7282
MongoDB shell version: 2.2.1
connecting to: 127.0.0.1:7282/test
mongos> sh.addShard("redhatB.example.com:5283");  
{ "shardAdded" : "shard0002", "ok" : 1 }
     备注:通过命令 addShard 新增 shard 节点。

--2.4 再次查看 shard cluster 状态

 mongos> sh.status();
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
        {  "_id" : "shard0000",  "host" : "redhatB.example.com:5281" }
        {  "_id" : "shard0001",  "host" : "redhatB.example.com:5282" }
        {  "_id" : "shard0002",  "host" : "redhatB.example.com:5283" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard0000" }
        {  "_id" : "francs",  "partitioned" : true,  "primary" : "shard0000" }
                francs.test_1 chunks:
                                shard0002       1
                                shard0000       1
                                shard0001       1
                        { "id" : { $minKey : 1 } } -->> { "id" : 1 } on : shard0002 Timestamp(3000, 0) 
                        { "id" : 1 } -->> { "id" : 11828 } on : shard0000 Timestamp(3000, 1) 
                        { "id" : 11828 } -->> { "id" : { $maxKey : 1 } } on : shard0001 Timestamp(2000, 0) 
        {

        

--2.5 查看 test_1 集合状态

 

mongos> db.test_1.stats();
{
        "sharded" : true,
        "ns" : "francs.test_1",
        "count" : 50000,
        "numExtents" : 15,
        "size" : 1800048,
        "storageSize" : 8642560,
        "totalIndexSize" : 3147760,
        "indexSizes" : {
                "_id_" : 1692432,
                "id_1" : 1455328
        },
        "avgObjSize" : 36.00096,
        "nindexes" : 2,
        "nchunks" : 4,
        "shards" : {
                "shard0000" : {
                        "ns" : "francs.test_1",
                        "count" : 1964,
                        "size" : 70704,
                        "avgObjSize" : 36,
                        "storageSize" : 3055616,
                        "numExtents" : 5,
                        "nindexes" : 2,
                        "lastExtentSize" : 2359296,
                        "paddingFactor" : 1,
                        "systemFlags" : 1,
                        "userFlags" : 0,
                        "totalIndexSize" : 138992,
                        "indexSizes" : {
                                "_id_" : 73584,
                                "id_1" : 65408
                        },
                        "ok" : 1
                },
                "shard0001" : {
                        "ns" : "francs.test_1",
                        "count" : 15650,
                        "size" : 563424,
                        "avgObjSize" : 36.00153354632588,
                        "storageSize" : 2793472,
                        "numExtents" : 5,
                        "nindexes" : 2,
                        "lastExtentSize" : 2097152,
                        "paddingFactor" : 1,
                        "systemFlags" : 1,
                        "userFlags" : 0,
                        "totalIndexSize" : 1005648,
                        "indexSizes" : {
                                "_id_" : 539616,
                                "id_1" : 466032
                        },
                        "ok" : 1
                },
                "shard0002" : {
                        "ns" : "francs.test_1",
                        "count" : 32386,
                        "size" : 1165920,
                        "avgObjSize" : 36.000741060952265,
                        "storageSize" : 2793472,
                        "numExtents" : 5,
                        "nindexes" : 2,
                        "lastExtentSize" : 2097152,
                        "paddingFactor" : 1,
                        "systemFlags" : 1,
                        "userFlags" : 0,
                        "totalIndexSize" : 2003120,
                        "indexSizes" : {
                                "_id_" : 1079232,
                                "id_1" : 923888
                        },
                        "ok" : 1
                }
        },
        "ok" : 1
}

     备注:数据已分散到节点 shard3 了,这些工作是一个名为 Balancing 的进程完成的,

         如果数据比较大,这步花的时间越长,这里不详细介绍。