MongoDB 分片管理(不定时更新)

背景:

      通过上一篇的 MongoDB 分片的原理、搭建、应用 大致了解了MongoDB分片的安装和一些基本的使用情况,现在来说明下如何管理和优化MongoDB分片的使用。

知识点:

1) 分片的配置和查看

① 添加分片:sh.addShard("IP:Port") 

mongos> sh.addShard("192.168.200.A:40000") #添加分片
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.200.B:40000") #添加分片
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.addShard("192.168.200.C:40000") #添加分片
{ "shardAdded" : "shard0002", "ok" : 1 }

② 开启分片功能:sh.enableSharding("库名")、sh.shardCollection("库名.集合名",{"key":1})

mongos> sh.enableSharding("dba")
{ "ok" : 1 }

mongos> sh.shardCollection("dba.account",{"name":1})
{ "collectionsharded" : "dba.account", "ok" : 1 }

③ 查看分片状态:sh.status()

mongos> sh.status()
--- Sharding Status ---...
  shards:
    {  "_id" : "shard0000",  "host" : "192.168.200.A:40000" }
    {  "_id" : "shard0001",  "host" : "192.168.200.B:40000" }
    {  "_id" : "shard0002",  "host" : "192.168.200.C:40000" }
...
  databases:  #库
    {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
    {  "_id" : "test",  "partitioned" : false,  "primary" : "shard0000" }
    {  "_id" : "dba",  "partitioned" : true,  "primary" : "shard0000" } #允许分片
        dba.account   #dba下的分片集合
            shard key: { "name" : 1 }                             
            chunks:   #块信息
                shard0000    1
            { "name" : { "$minKey" : 1 } } -->> { "name" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)  #块里数据的范围

2) 配置信息说明,config库的介绍。

① 分片信息:config.shards  

mongos> db.shards.find()
{ "_id" : "shard0000", "host" : "192.168.200.51:40000" }
{ "_id" : "shard0001", "host" : "192.168.200.52:40000" }
{ "_id" : "shard0002", "host" : "192.168.200.53:40000" }
{ "_id" : "mablevi", "host" : "mablevi/192.168.200.53:50000,192.168.200.53:50001,192.168.200.53:50002" } #副本级分片,不需要把所有的节点写出

② 分片中所有数据库信息:config.databases

mongos> db.databases.find()
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }
{ "_id" : "dba", "partitioned" : true, "primary" : "shard0000" }  #分片
{ "_id" : "abc", "partitioned" : true, "primary" : "shard0000" }  #分片

③ 分片集合信息:config.collections

mongos> db.collections.findOne()
{
    "_id" : "dba.account",
    "lastmod" : ISODate("2015-07-14T15:12:29.706Z"),
    "dropped" : false,
    "key" : {          #片键
        "name" : 1
    },
    "unique" : false,  #片键是否唯一
    "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
}

④ mongos路由的信息:config.mongs 。可以查看所有mongos的状态

mongos> db.mongos.findOne()
{
    "_id" : "mongo2:30000",
    "ping" : ISODate("2015-07-27T03:13:06.178Z"),
    "up" : 323671,      #活着时间
    "waiting" : true, 
    "mongoVersion" : "3.0.4" #版本
}

⑤ 均衡器锁的信息:config.locks,记录所有集群范围的锁,可得知哪个mongos是均衡器。

mongos> db.locks.findOne()
{
    "_id" : "balancer",   #均衡器
    "state" : 1,          #0非活动状态、1尝试得到锁,但还没得到,2表示正在进行均衡
    "who" : "mongo1:30000:1436888525:1804289383:Balancer:846930886", #哪个mongos当均衡器
    "ts" : ObjectId("55b5a2d5fdd9a605a039f951"),
    "process" : "mongo1:30000:1436888525:1804289383",
    "when" : ISODate("2015-07-27T03:17:41.159Z"),
    "why" : "doing balance round" #均衡导致锁
}

⑥ 记录所有块的信息:config.chunks,也可以通过sh.status()查看

