服務器
192.168.5.201 192.168.5.202 192.168.5.203
==========以下每台都執行========================
===========安裝過程=================
鏡像
docker pull mongo:4.2.7
網絡
docker network create --subnet=10.20.0.0/24 mongodbnet
文件夾
mkdir -p /home/soft/mongodbmoney/configsvr mkdir -p /home/soft/mongodbmoney/shard1 mkdir -p /home/soft/mongodbmoney/shard2 mkdir -p /home/soft/mongodbmoney/shard3 mkdir -p /home/soft/mongodbmoney/mongos
Config-Server 配置文件
路徑:vi /home/soft/mongodbmoney/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/mongodbmoney/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/192.168.5.201:27019,192.168.5.202:27019,192.168.5.203:27019
Shard-Server 配置文件1
路徑:vi /home/soft/mongodbmoney/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/mongodbmoney/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/mongodbmoney/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 -p 27019:27019 --network=mongodbnet -v /home/soft/mongodbmoney/configsvr:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
啟動3*3個Shard-Server容器:說明:分片服務器啟動后默認是以27018作為端口。
# 啟動第一個分片 - shard1
docker run -d --restart=always --name=shard1_1 -p 27018:27018 --network=mongodbnet -v /home/soft/mongodbmoney/shard1:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
# 啟動第二個分片 - shard2
docker run -d --restart=always --name=shard2_1 -p 27028:27018 --network=mongodbnet -v /home/soft/mongodbmoney/shard2:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
# 啟動第三個分片 - shard3
docker run -d --restart=always --name=shard3_1 -p 27038:27018 --network=mongodbnet -v /home/soft/mongodbmoney/shard3:/etc/mongodb mongo:4.2.7 -f /etc/mongodb/mongod.conf
啟動3個mongos服務器 說明:這里也使用了mongo鏡像,但是需要開啟mongos進程,mongod進程並不需要用到。
docker run -d --restart=always --name=mongos_1 -p 27017:27017 -p 27020:27020 --network=mongodbnet -v /home/soft/mongodbmoney/mongos:/etc/mongodb mongo:4.2.7
============集群過程========================
進入其中一個容器配置Config-Server副本集:
# 宿主機 docker exec -it cfg_1 bash # 容器中 mongo --port 27019 # Mongo Shell中 rs.initiate({ "_id":"cfg", "members":[ { "_id":0, "host":"192.168.5.201:27019" }, { "_id":1, "host":"192.168.5.202:27019" }, { "_id":2, "host":"192.168.5.203:27019" } ] })
進入其中一個容器配置Shard-Server1副本集:
# 宿主機 docker exec -it shard1_1 bash # 容器中 mongo --port 27018 # Mongo Shell中 rs.initiate({ "_id":"shard1", "members":[ { "_id":0, "host":"192.168.5.201:27018" }, { "_id":1, "host":"192.168.5.202:27018" }, { "_id":2, "host":"192.168.5.203:27018" } ] })
進入其中一個容器配置Shard-Server2副本集:
# 宿主機 docker exec -it shard2_1 bash # 容器中 mongo --port 27018 # Mongo Shell中 rs.initiate({ "_id":"shard2", "members":[ { "_id":0, "host":"192.168.5.201:27028" }, { "_id":1, "host":"192.168.5.202:27028" }, { "_id":2, "host":"192.168.5.203:27028" } ] })
進入其中一個容器配置Shard-Server3副本集:
# 宿主機 docker exec -it shard3_1 bash # 容器中 mongo --port 27018 # Mongo Shell中 rs.initiate({ "_id":"shard3", "members":[ { "_id":0, "host":"192.168.5.201:27038" }, { "_id":1, "host":"192.168.5.202:27038" }, { "_id":2, "host":"192.168.5.203:27038" } ] })
進入mongos容器中,啟動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/192.168.5.201:27018,192.168.5.202:27018,192.168.5.203:27018") sh.addShard("shard2/192.168.5.201:27028,192.168.5.202:27028,192.168.5.203:27028") sh.addShard("shard3/192.168.5.201:27038,192.168.5.202:27038,192.168.5.203:27038")
# 對數據庫開啟分片功能 sh.enableSharding("mydbtest") # 對數據庫中集合開啟分片,並指定片鍵 sh.shardCollection("mydbtest.coll1",{"age":1}) sh.shardCollection("[dbName.collectionName]",{[keyName]:1})
6. 嘗試寫入數據觀察數據分塊
# 插入1000個簡單的文檔,耐心等待插入結束 for(var i=1;i<=1000;i++){ db.coll1.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.coll1.find().count() db.coll1.find().limit(1) # 查看分片狀態 sh.status()
