mongodb分片集群由下面几个组建组成:
docker pull mongo 会从官方 下载最新的 镜像
docker run -d --name configsvr0 -v /home/mongodb/data/cs/configsvr0:/data/configdb mongo --configsvr --replSet “rs_configsvr” --bind_ip_all
docker run -d --name configsvr1 -v /home/mongodb/data/cs/configsvr1:/data/configdb mongo --configsvr --replSet “rs_configsvr” --bind_ip_all
docker run -d --name configsvr2 -v /home/mongodb/data/cs/configsvr2:/data/configdb 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 -v /home/mongodb/data/sh/shardsvr00:/data/db mongo --shardsvr --replSet “rs_shardsvr0” --bind_ip_all
docker run --name shardsvr01 -d -v /home/mongodb/data/sh/shardsvr01:/data/db mongo --shardsvr --replSet “rs_shardsvr0” --bind_ip_all
docker run --name shardsvr02 -d -v /home/mongodb/data/sh/shardsvr02:/data/db mongo --shardsvr --replSet “rs_shardsvr0” --bind_ip_all
docker run --name shardsvr10 -d -v /home/mongodb/data/sh/shardsvr10:/data/db mongo --shardsvr --replSet “rs_shardsvr1” --bind_ip_all
docker run --name shardsvr11 -d -v /home/mongodb/data/sh/shardsvr11:/data/db mongo --shardsvr --replSet “rs_shardsvr1” --bind_ip_all
docker run --name shardsvr12 -d -v /home/mongodb/data/sh/shardsvr12:/data/db 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 }
由于镜像的默认入口是 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.0.11: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
启动脚本 详见 github
https://github.com/yekoufeng/docker_mongodb_rs