https://blog.csdn.net/vincerom/article/details/81145938
鏡像
docker pull mongo:4.2.7
網絡
docker network create --subnet=10.20.0.0/24 mongodbnet
文件夾
mkdir -p /home/soft/mongodbone/configsvr mkdir -p /home/soft/mongodbone/shard1 mkdir -p /home/soft/mongodbone/shard2 mkdir -p /home/soft/mongodbone/shard3 mkdir -p /home/soft/mongodbone/mongos
Config-Server 配置文件
路徑:vi /home/soft/mongodbone/configsvr/mongod.conf
storage: dbPath: /data/db journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: bindIp: 0.0.0.0 processManagement: timeZoneInfo: /usr/share/zoneinfo replication: replSetName: cfg sharding: clusterRole: configsvr
Mongos 配置文件
路徑:vi /home/soft/mongodbone/mongos/mongos.conf
systemLog: destination: file logAppend: true path: /var/log/mongodb/mongos.log net: port: 27020 bindIp: 0.0.0.0 processManagement: fork: true timeZoneInfo: /usr/share/zoneinfo sharding: configDB: cfg/10.20.0.2:27019,10.20.0.3:27019,10.20.0.4:27019
Shard-Server 配置文件1
路徑:vi /home/soft/mongodbone/shard1/mongod.conf
storage: dbPath: /data/db journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: bindIp: 0.0.0.0 processManagement: timeZoneInfo: /usr/share/zoneinfo replication: replSetName: shard1 sharding: clusterRole: shardsvr
Shard-Server 配置文件2
路徑:vi /home/soft/mongodbone/shard2/mongod.conf
storage: dbPath: /data/db journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: bindIp: 0.0.0.0 processManagement: timeZoneInfo: /usr/share/zoneinfo replication: replSetName: shard2 sharding: clusterRole: shardsvr
Shard-Server 配置文件3
路徑:vi /home/soft/mongodbone/shard3/mongod.conf
storage: dbPath: /data/db journal: enabled: true systemLog: destination: file logAppend: true path: /var/log/mongodb/mongod.log net: bindIp: 0.0.0.0 processManagement: timeZoneInfo: /usr/share/zoneinfo replication: replSetName: shard3 sharding: clusterRole: shardsvr
啟動Docker容器 啟動3個Config-Server容器:
docker run -d --restart=always --name=cfg_1 --network=mongodbnet --ip=10.20.0.2 -v /home/soft/mongodbone/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=cfg_2 --network=mongodbnet --ip=10.20.0.3 -v /home/soft/mongodbone/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=cfg_3 --network=mongodbnet --ip=10.20.0.4 -v /home/soft/mongodbone/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
進入其中一個容器配置Config-Server副本集:
# 宿主機 docker exec -it cfg_1 bash # 容器中 mongo --port 27019 # Mongo Shell中 rs.initiate({ "_id":"cfg", "members":[ { "_id":0, "host":"10.20.0.2:27019" }, { "_id":1, "host":"10.20.0.3:27019" }, { "_id":2, "host":"10.20.0.4:27019" } ] })
啟動3*3個Shard-Server容器:說明:分片服務器啟動后默認是以27018作為端口。
# 啟動第一個分片 - shard1
docker run -d --restart=always --name=shard1_1 --network=mongodbnet --ip=10.20.0.5 -v /home/soft/mongodbone/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=shard1_2 --network=mongodbnet --ip=10.20.0.6 -v /home/soft/mongodbone/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=shard1_3 --network=mongodbnet --ip=10.20.0.7 -v /home/soft/mongodbone/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
進入其中一個容器配置Shard-Server副本集:
# 宿主機 docker exec -it shard1_1 bash # 容器中 mongo --port 27018 # Mongo Shell中 rs.initiate({ "_id":"shard1", "members":[ { "_id":0, "host":"10.20.0.5:27018" }, { "_id":1, "host":"10.20.0.6:27018" }, { "_id":2, "host":"10.20.0.7:27018" } ] })
# 啟動第二個分片 - shard2
docker run -d --restart=always --name=shard2_1 --network=mongodbnet --ip=10.20.0.8 -v /home/soft/mongodbone/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=shard2_2 --network=mongodbnet --ip=10.20.0.9 -v /home/soft/mongodbone/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=shard2_3 --network=mongodbnet --ip=10.20.0.10 -v /home/soft/mongodbone/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf 進入其中一個容器配置Shard-Server副本集: # 宿主機 docker exec -it shard2_1 bash # 容器中 mongo --port 27018 # Mongo Shell中 rs.initiate({ "_id":"shard2", "members":[ { "_id":0, "host":"10.20.0.8:27018" }, { "_id":1, "host":"10.20.0.9:27018" }, { "_id":2, "host":"10.20.0.10:27018" } ] })
# 啟動第三個分片 - shard3
docker run -d --restart=always --name=shard3_1 --network=mongodbnet --ip=10.20.0.11 -v /home/soft/mongodbone/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=shard3_2 --network=mongodbnet --ip=10.20.0.12 -v /home/soft/mongodbone/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf docker run -d --restart=always --name=shard3_3 --network=mongodbnet --ip=10.20.0.13 -v /home/soft/mongodbone/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf 進入其中一個容器配置Shard-Server副本集: # 宿主機 docker exec -it shard3_1 bash # 容器中 mongo --port 27018 # Mongo Shell中 rs.initiate({ "_id":"shard3", "members":[ { "_id":0, "host":"10.20.0.11:27018" }, { "_id":1, "host":"10.20.0.12:27018" }, { "_id":2, "host":"10.20.0.13:27018" } ] })
啟動3個mongos服務器 說明:這里也使用了mongo鏡像,但是需要開啟mongos進程,mongod進程並不需要用到。
docker run -d --restart=always --name=mongos_1 --network=mongodbnet --ip=10.20.0.14 -v /home/soft/mongodbone/mongos:/etc/mongodb mongo:4.2.7 docker run -d --restart=always --name=mongos_2 --network=mongodbnet --ip=10.20.0.15 -v /home/soft/mongodbone/mongos:/etc/mongodb mongo:4.2.7 docker run -d --restart=always --name=mongos_3 --network=mongodbnet --ip=10.20.0.16 -v /home/soft/mongodbone/mongos:/etc/mongodb mongo:4.2.7 進入每個容器中,啟動mongos進程(此處可以改進一下,自動運行mongos進程) # 宿主機 docker exec -it mongos_1 bash # 容器中 mongos -f /etc/mongodb/mongos.conf 可以就在其中一個mongos容器中使用mongo shell連接mongos進程配置分片集群 # 連接mongos,端口號與mongos配置文件中設定一致 mongo -port 27020 # 將分片加入集群 sh.addShard("shard1/10.20.0.5:27018,10.20.0.6:27018,10.20.0.7:27018") sh.addShard("shard2/10.20.0.8:27018,10.20.0.9:27018,10.20.0.10:27018") sh.addShard("shard3/10.20.0.11:27018,10.20.0.12:27018,10.20.0.13:27018")
# 對數據庫開啟分片功能
sh.enableSharding("mydbtest")
# 對數據庫中集合開啟分片,並指定片鍵
sh.shardCollection("[dbName.collectionName]",{[keyName]:1})
sh.shardCollection("mydbtest.chenIndex",{"age":1})
6. 嘗試寫入數據觀察數據分塊
# 插入3000個簡單的文檔,耐心等待插入結束這個文檔5個字段name,age,score1,score2,score3,score4,score5 for(var i=1;i<=3000;i++){ db.chenIndex.insert({ name:i, age:Math.round(Math.random() * 100), score1:Math.round(Math.random() * 100), score2:Math.round(Math.random() * 100), score3:Math.round(Math.random() * 100), score4:Math.round(Math.random() * 100), score5:Math.round(Math.random() * 100) }); }
查詢數據數量
// 查詢茶樹數據數量 db.chenIndex.find().count() // 查詢一條數據 db.chenIndex.find().limit(1)
# 查看分片狀態
sh.status()
