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.localdomain6
192.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/shard0000
mkdir -p logdir/shard0000
touch logdir/shard0000/shard0000.log
mkdir -p datadir/shard0001
mkdir -p logdir/shard0001
touch logdir/shard0001/shard0001.log
mkdir -p datadir/shard0002
mkdir -p logdir/shard0002
touch logdir/shard0002/shard0002.log
配置文件
shard0000.cnf
[root@localhost shd]# cat shard0000.cnf
port=27020
dbpath=/usr/local/mongodb/datadir/shard0000/
logpath=/usr/local/mongodb/logdir/shard0000/shard0000.log
fork=true
logappend=true
bind_ip=localhost,shd3
replSet=shard0000
directoryperdb=true
shardsvr=1
shard0001.cnf
[root@localhost shd]# cat shard0001.cnf
port=27021
dbpath=/usr/local/mongodb/datadir/shard0001/
logpath=/usr/local/mongodb/logdir/shard0001/shard0001.log
fork=true
logappend=true
bind_ip=localhost,shd3
replSet=shard0001
directoryperdb=true
shardsvr=1
shard0002.cnf
[root@localhost shd]# cat shard0002.cnf
port=27022
dbpath=/usr/local/mongodb/datadir/shard0002/
logpath=/usr/local/mongodb/logdir/shard0002/shard0002.log
fork=true
logappend=true
bind_ip=localhost,shd3
replSet=shard0002
directoryperdb=true
shardsvr=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" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
769 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
repshd 256
shard0000 256
shard0001 256
shard0002 256
too 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.products
shard key: { "prod_id" : "hashed" }
unique: false
balancing: true
chunks:
repshd 1
shard0000 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>