Docker搭建MongoDB集群(副本分片)


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(
{
_id: "rs_configsvr",
configsvr: true,
members: [
{ _id: 0, host : "123.30.194.71:10021" },
{ _id: 1, host : "123.30.194.72:10022" },
{ _id: 2, host : "123.30.194.73:10023" }
]
}
);

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(
   {
_id: "rs_shardsvr0",
members: [
{ _id: 0, host : "123.30.194.71:10031" },
{ _id: 1, host : "123.30.194.72:10032" },
{ _id: 2, host : "123.30.194.73:10033" }
]
}
);

docker exec -it shardsvr10 bash
mongo --host 123.30.194.71 --port 10041

rs.initiate(
{
_id: "rs_shardsvr0",
members: [
{ _id: 0, host : "123.30.194.71:10041" },
{ _id: 1, host : "123.30.194.72:10042" },
{ _id: 2, host : "123.30.194.73:10043" }
]
}
);

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM