mongodb分片模型圖
mongodb將大量的數據文件進行切割,將切割的數據塊分別保存到不同的片上,其中每個片爲一個副本集,而對外通過mongos提供統一接口,用戶實際上感覺不到內部分片機制。
ReplicaSet(副本集): mongodb集羣的一種實現方式,該副本集由一臺活躍Mongodb服務器(節點)和兩臺非活躍Mongodb服務器(節點)組成,其中活躍節點會由Mongodb自動選舉產生,數據插入在活躍節點,備份到非活躍節點,默認非活躍節點不可進行讀寫操作,可配置在非活躍節點進行讀操作。
Mongos: mongo路由器,對外屏蔽內部數據存儲,客戶端只需要連接mongos,mongos會根據實際情況將操作分發到不同的片(Replica Set)
shard: 存儲數據的節點,可以是單個mongod或者副本集
環境說明&機器規劃
操作系統:CentOS7.6
MongoDB:v3.6.11
首先確定各個組件的數量,mongos 3個, config server 3個,數據分3片 shard server 3個,每個shard 有一個副本一個仲裁也就是 3 * 2 = 6 個,總共需要部署15個實例。這裏使用三臺機器進行測試,機器規劃如下圖所示:
集羣搭建
1.創建目錄
在每臺機器上分別創建mongos 、config 、 shard1 、shard2、shard3
對應目錄:
mkdir -p /data/db/{shard1,shard2,shard3}
mkdir -p /data/configdb
mkdir -p /data/logs
chown -R mongod.mongod /data/configdb/
chown -R mongod.mongod /data/logs/
chown -R mongod.mongod /data/db
# tree /data/
/data/
├── configdb
├── db
│ ├── shard1
│ ├── shard2
│ └── shard3
└── logs
6 directories, 0 files
2.配置shard server(分片服務)
(1)在每臺機器上分別啓動副本集成員:
mongod --shardsvr --replSet shard1 --port 27018 --dbpath /data/db/shard1/ --logpath /data/logs/shard1.log --bind_ip_all --logappend --fork
mongod --shardsvr --replSet shard2 --port 27019 --dbpath /data/db/shard2/ --logpath /data/logs/shard2.log --bind_ip_all --logappend --fork
mongod --shardsvr --replSet shard3 --port 27020 --dbpath /data/db/shard3/ --logpath /data/logs/shard3.log --bind_ip_all --logappend --fork
(2)創建副本集並初始化:
複製集通過replSetInitiate
命令(或mongo shell的rs.initiate()
)進行初始化
在設置mongodb副本集時,Primary節點,second節點,仲裁節點,有可能資源配置(CPU或者內存)不均衡,所以要求某些節點不能成爲Primary。我們知道mongodb的設置:
- 除了仲裁節點,其他每個節點都有個優先權,可以手動設置優先權來決定誰的成爲primay的權重最大。
- 副本集中通過設置priority的值來決定優先權的大小,這個值的範圍是0–100,值越大,優先權越高。
- 默認的值是1,rs.conf是不顯示的;如果值是0,那麼不能成爲primay。
登錄成員主節點,設置節點成員:
192.168.20.215:
# mongo --port 27018
> config={_id:"shard1",members:[{_id:0,host:"192.168.20.215:27018",priority:2},{_id:1,host:"192.168.20.216:27018"},{_id:2,host:"192.168.20.217:27018",arbiterOnly:1}]}
> rs.initiate(config)
192.168.20.216:
# mongo --port 27019
> config={_id:"shard2",members:[{_id:0,host:"192.168.20.215:27019"},{_id:1,host:"192.168.20.216:27019",priority:2},{_id:2,host:"192.168.20.217:27019",arbiterOnly:1}]}
> rs.initiate(config)
192.168.20.217:
# mongo --port 27020
> config={_id:"shard3",members:[{_id:0,host:"192.168.20.215:27020",arbiterOnly:1},{_id:1,host:"192.168.20.216:27020"},{_id:2,host:"192.168.20.217:27020",priority:2}]}
> rs.initiate(config)
replSetInitiate命令:
> db.runCommand({"replSetInitiate":{"_id":"shard1","members":[{_id:0,host:"192.168.20.215:27018",priority:2},{_id:1,host:"192.168.20.216:27018"},{_id:2,host:"192.168.20.217:27018",arbiterOnly:1}]}})
3.配置config server
(1)在每一臺服務器分別啓動配置服務器:
mongod --configsvr --replSet cfgReplSet --port 27016 --dbpath /data/configdb/ --logpath /data/logs/config.log --bind_ip_all --logappend --fork
(2)連接到任意一臺配置服務器上,創建配置服務器副本集
# mongo --port 27016
> rs.initiate({_id:"cfgReplSet",configsvr:true,members:[{_id:0,host:"192.168.20.215:27016"},{_id:1,host:"192.168.20.216:27016"},{_id:2,host:"192.168.20.217:27016"}]})
4.配置mongos
(1)在每臺機上啓動mongos路由服務
mongos --port 27017 --configdb cfgReplSet/192.168.20.215:27016,192.168.20.216:27016,192.168.20.217:27016 --logpath /data/logs/mongos.log --bind_ip_all --logappend --fork
(2)添加shards成員
- 1.連接到mongos
- 2.add shards
- 3.enable sharding
- 4.對一個集合進行分片
登錄路由服務客戶端,添加分片到集羣:
# mongo #mongos啓動的27017端口
mongos> use admin
mongos> db.runCommand({addshard:"shard1/192.168.20.215:27018,192.168.20.216:27018,192.168.20.217:27018"})
mongos> db.runCommand({addshard:"shard2/192.168.20.215:27019,192.168.20.216:27019,192.168.20.217:27019"})
mongos> db.runCommand({addshard:"shard3/192.168.20.215:27020,192.168.20.216:27020,192.168.20.217:27020"})
開啓數據庫的分片功能
mongos> use admin
mongos> db.runCommand({enablesharding:"mydb1"})
開啓集合的分片功能:
mongos> use admin
mongos> db.runCommand({shardcollection:"mydb1.user",key:{userid:"hashed"}})
驗證
1.查看分片db.shards.find()
:
mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/192.168.20.215:27018,192.168.20.216:27018", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/192.168.20.215:27019,192.168.20.216:27019", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/192.168.20.216:27020,192.168.20.217:27020", "state" : 1 }
2.查看集合狀態db.collectionName.status()
:
mongos> use mydb1
mongos> db.user.stats()
3.查看分片信息db.printShardingStatus()
:
4.插入數據測試:
mongos> use mydb1
mongos> for(i=0;i<100000;i++){db.user.insert({userid:i})}