mongodb shard zones

mongo shard zones

准备工作

系统域名配置

[root@localhost shd]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6192.168.3.234 cfg1 
192.168.3.235 cfg2 
192.168.3.236 cfg3 192.168.3.234 shd1
192.168.3.235 shd2 
192.168.3.236 shd3 

相关目录

 mkdir -p datadir/shard0000mkdir -p logdir/shard0000touch logdir/shard0000/shard0000.log mkdir -p datadir/shard0001mkdir -p logdir/shard0001touch logdir/shard0001/shard0001.logmkdir -p datadir/shard0002mkdir -p logdir/shard0002touch logdir/shard0002/shard0002.log

配置文件

shard0000.cnf
[root@localhost shd]# cat shard0000.cnf 
port=27020dbpath=/usr/local/mongodb/datadir/shard0000/logpath=/usr/local/mongodb/logdir/shard0000/shard0000.logfork=truelogappend=truebind_ip=localhost,shd3replSet=shard0000directoryperdb=trueshardsvr=1
shard0001.cnf
[root@localhost shd]# cat shard0001.cnf 
port=27021dbpath=/usr/local/mongodb/datadir/shard0001/logpath=/usr/local/mongodb/logdir/shard0001/shard0001.logfork=truelogappend=truebind_ip=localhost,shd3replSet=shard0001directoryperdb=trueshardsvr=1
shard0002.cnf
[root@localhost shd]# cat shard0002.cnf 
port=27022dbpath=/usr/local/mongodb/datadir/shard0002/logpath=/usr/local/mongodb/logdir/shard0002/shard0002.logfork=truelogappend=truebind_ip=localhost,shd3replSet=shard0002directoryperdb=trueshardsvr=1

配置shard replica set

shard0000
mongo -port 27020
rs.initiate({_id : "shard0000",members: [{ _id : 0, host : "shd1:27020" },{ _id : 1, host : "shd2:27020" },{ _id : 2, host : "shd3:27020" }]}
)
shard0001
mongo -port 27021
rs.initiate({_id : "shard0001",members: [{ _id : 0, host : "shd1:27021" },{ _id : 1, host : "shd2:27021" },{ _id : 2, host : "shd3:27021" }]}
)
shard0002
mongo -port 27022
rs.initiate({_id : "shard0002",members: [{ _id : 0, host : "shd1:27022" },{ _id : 1, host : "shd2:27022" },{ _id : 2, host : "shd3:27022" }]}
)

配制shard

