1.下载 mongo最新版本
docker pull mongo
2.创建配置服务复制集
123.30.194.71:
docker run -d --name configsvr0 -p 10021:27019 -v /home/mongodb/data/cs/configsvr0:/data/configdb mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
123.30.194.72:
docker run -d --name configsvr1 -p 10022:27019 -v /home/mongodb/data/cs/configsvr1:/data/configdb mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
123.30.194.73:
docker run -d --name configsvr2 -p 10023:27019 -v /home/mongodb/data/cs/configsvr2:/data/configdb mongo --configsvr --replSet "rs_configsvr" --bind_ip_all
初始化配置服务复制集:
docker exec -it configsvr0 bash
mongo --host 123.30.194.71 --port 10021
rs.initiate( |
3.创建分片复制集
123.30.194.71:
docker run --name shardsvr00 -p 10031:27018 -d -v /home/mongodb/data/sh/shardsvr00:/data/db mongo --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr10 -p 10041:27018 -d -v /home/mongodb/data/sh/shardsvr10:/data/db mongo --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
123.30.194.72:
docker run --name shardsvr01 -p 10032:27018 -d -v /home/mongodb/data/sh/shardsvr00:/data/db mongo --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr11 -p 10042:27018 -d -v /home/mongodb/data/sh/shardsvr11:/data/db mongo --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
123.30.194.73:
docker run --name shardsvr02 -p 10033:27018 -d -v /home/mongodb/data/sh/shardsvr00:/data/db mongo --shardsvr --replSet "rs_shardsvr0" --bind_ip_all
docker run --name shardsvr12 -p 10043:27018 -d -v /home/mongodb/data/sh/shardsvr12:/data/db mongo --shardsvr --replSet "rs_shardsvr1" --bind_ip_all
初始化副本集
docker exec -it shardsvr00 bash
mongo --host 123.30.194.71 --port 10031
rs.initiate( { |
docker exec -it shardsvr10 bash
mongo --host 123.30.194.71 --port 10041
rs.initiate( |
4.创建mongos,连接mongos到分片集群
123.30.194.71:
docker run --name mongos0 -d -p 10011:27017 --entrypoint "mongos" mongo --configdb rs_configsvr/123.30.194.71:10021,123.30.194.72:10022,123.30.194.73:10023 --bind_ip_all
123.30.194.72:
docker run --name mongos1 -d -p 10012:27017 --entrypoint "mongos" mongo --configdb rs_configsvr/123.30.194.71:10021,123.30.194.72:10022,123.30.194.73:10023 --bind_ip_all
添加分片到集群
docker exec -it mongos0 bash
mongo --host 123.30.194.71 --port 10011
sh.addShard("rs_shardsvr0/123.30.194.71:10031,123.30.194.72:10032,123.30.194.73:10033")
sh.addShard("rs_shardsvr1/123.30.194.71:10041,123.30.194.72:10042,123.30.194.73:10043")
数据库 启用 分片
sh.enableSharding(“test”)
分片集合
对 test.order 的 _id 字段进行哈希分片:
sh.shardCollection(“test.order”, {"_id": “hashed” })
插入数据
use test
for (i = 1; i <= 1001; i=i+1){
db.order.insert({‘price’: 1})
}
查看数据分布
db.order.find().count()
1001