MongoDB基础之十 shared分片

时间:2021-09-14 08:19:44

水平分片实例分布图:

MongoDB基础之十 shared分片

mongodb sharding 服务器架构

 

MongoDB基础之十 shared分片

1. 添加mongdb两个shared实例

 # mkdir -p /home/m17 //home/m18 /home/m20 /home/mlog

 ./bin/mongod --dbpath /home/m17 --logpath /home/mlog/m17.log --fork --port  --smallfiles
./bin/mongod --dbpath /home/m18 --logpath /home/mlog/m18.log --fork --port --smallfiles

2.配置 configdb

# ./bin/mongod --dbpath /home/m20 --logpath /home/mlog/m20.log --fork --port  --configsvr 

3. 配置路由

 # ./bin/mongos --logpath /home/mlog/m30.log --port  --configdb 127.0.0.1: --fork

4. 定制分片信息

   4.1 连接到 configDB  

 # ./bin/mongo --port
MongoDB shell version: 2.4.
connecting to: 127.0.0.1:/test

4.2  添加分片实例以及需要分片的表

 mongos> sh.addShard('127.0.0.1:27018')
{ "shardAdded" : "shard0000", "ok" : }
mongos> sh.addShard('127.0.0.1:27017')
{ "shardAdded" : "shard0001", "ok" : }
mongos> sh.enableSharding('shop');
{ "ok" : } mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : ,
"version" : ,
"minCompatibleVersion" : ,
"currentVersion" : ,
"clusterId" : ObjectId("5746614b92e27268fdc306b4")
}
shards:
{ "_id" : "shard0000", "host" : "127.0.0.1:27018" }
{ "_id" : "shard0001", "host" : "127.0.0.1:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }
{ "_id" : "shop", "partitioned" : true, "primary" : "shard0000" }

4.3 配置表分片(分片键为 goods_id)

 mongos> sh.shardCollection('shop.goods',{goods_id:});
{ "collectionsharded" : "shop.goods", "ok" : }
mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : ,
"version" : ,
"minCompatibleVersion" : ,
"currentVersion" : ,
"clusterId" : ObjectId("5746614b92e27268fdc306b4")
}
shards:
{ "_id" : "shard0000", "host" : "127.0.0.1:27018" }
{ "_id" : "shard0001", "host" : "127.0.0.1:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }
{ "_id" : "shop", "partitioned" : true, "primary" : "shard0000" }
shop.goods
shard key: { "goods_id" : }
chunks:
shard0000
{ "goods_id" : { "$minKey" : } } -->> { "goods_id" : { "$maxKey" : } } on : shard0000 Timestamp(, ) 修改分片大小
mongos> use config
switched to db config
mongos> db.settings.find();
{ "_id" : "chunksize", "value" : }
mongos> db.settings.save({_id:'chunksize',"value":});

注:
mongodb不是从单篇文档的级别,绝对平均的散落在各个片上, 而是N篇文档,形成一个块"chunk",优先放在某个片上,当这片上的chunk,比另一个片的chunk,区别比较大时, (>=3) ,会把本片上的chunk,移到另一个片上, 以chunk为单位,维护片之间的数据均衡

问: 为什么插入了10万条数据,才2个chunk?
答: 说明chunk比较大(默认是64M)
在config数据库中,修改chunksize的值.

问: 既然优先往某个片上插入,当chunk失衡时,再移动chunk,
自然,随着数据的增多,shard的实例之间,有chunk来回移动的现象,这将带来什么问题?
答: 服务器之间IO的增加,

接上问: 能否我定义一个规则, 某N条数据形成1个块,预告分配M个chunk,
M个chunk预告分配在不同片上.
以后的数据直接入各自预分配好的chunk,不再来回移动?

答: 能, 手动预先分片!

5.预先分片

  5.1 以user表为例

 mongos>sh.shardCollection('shop.user',{userid:}); 

  5.2 预先在1K 2K...40K这样的界限切好chunk(虽然chunk是空的), 这些chunk将会均匀移动到各片上

 mongos> for(var i=;i<=;i++) { sh.splitAt('shop.user',{userid:i*}) } .

 

  5.3 查看状态 发现分片已经均匀的分布在两台实例上了

 mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : ,
"version" : ,
"minCompatibleVersion" : ,
"currentVersion" : ,
"clusterId" : ObjectId("5746614b92e27268fdc306b4")
}
shards:
{ "_id" : "shard0000", "host" : "127.0.0.1:27018" }
{ "_id" : "shard0001", "host" : "127.0.0.1:27017" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : false, "primary" : "shard0001" }
{ "_id" : "shop", "partitioned" : true, "primary" : "shard0000" }
shop.goods
shard key: { "goods_id" : }
chunks:
shard0000
{ "goods_id" : { "$minKey" : } } -->> { "goods_id" : { "$maxKey" : } } on : shard0000 Timestamp(, )
shop.user
shard key: { "userid" : }
chunks:
25 shard0001 20 -- 实例1 20个分片
26 shard0000 20 -- 实例2 20个分片

too many chunks to print, use verbose if you want to force print

5.4 通过mongos添加user数据. 数据会添加到预先分配好的chunk上, chunk就不会来回移动了.

 for(var i=;i<=;i++) {db.user.insert({userid:i,username:'hello world '+i});}; 

5.5 查看数据分片以及数据分布情况

 --节点1
> db.user.count(); --节点2
> db.user.count();