基於docker 的mongodb4集羣、分片搭建
mongodb集羣、分片概述
mongodb分片集羣由下面幾個組建組成:
- shard
- mongos
- configure servers
詳細見:https://docs.mongodb.com/manual/sharding/
如何搭建分片集羣(在linux機器測試通過)
創建配置服務複製集
docker run --name configsvr0 -d mongo --configsvr --replSet “rs_configsvr” --bind_ip_all
docker run --name configsvr1 -d mongo --configsvr --replSet “rs_configsvr” --bind_ip_all
docker run --name configsvr2 -d mongo --configsvr --replSet “rs_configsvr” --bind_ip_all
備註:mongo 就是官方的最新mongo鏡像,docker pull下就可以了。
通過docker inspect 容器名去獲取在容器裏的ip地址:
docker inspect configsvr0 | grep IPAddress
docker inspect configsvr1 | grep IPAddress
docker inspect configsvr2 | grep IPAddress
由於–configsvr 的默認端口爲 27019。所以配置服務的地址爲(我的機器,你的機器會不一樣)
•configsvr0: 172.17.0.3:27019
•configsvr1: 172.17.0.7:27019
•configsvr2: 172.17.0.8:27019
初始化配置服務複製集:
docker exec -it configsvr0 bash
mongo --host 172.17.0.3 --port 27019
rs.initiate(
{
_id: “rs_configsvr”,
configsvr: true,
members: [
{ _id : 0, host : “172.17.0.3:27019” },
{ _id : 1, host : “172.17.0.7:27019” },
{ _id : 2, host : “172.17.0.8:27019” }
]
}
)
創建分片複製集
docker run --name shardsvr00 -d mongo --shardsvr --replSet “rs_shardsvr0” --bind_ip_all
docker run --name shardsvr01 -d mongo --shardsvr --replSet “rs_shardsvr0” --bind_ip_all
docker run --name shardsvr02 -d mongo --shardsvr --replSet “rs_shardsvr0” --bind_ip_all
docker run --name shardsvr10 -d mongo --shardsvr --replSet “rs_shardsvr1” --bind_ip_all
docker run --name shardsvr11 -d mongo --shardsvr --replSet “rs_shardsvr1” --bind_ip_all
docker run --name shardsvr12 -d mongo --shardsvr --replSet “rs_shardsvr1” --bind_ip_all
繼續通過 docker inspect 找出實例的ip
由於–shardsvr 的默認端口爲 27018。所以地址爲
•shardsvr00: 172.17.0.9:27018
•shardsvr01: 172.17.0.10:27018
•shardsvr02: 172.17.0.11:27018
•shardsvr10: 172.17.0.12:27018
•shardsvr11: 172.17.0.13:27018
•shardsvr12: 172.17.0.14:27018
$ docker exec -it shardsvr00 bash
mongo --host 172.17.0.9 --port 27018
rs.initiate(
{
_id : “rs_shardsvr0”,
members: [
{ _id : 0, host : “172.17.0.9:27018” },
{ _id : 1, host : “172.17.0.10:27018” },
{ _id : 2, host : “172.17.0.11:27018” }
]
}
)
輸出 { “ok” : 1 }
mongo --host 172.17.0.12 --port 27018
rs.initiate(
{
_id : “rs_shardsvr1”,
members: [
{ _id : 0, host : “172.17.0.12:27018” },
{ _id : 1, host : “172.17.0.13:27018” },
{ _id : 2, host : “172.17.0.14:27018” }
]
}
)
輸出 { “ok” : 1 }
創建mongos,連接mongos到分片集羣
由於鏡像的默認入口是 mongod,所以要通過 --entrypoint “mongos” 將其改爲 mongos:
docker run --name mongos0 -d -p 27017:27017 --entrypoint “mongos” mongo --configdb rs_configsvr/172.17.0.3:27019,172.17.0.7:27019,172.17.0.8:27019 --bind_ip_all
地址爲:172.17.0.15:27017
注:映射到宿主機便於外面客戶端訪問
添加分片到集羣
docker exec -it mongos0 bash
mongo --host 172.17.0.15 --port 27017
sh.addShard(“rs_shardsvr0/172.17.0.9:27018,172.17.0.10:27018,172.17.11.7:27018”)
{
“shardAdded” : “rs_shardsvr0”,
“ok” : 1,
“operationTime” : Timestamp(1540463225, 1),
“$clusterTime” : {
“clusterTime” : Timestamp(1540463225, 1),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
}
}
sh.addShard(“rs_shardsvr1/172.17.0.12:27018,172.17.0.13:27018,172.17.0.14:27018”)
數據庫 啓用 分片
sh.enableSharding(“test”)
{
“ok” : 1,
“operationTime” : Timestamp(1540463335, 5),
“$clusterTime” : {
“clusterTime” : Timestamp(1540463335, 5),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
}
}
分片集合
對 test.order 的 _id 字段進行哈希分片:
sh.shardCollection(“test.order”, {"_id": “hashed” })
{
“collectionsharded” : “test.order”,
“collectionUUID” : UUID(“ed4a25e5-a890-487a-8152-45fe7ccfe9c9”),
“ok” : 1,
“operationTime” : Timestamp(1540463374, 2),
“$clusterTime” : {
“clusterTime” : Timestamp(1540463374, 2),
“signature” : {
“hash” : BinData(0,“AAAAAAAAAAAAAAAAAAAAAAAAAAA=”),
“keyId” : NumberLong(0)
}
}
}
插入數據
use test
for (i = 1; i <= 1001; i=i+1){
db.order.insert({‘price’: 1})
}
查看數據分佈
db.order.find().count()
1001
進入 shardsvr0
docker exec -it shardsvr00 bash
db.order.find().count()
484
進入 shardsvr1
docker exec -it shardsvr10 bash
db.order.find().count()
517