mongos> db.chunks.find().pretty()
{
    "_id" : "dba.account-name_MinKey",   #标识符
    "lastmod" : Timestamp(2, 0),         #块的版本
    "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77"), #块的版本
    "ns" : "dba.account",    #集合名
    "min" : {                #数据范围
        "name" : { "$minKey" : 1 }
    },
    "max" : {
        "name" : "9XXqCaBhfhPIXLq"
    },
    "shard" : "mablevi"      #所在分片
}
{
    "_id" : "dba.account-name_\"9XXqCaBhfhPIXLq\"",
    "lastmod" : Timestamp(4, 0),
    "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77"),
    "ns" : "dba.account",
    "min" : {
        "name" : "9XXqCaBhfhPIXLq"
    },
    "max" : {
        "name" : "RWINvgjYYQmbZds"
    },
    "shard" : "shard0002"
}

⑦ 记录所有的分片操作:config.changelog,拆分、迁移

拆分:

{
    "_id" : "mongo1-2015-07-14T15:12:40-55a526e8f0432675a473009c",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:40.375Z"),
    "what" : "multi-split",  #拆分
    "ns" : "dba.account",
    "details" : {            #拆分先后的信息
        "before" : {
            "min" : {        #拆分前范围
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            }
        },
        "number" : 1,   #第一个块
        "of" : 3,       #拆分成3个块
        "chunk" : {
            "min" : {
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : "9XXqCaBhfhPIXLq"
            },
            "lastmod" : Timestamp(1, 1), #版本号
            "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
        }
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:40-55a526e8f0432675a473009d",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:40.378Z"),
    "what" : "multi-split",
    "ns" : "dba.account",
    "details" : {
        "before" : {
            "min" : {
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            }
        },
        "number" : 2,
        "of" : 3,
        "chunk" : {
            "min" : {
                "name" : "9XXqCaBhfhPIXLq"
            },
            "max" : {
                "name" : "okmjUUZuuKgftDC"
            },
            "lastmod" : Timestamp(1, 2), #版本号
            "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
        }
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:40-55a526e8f0432675a473009e",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:40.381Z"),
    "what" : "multi-split",
    "ns" : "dba.account",
    "details" : {
        "before" : {
            "min" : {
                "name" : { "$minKey" : 1 }
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            }
        },
        "number" : 3,
        "of" : 3,
        "chunk" : {
            "min" : {
                "name" : "okmjUUZuuKgftDC"
            },
            "max" : {
                "name" : { "$maxKey" : 1 }
            },
            "lastmod" : Timestamp(1, 3),  #版本号
            "lastmodEpoch" : ObjectId("55a526dd511d36716224fb77")
        }
    }
}

迁移:4个文档

{
    "_id" : "mongo1-2015-07-14T15:12:41-55a526e9f0432675a47300a1",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:41.406Z"),
    "what" : "moveChunk.start",  #迁移开始
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },
        "from" : "shard0000",
        "to" : "mablevi"
    }
}
{
    "_id" : "mongo3-2015-07-14T15:12:42-55a526ead5bee637c12aadd4",
    "server" : "mongo3",
    "clientAddr" : ":27017",
    "time" : ISODate("2015-07-14T15:12:42.419Z"),
    "what" : "moveChunk.to",   #from分片
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },
        "step 1 of 5" : 327,  #耗时,单位毫秒。检查命令参数
        "step 2 of 5" : 301,  #申请分布锁
        "step 3 of 5" : 1,    #连接到to分片
        "step 4 of 5" : 0,    #数据复制,每个分片都是直接和另一个分片、配置服务器直接连接
        "step 5 of 5" : 407,  #和to分片、配置服务器确认是否完成
        "note" : "success"
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:42-55a526eaf0432675a47300a2",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:42.854Z"),
    "what" : "moveChunk.commit",  #提交
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },
        "from" : "shard0000",
        "to" : "mablevi",
        "cloned" : NumberLong(1),
        "clonedBytes" : NumberLong(94),
        "catchup" : NumberLong(0),
        "steady" : NumberLong(0)
    }
}
{
    "_id" : "mongo1-2015-07-14T15:12:43-55a526ebf0432675a47300a3",
    "server" : "mongo1",
    "clientAddr" : "192.168.200.52:53257",
    "time" : ISODate("2015-07-14T15:12:43.258Z"),
    "what" : "moveChunk.from",  #to分片
    "ns" : "dba.account",
    "details" : {
        "min" : {
            "name" : { "$minKey" : 1 }
        },
        "max" : {
            "name" : "9XXqCaBhfhPIXLq"
        },    #步骤的时间为毫秒
        "step 1 of 6" : 0,      #迁移索引 
        "step 2 of 6" : 613,    #删除块内的数据,清理数据残余
        "step 3 of 6" : 2,      #文档复制到to分片
        "step 4 of 6" : 1029,   #复制期间,在to分片上执行操作
        "step 5 of 6" : 415,    #等待to分片将新迁移过来的数据复制到集群
        "step 6 of 6" : 0,      #修改元数据完成迁移
"to" : "mablevi",
        "from" : "shard0000",
        "note" : "success"
    }
}