mongo -port 27019
mongos> sh.addShard( "shard0000/shd1:27020,shd2:27020,shd3:27020")
{"shardAdded" : "shard0000","ok" : 1,"operationTime" : Timestamp(1604646164, 5),"$clusterTime" : {"clusterTime" : Timestamp(1604646164, 5),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> 
mongos> sh.addShard( "shard0001/shd1:27021,shd2:27021,shd3:27021")
{"shardAdded" : "shard0001","ok" : 1,"operationTime" : Timestamp(1604646275, 7),"$clusterTime" : {"clusterTime" : Timestamp(1604646275, 7),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> 
mongos> 
mongos> sh.addShard( "shard0002/shd1:27022,shd2:27022,shd3:27022")
{"shardAdded" : "shard0002","ok" : 1,"operationTime" : Timestamp(1604646370, 25),"$clusterTime" : {"clusterTime" : Timestamp(1604646370, 25),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
use config
mongos> db.shards.find()
{ "_id" : "repshd", "host" : "repshd/shd1:27018,shd2:27018,shd3:27018", "state" : 1 }
{ "_id" : "shard0000", "host" : "shard0000/shd1:27020,shd2:27020,shd3:27020", "state" : 1 }
{ "_id" : "shard0001", "host" : "shard0001/shd1:27021,shd2:27021,shd3:27021", "state" : 1 }
{ "_id" : "shard0002", "host" : "shard0002/shd1:27022,shd2:27022,shd3:27022", "state" : 1 }
mongos> 
mongos> 
mongos> 

shard zones

增加zone

mongos> sh.addShardToZone("shard0000", "NYC")
{"ok" : 1,"operationTime" : Timestamp(1604646386, 4),"$clusterTime" : {"clusterTime" : Timestamp(1604646386, 4),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> sh.addShardToZone("shard0001", "NYC")
{"ok" : 1,"operationTime" : Timestamp(1604646386, 5),"$clusterTime" : {"clusterTime" : Timestamp(1604646386, 5),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> sh.addShardToZone("shard0002", "SFO")
{"ok" : 1,"operationTime" : Timestamp(1604646386, 6),"$clusterTime" : {"clusterTime" : Timestamp(1604646386, 6),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> sh.addShardToZone("shard0002", "NRT")
{"ok" : 1,"operationTime" : Timestamp(1604646387, 1),"$clusterTime" : {"clusterTime" : Timestamp(1604646387, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}

验证zone

mongos> 
mongos> db.shards.find()
{ "_id" : "repshd", "host" : "repshd/shd1:27018,shd2:27018,shd3:27018", "state" : 1 }
{ "_id" : "shard0000", "host" : "shard0000/shd1:27020,shd2:27020,shd3:27020", "state" : 1, "tags" : [ "NYC" ] }
{ "_id" : "shard0001", "host" : "shard0001/shd1:27021,shd2:27021,shd3:27021", "state" : 1, "tags" : [ "NYC" ] }
{ "_id" : "shard0002", "host" : "shard0002/shd1:27022,shd2:27022,shd3:27022", "state" : 1, "tags" : [ "SFO", "NRT" ] }
mongos> 
mongos> 

指定zone shard key值范围

mongos> sh.updateZoneKeyRange("records.users", { zipcode: "10001" }, { zipcode: "10281" }, "NYC")
35" }, "SFO"){"ok" : 1,"operationTime" : Timestamp(1604648215, 1),"$clusterTime" : {"clusterTime" : Timestamp(1604648215, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> sh.updateZoneKeyRange("records.users", { zipcode: "11201" }, { zipcode: "11240" }, "NYC")
{"ok" : 1,"operationTime" : Timestamp(1604648215, 2),"$clusterTime" : {"clusterTime" : Timestamp(1604648215, 2),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> sh.updateZoneKeyRange("records.users", { zipcode: "94102" }, { zipcode: "94135" }, "SFO")
{"ok" : 1,"operationTime" : Timestamp(1604648216, 1),"$clusterTime" : {"clusterTime" : Timestamp(1604648216, 1),"signature" : {"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),"keyId" : NumberLong(0)}}
}
mongos> 

查看shard 状态

mongos> 
mongos> sh.status()
--- Sharding Status --- sharding version: {"_id" : 1,"minCompatibleVersion" : 5,"currentVersion" : 6,"clusterId" : ObjectId("5fa3773217003a6da928bcf8")}shards:{  "_id" : "repshd",  "host" : "repshd/shd1:27018,shd2:27018,shd3:27018",  "state" : 1 }{  "_id" : "shard0000",  "host" : "shard0000/shd1:27020,shd2:27020,shd3:27020",  "state" : 1,  "tags" : [ "NYC" ] }{  "_id" : "shard0001",  "host" : "shard0001/shd1:27021,shd2:27021,shd3:27021",  "state" : 1,  "tags" : [ "NYC" ] }{  "_id" : "shard0002",  "host" : "shard0002/shd1:27022,shd2:27022,shd3:27022",  "state" : 1,  "tags" : [ "SFO", "NRT" ] }active mongoses:"4.2.7" : 1autosplit:Currently enabled: yesbalancer:Currently enabled:  yesCurrently running:  noFailed balancer rounds in last 5 attempts:  0Migration Results for the last 24 hours: 769 : Successdatabases:{  "_id" : "config",  "primary" : "config",  "partitioned" : true }config.system.sessionsshard key: { "_id" : 1 }unique: falsebalancing: truechunks:repshd	256shard0000	256shard0001	256shard0002	256too many chunks to print, use verbose if you want to force print{  "_id" : "inventory",  "primary" : "repshd",  "partitioned" : true,  "version" : {  "uuid" : UUID("6cedc66f-91f0-41da-8bf5-ed72279bbd0e"),  "lastMod" : 1 } }inventory.productsshard key: { "prod_id" : "hashed" }unique: falsebalancing: truechunks:repshd	1shard0000	1{ "prod_id" : { "$minKey" : 1 } } -->> { "prod_id" : NumberLong(0) } on : shard0000 Timestamp(2, 0) { "prod_id" : NumberLong(0) } -->> { "prod_id" : { "$maxKey" : 1 } } on : repshd Timestamp(2, 1) mongos> 
mongos> 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部