Shard部署
准备测试环境
为准备数据文件夹
Cd /home/tiansign/fanr/mongodb/Shard
mkdir configdb1 configdb2 configdb3
mkdir shard1 shard2 shard3
mkdir mongos
准备配置文件
为Config准备配置文件
dbpath=/home/tiansign/fanr/mongodb/Shard/configdb1
fork = true
logpath=/home/tiansign/fanr/mongodb/Shard/configdb1/config.log
为Shard准备配置文件
dbpath=/home/tiansign/fanr/mongodb/Shard/shard1
fork = true
logpath=/home/tiansign/fanr/mongodb/Shard/shard1/shard.log
port=27018
dbpath=/home/tiansign/fanr/mongodb/Shard/shard2
fork = true
logpath=/home/tiansign/fanr/mongodb/Shard/shard2/shard.log
port=27021
dbpath=/home/tiansign/fanr/mongodb/Shard/shard3
fork = true
logpath=/home/tiansign/fanr/mongodb/Shard/shard3/shard.log
port=27020
启动Config服务
mongod --config /home/tiansign/fanr/mongodb/Shard/configdb1/config.cfg --configsvr
启动Mongos服务
#mongos -f /home/tiansign/fanr/mongodb/Shard/mongos/mongos.cfg
mongos –configdb 127.0.0.1:27019
启动Shard的Mongod
mongod --config /home/tiansign/fanr/mongodb/Shard/shard1/shard.cfg
mongod --config /home/tiansign/fanr/mongodb/Shard/shard2/shard.cfg
mongod --config /home/tiansign/fanr/mongodb/Shard/shard3/shard.cfg
加入Shard服务
Mongo
sh.addShard("127.0.0.1:27018")
sh.addShard("127.0.0.1:27021")
sh.addShard("127.0.0.1:27020")
db.settings.save( { _id:"chunksize", value: 1} )
配置Shard Database
sh.enableSharding("test")
配置Shard Collection
sh.shardCollection("test.joker", {jokes:1});
插入测试数据
for (var i=0; i<100000; i++) { db.joker.insert({jokes: Math.random(), count: i, time: new Date()}); }
查看Chunk分布情况
use config
db.chunks.find({ns: "test.joker"}, {shard:1, _id:0}).sort({shard:1})
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0001" }
{ "shard" : "shard0001" }
{ "shard" : "shard0001" }
{ "shard" : "shard0002" }
{ "shard" : "shard0002" }
{ "shard" : "shard0002" }
Shard Tag的使用
先把Tag和Shard关联
sh.addShardTag("shard0000", "mr. j")
为Tag分配shard key
sh.addTagRange("test.joker", {jokes:MinKey}, {jokes:MaxKey}, "mr. j")
查看均衡器状态
db.locks.find( { _id : "balancer" } ).pretty()
查看Chunk分布情况
use config
db.chunks.find({ns: "test.joker"}, {shard:1, _id:0}).sort({shard:1})
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }
{ "shard" : "shard0000" }