⑧ 分片标签:config.tags,sh.addShardTag

mongos> db.tags.findOne()
{
    "_id" : {
        "ns" : "abc.number",
        "min" : {
            "num" : 0
        }
    },
    "ns" : "abc.number",
    "min" : {
        "num" : 0
    },
    "max" : {
        "num" : 20
    },
    "tag" : "AAA"
}

⑨ 分片设置:config.settings,设置分片块的大小和开启关闭均衡器

mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 32 }
{ "_id" : "balancer", "stopped" : false }

⑩ 网络连接数: db.adminCommand({"connPoolStats":1})

mongos> db.adminCommand({"connPoolStats":1})
{
    "hosts" : {
        "192.168.200.51:20000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:20000::30" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:20000,192.168.200.51:21000,192.168.200.51:22000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:20000,192.168.200.51:21000,192.168.200.51:22000::30" : {
            "available" : 3,
            "created" : 422
        },
        "192.168.200.51:21000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:21000::30" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:22000::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:22000::30" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.51:40000::0" : {
            "available" : 2,
            "created" : 2
        },
        "192.168.200.52:40000::0" : {
            "available" : 1,
            "created" : 4
        },
        "192.168.200.53:40000::0" : {
            "available" : 1,
            "created" : 2
        },
        "192.168.200.53:50000::5" : {
            "available" : 1,
            "created" : 2
        },
        "192.168.200.53:50001::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.53:50001::5" : {
            "available" : 1,
            "created" : 2
        },
        "192.168.200.53:50002::0" : {
            "available" : 1,
            "created" : 1
        },
        "192.168.200.53:50002::5" : {
            "available" : 1,
            "created" : 2
        },
        "mablevi/192.168.200.53:50000,192.168.200.53:50001,192.168.200.53:50002::0" : {
            "available" : 2,
            "created" : 3
        }
    },
    "replicaSets" : {
        "mablevi" : {
            "hosts" : [
                {
                    "addr" : "192.168.200.53:50000",
                    "ok" : true,
                    "ismaster" : true,
                    "hidden" : false,
                    "secondary" : false,
                    "pingTimeMillis" : 1
                },
                {
                    "addr" : "192.168.200.53:50001",
                    "ok" : true,
                    "ismaster" : false,
                    "hidden" : false,
                    "secondary" : true,
                    "pingTimeMillis" : 1
                },
                {
                    "addr" : "192.168.200.53:50002",
                    "ok" : true,
                    "ismaster" : false,
                    "hidden" : false,
                    "secondary" : true,
                    "pingTimeMillis" : 1
                }
            ]
        }
    },
    "createdByType" : {
        "master" : 22,
        "set" : 3,
        "sync" : 423
    },
    "totalAvailable" : 21,   #总的可用连接
    "totalCreated" : 448,   #总创建的连接
    "numDBClientConnection" : 40,
    "numAScopedConnection" : 1,
    "ok" : 1
}
View Code

