目录
概念
-
分片动机 = 分布式水平扩展
-
最小分片 = 1路由服务 + 1配置服务(×3副本集) + 2分片服务(×3副本集)
搭建
mkdir sharding && cd sharding
分片服务
mkdir ./rs-a-1 ./rs-a-2 ./rs-a-3
mongod --shardsvr --replSet shard-a --dbpath ./rs-a-1 --port 30000 --fork --logpath=rs-a-1.log
mongod --shardsvr --replSet shard-a --dbpath ./rs-a-2 --port 30001 --fork --logpath=rs-a-2.log
mongod --shardsvr --replSet shard-a --dbpath ./rs-a-3 --port 30002 --fork --logpath=rs-a-3.log
mongo --port 30000
use admin
config = {
"_id": "shard-a",
"members": [
{"_id": 0, "host": "127.0.0.1:30000"},
{"_id": 1, "host": "127.0.0.1:30001"},
{"_id": 2, "host": "127.0.0.1:30002", arbiterOnly: true}
]
}
rs.initiate(config)
mkdir ./rs-b-1 ./rs-b-2 ./rs-b-3
mongod --shardsvr --replSet shard-b --dbpath ./rs-b-1 --port 40000 --fork --logpath=rs-b-1.log
mongod --shardsvr --replSet shard-b --dbpath ./rs-b-2 --port 40001 --fork --logpath=rs-b-2.log
mongod --shardsvr --replSet shard-b --dbpath ./rs-b-3 --port 40002 --fork --logpath=rs-b-3.log
mongo --port 40000
use admin
config = {
"_id": "shard-b",
"members": [
{"_id": 0, "host": "127.0.0.1:40000"},
{"_id": 1, "host": "127.0.0.1:40001"},
{"_id": 2, "host": "127.0.0.1:40002", arbiterOnly: true}
]
}
rs.initiate(config)
配置服务
mkdir ./config-1 ./config-2 ./config-3
mongod --configsvr --replSet conf --dbpath ./config-1 --port 50000 --fork --logpath=config-1.log
mongod --configsvr --replSet conf --dbpath ./config-2 --port 50001 --fork --logpath=config-2.log
mongod --configsvr --replSet conf --dbpath ./config-3 --port 50002 --fork --logpath=config-3.log
mongo --port 50000
use admin
config = {
"_id": "conf",
"members": [
{"_id": 0, "host": "127.0.0.1:50000"},
{"_id": 1, "host": "127.0.0.1:50001"},
{"_id": 2, "host": "127.0.0.1:50002"}
]
}
rs.initiate(config)
路由服务
mongos --configdb conf/127.0.0.1:50000,127.0.0.1:50001,127.0.0.1:50002 --port 60000 --fork --logpath=mongos.log
ps -ax | grep mongo
41885 ?? 0:18.11 mongod --shardsvr --replSet shard-a --dbpath ./rs-a-1 --port 30000 --fork --logpath=rs-a-1.log
41933 ?? 0:19.65 mongod --shardsvr --replSet shard-a --dbpath ./rs-a-2 --port 30001 --fork --logpath=rs-a-2.log
41946 ?? 0:12.03 mongod --shardsvr --replSet shard-a --dbpath ./rs-a-3 --port 30002 --fork --logpath=rs-a-3.log
42199 ?? 0:16.42 mongod --shardsvr --replSet shard-b --dbpath ./rs-b-1 --port 40000 --fork --logpath=rs-b-1.log
42213 ?? 0:18.02 mongod --shardsvr --replSet shard-b --dbpath ./rs-b-2 --port 40001 --fork --logpath=rs-b-2.log
42226 ?? 0:10.31 mongod --shardsvr --replSet shard-b --dbpath ./rs-b-3 --port 40002 --fork --logpath=rs-b-3.log
44110 ?? 0:01.35 mongod --configsvr --replSet config --dbpath ./config-1 --port 50000 --fork --logpath=config-1.log
44122 ?? 0:01.43 mongod --configsvr --replSet config --dbpath ./config-2 --port 50001 --fork --logpath=config-2.log
44136 ?? 0:01.37 mongod --configsvr --replSet config --dbpath ./config-3 --port 50002 --fork --logpath=config-3.log
44221 ?? 0:00.06 mongos --configdb config/127.0.0.1:50000,127.0.0.1:50001,127.0.0.1:50002 --port 60000 --fork --logpath=mongos.log
配置
mongo --port 60000
use admin
sh.addShard("shard-a/127.0.0.1:30000,127.0.0.1:30001")
sh.addShard("shard-b/127.0.0.1:40000,127.0.0.1:40001")
db.runCommand({listshards: 1})
{
"shards" : [
{
"_id" : "shard-a",
"host" : "shard-a/127.0.0.1:30000,127.0.0.1:30001",
"state" : 1
},
{
"_id" : "shard-b",
"host" : "shard-b/127.0.0.1:40000,127.0.0.1:40001",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1541488697, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1541488697, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}
sh.enableSharding("test-db")
sh.shardCollection("test-db.users", {name: 1})
测试
use config
db.settings.save( { _id:"chunksize", value: 1 } )
use test-db
for(i=0;i<200000;i++){ db.demo.insert({"uid":i,"name":"name"+i,"age":13,"data":new Date()}); } # 约3分钟
sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5be11f2bd8034ce4c5abccc1")
}
shards:
{ "_id" : "shard-a", "host" : "shard-a/127.0.0.1:30000,127.0.0.1:30001", "state" : 1 }
{ "_id" : "shard-b", "host" : "shard-b/127.0.0.1:40000,127.0.0.1:40001", "state" : 1 }
active mongoses:
"4.0.3" : 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:
15 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard-a 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard-a Timestamp(1, 0)
{ "_id" : "test-db", "primary" : "shard-b", "partitioned" : true, "version" : { "uuid" : UUID("1d04c2c7-1c32-4595-9f18-0d421cd45796"), "lastMod" : 1 } }
test-db.users
shard key: { "name" : 1 }
unique: false
balancing: true
chunks:
shard-a 9
shard-b 8
{ "name" : { "$minKey" : 1 } } -->> { "name" : "name1" } on : shard-b Timestamp(8, 0)
{ "name" : "name1" } -->> { "name" : "name105751" } on : shard-a Timestamp(8, 1)
{ "name" : "name105751" } -->> { "name" : "name111504" } on : shard-a Timestamp(7, 5)
{ "name" : "name111504" } -->> { "name" : "name13614" } on : shard-a Timestamp(7, 6)
{ "name" : "name13614" } -->> { "name" : "name14684" } on : shard-a Timestamp(7, 7)
{ "name" : "name14684" } -->> { "name" : "name15823" } on : shard-a Timestamp(7, 4)
{ "name" : "name15823" } -->> { "name" : "name21648" } on : shard-a Timestamp(4, 0)
{ "name" : "name21648" } -->> { "name" : "name27473" } on : shard-a Timestamp(5, 0)
{ "name" : "name27473" } -->> { "name" : "name33298" } on : shard-a Timestamp(6, 0)
{ "name" : "name33298" } -->> { "name" : "name4014" } on : shard-a Timestamp(7, 0)
{ "name" : "name4014" } -->> { "name" : "name5097" } on : shard-b Timestamp(7, 1)
{ "name" : "name5097" } -->> { "name" : "name5711" } on : shard-b Timestamp(4, 4)
{ "name" : "name5711" } -->> { "name" : "name6794" } on : shard-b Timestamp(2, 7)
{ "name" : "name6794" } -->> { "name" : "name73764" } on : shard-b Timestamp(6, 2)
{ "name" : "name73764" } -->> { "name" : "name79590" } on : shard-b Timestamp(6, 3)
{ "name" : "name79590" } -->> { "name" : "name9095" } on : shard-b Timestamp(6, 4)
{ "name" : "name9095" } -->> { "name" : { "$maxKey" : 1 } } on : shard-b Timestamp(1, 3)
mongo --port 60000
use test-db
db.users.find({ "name": "name1" })
# { "_id" : ObjectId("5be12c23e91421475eb4d5be"), "uid" : 1, "name" : "name1", "age" : 20, "data" : ISODate("2018-11-06T05:52:35.332Z") }
db.users.find({ "name": "name4014" })
# { "_id" : ObjectId("5be12c25e91421475eb4e56b"), "uid" : 4014, "name" : "name4014", "age" : 20, "data" : ISODate("2018-11-06T05:52:37.635Z") }
db.users.find({ "name": "name199999" })
# { "_id" : ObjectId("5be12caee91421475eb7e2fc"), "uid" : 199999, "name" : "name199999", "age" : 20, "data" : ISODate("2018-11-06T05:54:54.960Z") }
mongo --port 30000
use test-db
db.users.find({ "name": "name1" })
# { "_id" : ObjectId("5be12c23e91421475eb4d5be"), "uid" : 1, "name" : "name1", "age" : 20, "data" : ISODate("2018-11-06T05:52:35.332Z") }
db.users.find({ "name": "name4014" })
db.users.find({ "name": "name199999" })
# { "_id" : ObjectId("5be12caee91421475eb7e2fc"), "uid" : 199999, "name" : "name199999", "age" : 20, "data" : ISODate("2018-11-06T05:54:54.960Z") }
mongo --port 40000
use test-db
db.users.find({ "name": "name1" })
db.users.find({ "name": "name4014" })
# { "_id" : ObjectId("5be12c25e91421475eb4e56b"), "uid" : 4014, "name" : "name4014", "age" : 20, "data" : ISODate("2018-11-06T05:52:37.635Z") }
db.users.find({ "name": "name199999" })
网友评论