docker run --name mongo -v /mongodb/mongodb:/data/db -v /etc/localtime:/etc/localtime:ro -p 27017:27017 --restart=always -d mongo
docker inspect mongo | grep IPAddress
docker exec -it mongo bash
mongo --host 172.17.0.3 --port 27017
docker pull mongo:4.0.0
1、创建配置服务复制集
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
docker inspect configsvr0 | grep IPAddress
docker inspect configsvr1 | grep IPAddress
docker inspect configsvr2 | grep IPAddress
docker exec -it configsvr0 bash
mongo --host 172.17.0.7 --port 27019
rs.initiate(
{
_id: "rs_configsvr",
configsvr: true,
members: [
{ _id : 0, host : "172.17.0.3:27019" },
{ _id : 1, host : "172.17.0.4:27019" },
{ _id : 2, host : "172.17.0.7:27019" }
]
})
2、创建分片复制集
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 shardsvr00 | grep IPAddress
docker inspect shardsvr01 | grep IPAddress
docker inspect shardsvr02 | grep IPAddress
docker exec -it shardsvr00 bash
mongo --host 172.17.0.8 --port 27018
rs.initiate(
{
_id : "rs_shardsvr0",
members: [
{ _id : 0, host : "172.17.0.8:27018" },
{ _id : 1, host : "172.17.0.9:27018" },
{ _id : 2, host : "172.17.0.10:27018" }
]
})
docker inspect shardsvr10 | grep IPAddress
docker inspect shardsvr11 | grep IPAddress
docker inspect shardsvr12 | grep IPAddress
docker exec -it shardsvr10 bash
mongo --host 172.17.0.11 --port 27018
rs.initiate(
{
_id : "rs_shardsvr1",
members: [
{ _id : 0, host : "172.17.0.11:27018" },
{ _id : 1, host : "172.17.0.14:27018" },
{ _id : 2, host : "172.17.0.15:27018" }
]
})
3、创建mongos,连接mongos到分片集群
docker run --name mongos0 -d -p 27017:27017 --entrypoint "mongos" mongo --configdb rs_configsvr/172.17.0.3:27019,172.17.0.4:27019,172.17.0.7:27019 --bind_ip_all
4、添加分片到集群
docker inspect mongos0 | grep IPAddress
docker exec -it mongos0 bash
mongo --host 172.17.0.16 --port 27017
sh.addShard("rs_shardsvr0/172.17.0.8:27018,172.17.0.9:27018,172.17.0.10:27018")
sh.addShard("rs_shardsvr1/172.17.0.11:27018,172.17.0.14:27018,172.17.0.15:27018")
5、数据库 启用 分片
sh.enableSharding("tst99")
use tst99
db.tst99.insert({"name":"菜鸟教程5555"})
6、分片集合
# 对 test.order 的 _id 字段进行哈希分片:
sh.shardCollection("test.order", {"_id": "hashed" })
7、插入数据
use test
for (i = 1; i <= 1001; i=i+1){
db.order.insert({'price': 1})
}
8、查看数据分布
db.order.find().count()
#进入 shardsvr0
docker exec -it shardsvr00 bash
mongo --host 172.17.0.6 --port 27018
db.order.find().count()
484
#进入 shardsvr1
docker exec -it shardsvr10 bash
mongo --host 172.17.0.9 --port 27018
db.order.find().count()
517