2)拆分块管理

mongos记录每个块中插入多少数据,一旦达到某个阈值,就会检查是否需要拆分块,需要则更新配置服务器上这个块的元信息。具体过程:

① 客户端发起请求,mongos检查当前块的阈值点。

② 达到拆分的阈值点,mongos向分片发起一个拆分请求。

③ 分片计算拆分点,将信息发回给mongos。

④ mongos选择一个拆分点,将这些信息发给配置服务器。

阈值点和块大小相关,块大小可以直接设置,设置块大小:块大小不存在限制,因为当mongos不断重启,计数器可能永远无法达到阈值点。

mongos> db.settings.find({"_id":"chunksize"})
{ "_id" : "chunksize", "value" : 32 }
mongos> db.settings.save({"_id":"chunksize","value":64})   #块大小设置成64M
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongos> db.settings.find({"_id":"chunksize"})
{ "_id" : "chunksize", "value" : 64 }

也可以对块进行手动拆分块sh.splitAt('库.集合',{"name":"块值"})

mongos> db.chunks.findOne({"_id" : "abc.account-name_\"AIj2eqA3JsRxUxj\"",})
{
    "_id" : "abc.account-name_\"AIj2eqA3JsRxUxj\"",
    "lastmod" : Timestamp(13, 3),
    "lastmodEpoch" : ObjectId("55a52ff1fdd9a605a0371327"),
    "ns" : "abc.account",
    "min" : {
        "name" : "AIj2eqA3JsRxUxj"
    },
    "max" : {
        "name" : "DM9BnUXfNIgPA15"
    },
    "shard" : "shard0000"
}
mongos> sh.splitAt('abc.account',{"name":"AIj7eqA3JsRxUxj"})
{ "ok" : 1 }
mongos> db.chunks.findOne({"_id" : "abc.account-name_\"AIj2eqA3JsRxUxj\"",})
{
    "_id" : "abc.account-name_\"AIj2eqA3JsRxUxj\"",
    "lastmod" : Timestamp(14, 2),
    "lastmodEpoch" : ObjectId("55a52ff1fdd9a605a0371327"),
    "ns" : "abc.account",
    "min" : {
        "name" : "AIj2eqA3JsRxUxj"
    },
    "max" : {
        "name" : "AIj7eqA3JsRxUxj"   #被成功拆分
    },
    "shard" : "shard0000"
}

上面看到把块 abc.account-name_\"AIj2eqA3JsRxUxj\" 拆分成:

AIj2eqA3JsRxUxj到DM9BnUXfNIgPA15 拆分成 AIj2eqA3JsRxUxj到AIj7eqA3JsRxUxj到DM9BnUXfNIgPA15

具体的块信息和操作信息见config库里的chunks和changlogs集合。

3)均衡器:均衡器是使用块数量的多少,而不是数据的大小进行均衡

均衡器负责数据的迁移,会周期性的检查分片是否存在不均衡,如果存在则会进行块的迁移,不会影响到mongos正常的操作。均衡器进行均衡的条件是块数量的多少,而不是块大小,比如A片有几个较大的块,B片有很多较小的块,则均衡器会把B的分片迁移至A片。

关闭/开启自动均衡器:

mongos> sh.setBalancerState(false)   #关闭均衡器
mongos> db.settings.find({"_id" : "balancer"})
{ "_id" : "balancer", "stopped" : true}

mongos> sh.setBalancerState(true)    #开启均衡器
mongos> db.settings.find({"_id" : "balancer"})
{ "_id" : "balancer", "stopped" : false}

mongos> sh.stopBalancer()             #关闭均衡器
Waiting for active hosts...
Waiting for active host mongo2:30000 to recognize new settings... (ping : Mon Jul 27 2015 16:08:33 GMT+0800 (CST))
Waiting for the balancer lock...
Waiting again for active hosts after balancer is off...
mongos> db.settings.find({"_id" : "balancer"})
{ "_id" : "balancer", "stopped" : true}
mongos> sh.startBalancer()               #开启均衡器
mongos> db.settings.find({"_id" : "balancer"})
{ "_id" : "balancer", "stopped" : false}

