1. 程式人生 > >mongoDB分片技術

mongoDB分片技術

數據塊 啟動參數 -o ESS 單位 所有 conn 這一 代碼

MongoDB 分片

分片

在Mongodb裏面存在另一種集群,就是分片技術,可以滿足MongoDB數據量大量增長的需求。


當MongoDB存儲海量的數據時,一臺機器可能不足以存儲數據,也可能不足以提供可接受的讀寫吞吐量。這時,我們就可以通過在多臺機器上分割數據,使得數據庫系統能存儲和處理更多的數據。


為什麽使用分片

復制所有的寫入操作到主節點

延遲的敏感數據會在主節點查詢

單個副本集限制在12個節點

當請求量巨大時會出現內存不足。

本地磁盤不足

垂直擴展價格昂貴

MongoDB分片

下圖展示了在MongoDB中使用分片集群結構分布:

技術分享圖片



上圖中主要有如下所述三個主要組件:


Shard:

用於存儲實際的數據塊,實際生產環境中一個shard server角色可由幾臺機器組個一個replica set承擔,防止主機單點故障


Config Server:

mongod實例,存儲了整個 ClusterMetadata,其中包括 chunk信息。


Query Routers:

前端路由,客戶端由此接入,且讓整個集群看上去像單一數據庫,前端應用可以透明使用。


分片實例

分片結構端口分布如下:


Shard Server 1:27020

Shard Server 2:27021

Shard Server 3:27022

Shard Server 4:27023

Config Server :27100

Route Process:40000

步驟一:啟動Shard Server


[root@100 /]# mkdir -p /www/mongoDB/shard/s0

[root@100 /]# mkdir -p /www/mongoDB/shard/s1

[root@100 /]# mkdir -p /www/mongoDB/shard/s2

[root@100 /]# mkdir -p /www/mongoDB/shard/s3

[root@100 /]# mkdir -p /www/mongoDB/shard/log

[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27020 --dbpath=/www/mongoDB/shard/s0 --logpath=/www/mongoDB/shard/log/s0.log --logappend --fork

....

[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27023 --dbpath=/www/mongoDB/shard/s3 --logpath=/www/mongoDB/shard/log/s3.log --logappend --fork

步驟二: 啟動Config Server


[root@100 /]# mkdir -p /www/mongoDB/shard/config

[root@100 /]# /usr/local/mongoDB/bin/mongod --port 27100 --dbpath=/www/mongoDB/shard/config --logpath=/www/mongoDB/shard/log/config.log --logappend --fork

註意:這裏我們完全可以像啟動普通mongodb服務一樣啟動,不需要添加—shardsvr和configsvr參數。因為這兩個參數的作用就是改變啟動端口的,所以我們自行指定了端口就可以。


步驟三: 啟動Route Process


/usr/local/mongoDB/bin/mongos --port 40000 --configdb localhost:27100 --fork --logpath=/www/mongoDB/shard/log/route.log --chunkSize 500

mongos啟動參數中,chunkSize這一項是用來指定chunk的大小的,單位是MB,默認大小為200MB.


步驟四: 配置Sharding


接下來,我們使用MongoDB Shell登錄到mongos,添加Shard節點


[root@100 shard]# /usr/local/mongoDB/bin/mongo admin --port 40000

MongoDB shell version: 2.0.7

connecting to: 127.0.0.1:40000/admin

mongos> db.runCommand({ addshard:"localhost:27020" })

{ "shardAdded" : "shard0000", "ok" : 1 }

......

mongos> db.runCommand({ addshard:"localhost:27029" })

{ "shardAdded" : "shard0009", "ok" : 1 }

mongos> db.runCommand({ enablesharding:"test" }) #設置分片存儲的數據庫

{ "ok" : 1 }

mongos> db.runCommand({ shardcollection: "test.log", key: { id:1,time:1}})

{ "collectionsharded" : "test.log", "ok" : 1 }

步驟五: 程序代碼內無需太大更改,直接按照連接普通的mongo數據庫那樣,將數據庫連接接入接口40000


補充:

1. 創建Sharding復制集 rs0


# mkdir /data/log

# mkdir /data/db1

# nohup mongod --port 27020 --dbpath=/data/db1 --logpath=/data/log/rs0-1.log --logappend --fork --shardsvr --replSet=rs0 &


# mkdir /data/db2

# nohup mongod --port 27021 --dbpath=/data/db2 --logpath=/data/log/rs0-2.log --logappend --fork --shardsvr --replSet=rs0 &

1.1 復制集rs0配置


# mongo localhost:27020 > rs.initiate({_id: 'rs0', members: [{_id: 0, host: 'localhost:27020'}, {_id: 1, host: 'localhost:27021'}]}) > rs.isMaster() #查看主從關系

2. 創建Sharding復制集 rs1


# mkdir /data/db3

# nohup mongod --port 27030 --dbpath=/data/db3 --logpath=/data/log/rs1-1.log --logappend --fork --shardsvr --replSet=rs1 &

# mkdir /data/db4

# nohup mongod --port 27031 --dbpath=/data/db4 --logpath=/data/log/rs1-2.log --logappend --fork --shardsvr --replSet=rs1 &

2.1 復制集rs1配置


# mongo localhost:27030

> rs.initiate({_id: 'rs1', members: [{_id: 0, host: 'localhost:27030'}, {_id: 1, host: 'localhost:27031'}]})

> rs.isMaster() #查看主從關系

3. 創建Config復制集 conf


# mkdir /data/conf1

# nohup mongod --port 27100 --dbpath=/data/conf1 --logpath=/data/log/conf-1.log --logappend --fork --configsvr --replSet=conf &

# mkdir /data/conf2

# nohup mongod --port 27101 --dbpath=/data/conf2 --logpath=/data/log/conf-2.log --logappend --fork --configsvr --replSet=conf &

3.1 復制集conf配置


# mongo localhost:27100

> rs.initiate({_id: 'conf', members: [{_id: 0, host: 'localhost:27100'}, {_id: 1, host: 'localhost:27101'}]})

> rs.isMaster() #查看主從關系

4. 創建Route


# nohup mongos --port 40000 --configdb conf/localhost:27100,localhost:27101 --fork --logpath=/data/log/route.log --logappend &

4.1 設置分片


# mongo localhost:40000

> use admin

> db.runCommand({ addshard: 'rs0/localhost:27020,localhost:27021'})

> db.runCommand({ addshard: 'rs1/localhost:27030,localhost:27031'})

> db.runCommand({ enablesharding: 'test'})

> db.runCommand({ shardcollection: 'test.user', key: {name: 1}})


mongoDB分片技術