mongodb shard集羣擴容和收縮

集羣搭建參考http://my.oschina.net/u/1791060/blog/652600mongodb

在這個集羣的基礎上再加個shard-4 mongodb-shard-4:shell

192.168.10.91:10003數據庫

192.168.10.92:10003服務器

192.168.10.93:10003app

###shard集羣水平擴展 ####在上述三臺服務器上啓動shard4的mongodb實例 # mkdir -p /data/mongodb/shard4.net

# mongod --shardsvr --replSet shard4 --port 10003 --dbpath /data/mongodb/shard4 --oplogSize 100 --logpath /data/mongodb/shard4/shard4.log --logappend --forkcode

####創建shard關係blog

# mongo --port 10003
MongoDB shell version: 2.4.14
connecting to: 127.0.0.1:10003/test
> config={_id:'shard4',members:[{_id:0,host:'192.168.10.91:10003'},{_id:1,host:'192.168.10.92:10003'},{_id:2,host:'192.168.10.93:100023'}]}
{
        "_id" : "shard4",
        "members" : [
                {
                        "_id" : 0,
                        "host" : "192.168.10.91:10003"
                },
                {
                        "_id" : 1,
                        "host" : "192.168.10.92:10003"
                },
                {
                        "_id" : 2,
                        "host" : "192.168.10.93:10003"
                }
        ]
}
> rs.initiate(config)
{
        "info" : "Config now saved locally.  Should come online in about a minute.",
        "ok" : 1
}
> 
shard4:PRIMARY>

####mongos上添加shard4rem

]# mongo --port 30000
MongoDB shell version: 2.4.14
connecting to: 127.0.0.1:30000/test
mongos> use admin
switched to db admin
mongos> db.runCommand({addshard:'shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003'})
{ "shardAdded" : "shard4", "ok" : 1 }

####查看集羣狀態get

  • 查看集羣狀態
mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "version" : 3,
        "minCompatibleVersion" : 3,
        "currentVersion" : 4,
        "clusterId" : ObjectId("570394b40d1b67f7f38c434a")
}
  shards:
        {  "_id" : "shard1",  "host" : "shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000" }
        {  "_id" : "shard2",  "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }
        {  "_id" : "shard3",  "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }
        {  "_id" : "shard4",  "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard3" }
        {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard1" }
                testdb.table1
                        shard key: { "_id" : 1 }
                        chunks:
                                shard4  1
                                shard1  1
                                shard2  2
                                shard3  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0) 
                        { "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard1 Timestamp(5, 1) 
                        { "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1) 
                        { "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0) 
                        { "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1) 
                        { "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5)

###shard集羣收縮 ####縮減shard1

  • mongos上剔除shard1
mongos> db.runCommand({removeshard:"shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000"})
{
        "msg" : "draining started successfully",
        "state" : "started",
        "shard" : "shard1",
        "note" : "you need to drop or movePrimary these databases",
        "dbsToMove" : [
                "testdb"
        ],
        "ok" : 1
}
  • 查看shard1的狀態,顯示draining,正在移除
mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "version" : 3,
        "minCompatibleVersion" : 3,
        "currentVersion" : 4,
        "clusterId" : ObjectId("570394b40d1b67f7f38c434a")
}
  shards:
        {  "_id" : "shard1",  "draining" : true,  "host" : "shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000" }
        {  "_id" : "shard2",  "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }
        {  "_id" : "shard3",  "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }
        {  "_id" : "shard4",  "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard3" }
        {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard1" }
                testdb.table1
                        shard key: { "_id" : 1 }
                        chunks:
                                shard4  2
                                shard2  2
                                shard3  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0) 
                        { "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard4 Timestamp(6, 0) 
                        { "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1) 
                        { "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0) 
                        { "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1) 
                        { "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5)
  • 再次執行removed可查看進度,chunks爲0即爲完成,可是dbs須要執行movePrimary
mongos> db.runCommand({removeshard:"shard1/192.168.10.91:10000,192.168.10.92:10000,192.168.10.93:10000"})
{
        "msg" : "draining ongoing",
        "state" : "ongoing",
        "remaining" : {
                "chunks" : NumberLong(0),
                "dbs" : NumberLong(1)
        },
        "note" : "you need to drop or movePrimary these databases",
        "dbsToMove" : [
                "testdb"
        ],
        "ok" : 1
}
  • 移動數據庫 #可查看config.databases發現原來primary在shard1上,如今挪至shard2
mongos> db.runCommand({movePrimary:'testdb',to:'shard2'})
{
        "primary " : "shard2:shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001",
        "ok" : 1
}
mongos> db.runCommand({removeshard:'shard1'})
{
        "msg" : "removeshard completed successfully",
        "state" : "completed",
        "shard" : "shard1",
        "ok" : 1
}
  • 再次查看分片信息發現shard已移除
mongos> printShardingStatus()
--- Sharding Status --- 
  sharding version: {
        "_id" : 1,
        "version" : 3,
        "minCompatibleVersion" : 3,
        "currentVersion" : 4,
        "clusterId" : ObjectId("570394b40d1b67f7f38c434a")
}
  shards:
        {  "_id" : "shard2",  "host" : "shard2/192.168.10.91:10001,192.168.10.92:10001,192.168.10.93:10001" }
        {  "_id" : "shard3",  "host" : "shard3/192.168.10.91:10002,192.168.10.92:10002,192.168.10.93:10002" }
        {  "_id" : "shard4",  "host" : "shard4/192.168.10.91:10003,192.168.10.92:10003,192.168.10.93:10003" }
  databases:
        {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
        {  "_id" : "test",  "partitioned" : false,  "primary" : "shard3" }
        {  "_id" : "testdb",  "partitioned" : true,  "primary" : "shard2" }
                testdb.table1
                        shard key: { "_id" : 1 }
                        chunks:
                                shard4  2
                                shard2  2
                                shard3  2
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("57039cd208bca3c16785e081") } on : shard4 Timestamp(5, 0) 
                        { "_id" : ObjectId("57039cd208bca3c16785e081") } -->> { "_id" : ObjectId("57039d9708bca3c167860949") } on : shard4 Timestamp(6, 0) 
                        { "_id" : ObjectId("57039d9708bca3c167860949") } -->> { "_id" : ObjectId("57039d9e08bca3c167865f9e") } on : shard2 Timestamp(3, 1) 
                        { "_id" : ObjectId("57039d9e08bca3c167865f9e") } -->> { "_id" : ObjectId("57039da408bca3c16786aadb") } on : shard2 Timestamp(4, 0) 
                        { "_id" : ObjectId("57039da408bca3c16786aadb") } -->> { "_id" : ObjectId("57039da508bca3c16786f705") } on : shard3 Timestamp(4, 1) 
                        { "_id" : ObjectId("57039da508bca3c16786f705") } -->> { "_id" : { "$maxKey" : 1 } } on : shard3 Timestamp(3, 5) 
        {  "_id" : "testdb1",  "partitioned" : false,  "primary" : "shard2" }
相關文章
相關標籤/搜索