查看均衡器的锁:

mongos> use config

mongos> db.locks.find( { _id : "balancer" } ).pretty()
{
    "_id" : "balancer",
    "state" : 0, #0:非活动状态,1:正在获取,但还没有执行,2:正在均衡,迁移,有锁
    "who" : "mongo3c:30000:1439446424:1804289383:Balancer:846930886",
    "ts" : ObjectId("55ccb2e71778d3d54573d23d"),
    "process" : "mongo3c:30000:1439446424:1804289383",
    "when" : ISODate("2015-08-13T15:08:23.539Z"),
    "why" : "doing balance round"
}

指定均衡器的工作时间activeWindow

mongos> db.settings.update({"_id":"balancer"},{"$set":{"activeWindow":{"start":"07:00:00","stop":"03:00:00"}}},true) 
WriteResult({
"nMatched" : 1, "nUpserted" : 0, "nModified" : 1 }) mongos> db.settings.findOne({"_id":"balancer"}) { "_id" : "balancer", "stopped" : false, "activeWindow" : { #凌晨7点到第二天凌晨3点开启均衡器功能。 "start" : "07:00:00", "stop" : "03:00:00" } }

移除:

db.settings.update({ _id : "balancer" }, { $unset : { activeWindow : true } })

指定集合不进行/进行迁移:

sh.disableBalancing("students.grades")  #关闭迁移
sh.enableBalancing("students.grades")   #开启迁移

手动迁移数据块:sh.moveChunk(),要是被迁移的数据块比较大则迁移比较慢,手动操作需要关闭均衡器

mongos> sh.moveChunk('abc.account',{"name":"W8evOO2dndQrmTy"},"mablevi") #把name为W8ev...(左下限值)的块迁移到mablevi分片里
{ "millis" : 19303, "ok" : 1 }

效果:

            chunks:
                mablevi    3
                shard0000    4
                shard0001    3
                shard0002    3


after:

            chunks:
                mablevi    4
                shard0000    3
                shard0001    3
                shard0002    3
View Code

手动拆分+迁移数据:要是被迁移的数据快大小超出了范围(默认块大小),则不能被迁移:

chunk to big too move ...

方法一:设置块大小,大于被迁移的块。方法二:先手动拆分块,再迁移。

4)片键 :(参考权威指南第二版第15章)

片键一般有三种形式:自增、随机、基于位置(Tag)。

各自的优缺点:

自增(升序)片键:
缺:所有的写请求都只在一个分片上,插入的文档总出现找最大的块里。分片的写入性能得不到提升,使得均衡处理数据变的困难。
优:在查询的时候能提高性能,数据有序写入,顺序。

随机片键:
缺:查询效率不高,数据分布分散。
优:写入比较均衡,写入请求性能比较高。

基于位置(Tag):
通过sh.addShardTag把一个或则多个分片设置成一个tag,写入指定tag。

自增和随机分片好理解,就不做说明了,下面说明Tag片键的使用方法。

① 为分片指定tag:sh.addShardTag('shardName','tagName')

mongos> sh.addShardTag("mablevi","AAA")
mongos> sh.addShardTag("shard0000","BBB")

② 为tag指定规则:sh.addTagRange('库.集合',{minkey:num},{maxkey:num},tagName)

mongos> sh.addTagRange("abc.number",{"num":0},{"num":20},"AAA")
mongos> sh.addTagRange("abc.number",{"num":21},{"num":50},"BBB")

①和②的意思是把分片mablevi、shard000指定AAA、BBB标签。并且0到20的数据写到AAA标签里,21到50的数据写到BBB标签里。其他范围的数据也可以写进

③ 数据写入到指定的标签里,比如想把一些指定的数据写入到指定的分片中,则:

写入数据:
mongos> db.number.find()
{ "_id" : ObjectId("55b608d7e7baf9146fe15ac3"), "num" : 21, "age" : 21 }  #写到mablevi分片
{ "_id" : ObjectId("55b608d0e7baf9146fe15ac2"), "num" : 1, "age" : 11 }   #写到shard000分片

单独进入到各个分片的mongod中看到,要写入的数据已被成功指定到分片。

mongos> sh.status()
--- Sharding Status --- 
...
...
        abc.number
            shard key: { "num" : 1 }
            chunks:
                mablevi    1
                shard0000    2
            { "num" : { "$minKey" : 1 } } -->> { "num" : 0 } on : shard0000 Timestamp(2, 1)    #数据范围
            { "num" : 0 } -->> { "num" : 21 } on : mablevi Timestamp(3, 1)  #数据范围
            { "num" : 21 } -->> { "num" : { "$maxKey" : 1 } } on : shard0000 Timestamp(3, 0)   #数据范围
             tag: AAA  { "num" : 0 } -->> { "num" : 20 }
             tag: BBB  { "num" : 21 } -->> { "num" : 50 }

比如分片mablevi是在SSD硬盘上的分片,其余都是普通的硬盘,现在把数据都写入到SSD标签的分片,提高写的性能,注意:这并不会影响集合的分发迁移,数据仍会被均衡器迁移到其他分片:

mongos> sh.addShardTag("mablevi","high")   #把分片mablevi,添加到high标签里

mongos> sh.addTagRange('abc.ttt',{"age":MinKey},{"age":MaxKey},"high")  #片键指定MinKey和MaxKey,把所有数据都写入改分片

写入数据并查看:

mongos> sh.status()
...
  shards:
    {  "_id" : "mablevi",  "host" : "mablevi/192.168.200.53:50000,192.168.200.53:50001,192.168.200.53:50002",  "tags" : [ "AAA", "high" ] }  #分片的标签名
...
        abc.ttt
            shard key: { "age" : 1 }
            chunks:
                mablevi    1
            { "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } } on : mablevi Timestamp(2, 0) 
             tag: high  { "age" : { "$minKey" : 1 } } -->> { "age" : { "$maxKey" : 1 } }               

进入到mablevi分片里查看,数据都写入在该分片,后续数据继续写入到达阈值后则均衡器会进行数据迁移。要想把历史数据进行迁移,保证其他分片数据均衡,则只需要在上面的基础上把MinKey改成当前值即可:

mongos> sh.shardCollection('abc.xxx',{"_id":1})   #把自带的_id 设为片键
{ "collectionsharded" : "abc.xxx", "ok" : 1 }

mongos> sh.addShardTag('shard0001','ssd')         #设置标签

mongos> sh.addTagRange('abc.xxx',{"_id":ObjectId()},{"_id":MaxKey},"ssd")    #ObjectId()表示当前值

查看:

mongos> sh.status()
...
        abc.xxx
            shard key: { "_id" : 1 }
            chunks:
                shard0000    1
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0) 
             tag: ssd  { "_id" : ObjectId("55b6431f54858df435b6ba63") } -->> { "_id" : { "$maxKey" : 1 } }  #当前值到正无穷大

上面tag建立成功,数据也写到指定的分片中,现在需要把写入到ssd标签中的历史数据进行迁移,更新标签范围

config库下:
mongos> db.tags.findOne({ns:"abc.xyz"}).min    #查看最小值
{ "_id" : ObjectId("55b6463b54858df435b6ba68") }
mongos> db.tags.update({ns:"abc.xyz"},{"$set":{"min":ObjectId()}})   #把之前的最小值更新更当前值
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongos> db.tags.findOne({ns:"abc.xyz"}).min
ObjectId("55b647f654858df435b6ba6c")

更新完之后查看:

刷新:
mongos> db.adminCommand({"flushRouterConfig":1})
{ "flushed" : true, "ok" : 1 }
查看
mongos> sh.status()
--- Sharding Status --
        abc.xyz
            shard key: { "_id" : 1 }
            chunks:
                shard0000    1
                shard0002    1
            { "_id" : { "$minKey" : 1 } } -->> { "_id" : ObjectId("55b6463b54858df435b6ba68") } on : shard0000 Timestamp(2, 1) 
            { "_id" : ObjectId("55b6463b54858df435b6ba68") } -->> { "_id" : { "$maxKey" : 1 } } on : shard0002 Timestamp(2, 0) #历史数据被迁移到shard0002中
             tag: ssd  ObjectId("55b647f654858df435b6ba6c") -->> { "_id" : { "$maxKey" : 1 } }   #查看到块的范围已经被更新 

如果该模式下出现问题,一个SSD分片写入满足不了要求,则需要删除tag,重新设置。

④ 删除标签:sh.removeShardTag(),标签被删除之后,数据不会丢失,后续的读写不会因为没有tag而不成功,但数据不会被迁移到其他的分片。

mongos> sh.removeShardTag('mablevi','high')

删除标签后,由于标签范围还在:

tag: ssd  ObjectId("55b647f654858df435b6ba6c") -->> { "_id" : { "$maxKey" : 1 } } 

继续删除标签范围:sh.removeTagRange

3.0之前:进入到config库,删除tags集合上的和mablevi分片相关的信息。
3.0之后:sh.removeTagRange('abc.xyz',ObjectId("55b647f654858df435b6ba6c"),{ "_id" : { "$maxKey" : 1 },'ssd')

5)删除分片

db.runCommand({"removeshard":"mablevi"})   #删除mablevi分片

db.adminCommand({"movePrimary":"dba","to":"shard0001"})  #移动dba的主分片到shard0001.

6)查看分片成员:db.runCommand({ listshards : 1 })

