使用 Docker 部署 MongoDB 分片
创建配置服务复制集
docker run --name configsvr0 -d mongo:3.6.2-jessie --configsvr --replSet "rs_configsvr" --bind_ip_all
docker run --name configsvr1 -d mongo:3.6.2-jessie --configsvr --replSet "rs_configsvr" --bind_ip_all
docker run --name configsvr2 -d mongo:3.6.2-jessie --configsvr --replSet "rs_configsvr" --bind_ip_all
$ docker ps
IMAGE COMMAND CREATED STATUS PORTS NAMES
mongo:3.6.2-jessie "docker-entrypoint..." 8 seconds ago Up 6 seconds 27019/tcp configsvr2
mongo:3.6.2-jessie "docker-entrypoint..." 9 seconds ago Up 7 seconds 27019/tcp configsvr1
mongo:3.6.2-jessie "docker-entrypoint..." 19 seconds ago Up 16 seconds 27019/tcp configsvr0
通过 docker inspect
找到三个配置服务实例的 IP。
比如 docker inspect configsvr0 | grep IPAddress
,
又由于--configsvr
的默认端口为 27019。所以配置服务的地址为
- configsvr0: 172.17.0.2:27019
- configsvr1: 172.17.0.3:27019
- configsvr2: 172.17.0.4:27019
初始化
docker exec -it configsvr0 bash
mongo --host 172.17.0.2 --port 27019
rs.initiate(
{
_id: "rs_configsvr",
configsvr: true,
members: [
{ _id : 0, host : "172.17.0.2:27019" },
{ _id : 1, host : "172.17.0.3:27019" },
{ _id : 2, host : "172.17.0.4:27019" }
]
}
)
连接字符串为
mongodb://172.17.0.2:27019,172.17.0.3:27019,172.17.0.4:27019/test?replicaSet=rs_configsvr
创建分片复制集
与上面同理,不再赘述,直接上命令行。
docker run --name shardsvr00 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr01 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr02 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr10 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
docker run --name shardsvr11 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
docker run --name shardsvr12 -d mongo:3.6.2-jessie --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
通过 docker inspect
找到对应实例的 IP。
比如 docker inspect shardsvr00 | grep IPAddress
,
又由于--shardsvr
的默认端口为 27018。所以地址为
- shardsvr00: 172.17.0.5:27018
- shardsvr01: 172.17.0.6:27018
- shardsvr02: 172.17.0.7:27018
由于后来才增加的 shardsvr1,所以 IP 末尾是 9 而不是 8。
- shardsvr10: 172.17.0.9:27018
- shardsvr11: 172.17.0.10:27018
- shardsvr12: 172.17.0.11:27018
$ docker exec -it shardsvr00 bash
mongo --host 172.17.0.5 --port 27018
> rs.initiate(
{
_id : "rs_shardsvr",
members: [
{ _id : 0, host : "172.17.0.5:27018" },
{ _id : 1, host : "172.17.0.6:27018" },
{ _id : 2, host : "172.17.0.7:27018" }
]
}
)
# 输出 { "ok" : 1 }
mongo --host 172.17.0.9 --port 27018
> rs.initiate(
{
_id : "rs_shardsvr1",
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" }
]
}
)
连接字符串为
mongodb://172.17.0.5:27018,172.17.0.6:27018,172.17.0.7:27018/test?replicaSet=rs_shardsvr0
mongodb://172.17.0.9:27018,172.17.0.10:27018,172.17.0.11:27018/test?replicaSet=rs_shardsvr1
连接 mongos 到分片集群
由于镜像的默认入口是 mongod
,所以要通过 --entrypoint "mongos"
将其改为 mongos
:
docker run --name mongos0 -d --entrypoint "mongos" mongo:3.6.2-jessie --configdb rs_configsvr/172.17.0.2:27019,172.17.0.3:27019,172.17.0.4:27019 --bind_ip_all
地址为 172.17.0.8:27017
增加分片到集群
$ docker exec -it mongos bash
$ mongo --host 172.17.0.8 --port 27017
> sh.addShard("rs_shardsvr0/172.17.0.5:27018,172.17.0.6:27018,172.17.0.7:27018")
{
"shardAdded" : "rs_shardsvr",
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1518405986, 8),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1518405986, 8)
}
> sh.addShard("rs_shardsvr1/172.17.0.9:27018,172.17.0.10:27018,172.17.0.11:27018")
数据库启用分片
> sh.enableSharding("test")
{
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1518406101, 8),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1518406101, 8)
}
分片 Collection
对 test.order
的 _id
字段进行哈希分片:
> sh.shardCollection("test.order", {"_id": "hashed" })
{
"collectionsharded" : "test.order",
"collectionUUID" : UUID("12b22b5d-93d7-4ccf-b989-2a0e550e750f"),
"ok" : 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1518406317, 12),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1518406317, 6)
}
插入数据
use test
for (i = 1; i <= 1000; i=i+1){
db.order.insert({'price': 1})
}
查看数据分布
mongos> db.order.find().count()
1000
rs_shardsvr0:PRIMARY> db.order.find().count()
526
rs_shardsvr1:PRIMARY> db.order.find().count()
474