mongos> db.runCommand({ listshards : 1 })
{
    "shards" : [
        {
            "_id" : "shard0000",
            "host" : "192.168.200.51:40000",
            "tags" : [
                "BBB"
            ]
        },
        {
            "_id" : "shard0001",
            "host" : "192.168.200.52:40000",
            "tags" : [ ]
        },
        {
            "_id" : "shard0002",
            "host" : "192.168.200.53:40000",
            "tags" : [ ]
        },
        {
            "_id" : "mablevi",
            "host" : "mablevi/192.168.200.53:50000,192.168.200.53:50001,192.168.200.53:50002",
            "tags" : [
                "AAA"
            ],
            "draining" : true   #还在迁移
        }
    ],
    "ok" : 1
}

注意:要是在mablevi分片上设置了标签,需要把标签删除,包括标签范围(在config.tags里remove),分片才能被删除掉。数据不会丢失,会被迁移到其他分片:下面表示删除分片成功

mongos> db.runCommand({"removeshard":"mablevi"})
{
    "msg" : "removeshard completed successfully",
    "state" : "completed",
    "shard" : "mablevi",
    "ok" : 1
}

7)查看各分片的状态:mongostat --discover 

root@mongo1:~# mongostat --discover -udba -pdba --authenticationDatabase admin --port=30000 
                insert query update delete getmore command flushes mapped  vsize   res faults qr|qw ar|aw netIn netOut conn set repl     time
localhost:30000     *0    *0     *0     *0       0     1|0       0        160.0M 39.0M      0   0|0   0|0   79b     6k    1      RTR 11:12:37

192.168.200.51:40000     *0    *0     *0     *0       0     1|0       0 320.0M 941.0M 209.0M      0   0|0   0|0   79b    10k   10          11:12:38
192.168.200.52:40000     *0    *0     *0     *0       0     1|0       0 448.0M   1.1G 192.0M      0   0|0   0|0   79b    10k    9          11:12:38
192.168.200.53:40000     *0    *0     *0     *0       0     1|0       0 448.0M   1.1G 154.0M      0   0|0   0|0   79b    10k    9          11:12:38
     localhost:30000     *0     1     *0     *0       0     1|0       0        160.0M  39.0M      0   0|0   0|0  126b     6k    1      RTR 11:12:38

8)

 

posted @ 2015-07-28 10:46  jyzhou  阅读(14298)  评论(13编辑  收藏  举报