mongdb分片原理以及分片副本集羣搭建

mongdb分片原理
分片,是指將數據拆分,將其分散到不同的機器上,分片類似於raid0,副本類似於raid1
MongoDB的副本集與我們常見的主從有所不同,主從在主機宕機後所有服務將停止

分片集羣主要由三種組件組成:mongos,config server,shard
1) mongos (路由進程, 應用程序接入 mongos 再查詢到具體分片)
數據庫集羣請求的入口,所有的請求都通過 mongos 進行協調,不需要在應用程序添加一個路由選擇器,mongos 自己就是一個請求分發中心,它負責把對應的數據請求
請求轉發到對應的 shard 服務器上。在生產環境通常有多個 mongos 作爲請求的入口,防止其中一個掛掉所有的 mongodb 請求都沒有辦法操作。
2) config server (路由表服務。 每一臺都具有全部 chunk 的路由信息)
顧名思義爲配置服務器,存儲所有數據庫元信息(路由、分片)的配置。mongos 本身沒有物理存儲分片服務器和數據路由信息,只是緩存在內存裏,配置服務器則實際存儲
這些數據。mongos 第一次啓動或者關掉重啓就會從 config server 加載配置信息,以後如果配置服務器信息變化會通知到所有的 mongos 更新自己的狀態,這樣
mongos 就能繼續準確路由。在生產環境通常有多個 config server 配置服務器,因爲它存儲了分片路由的元數據,這個可不能丟失!就算掛掉其中一臺,只要還有存貨,
mongodb 集羣就不會掛掉。
3) shard (爲數據存儲分片。 每一片都可以是複製集(replica set))
這就是傳說中的分片了。如圖所示,一臺機器的一個數據表 Collection1 存儲了 1T 數據,壓力太大了!在分給 4 個機器後, 每個機器都是 256G,則分攤了集中在一臺
機器的壓力。事實上,上圖4個分片如果沒有副本集(replica set)是個不完整架構,假設其中的一個分片掛掉那四 分之一的數據就丟失了,所以在高可用性的分片架構還
需要對於每一個分片構建 replica set 副本集保 證分片的可靠性。生產環境通常是 2 個副本 + 1 個仲裁。

廢話不多說

1.從github上拉取配置文件
git clone [email protected]:herrywen-nanj/mongodb.git

2.啓動順序爲configserver --> mongos --> shared

3.刪除dbPath下的內容
rm -rf configserver/dbPath

4.根據對應配置文件啓動mongdb進程,進入mongdb中配置副本集
configserver啓動:
mongod -f mongdb.conf
configserver2啓動:
mongod -f mongdb.conf
配置副本集:

進入configserver

mongo --port 27018

初始化

rs.initiate()

添加副本節點

rs.add("worker2:27019")

查看副本集狀態

rs.status
返回內容:

MongoDB Enterprise config-rs:PRIMARY> rs.status()
{
"set" : "config-rs", # 副本集已經配置成功
"date" : ISODate("2019-11-23T04:56:35.588Z"),
"myState" : 1,
"term" : NumberLong(1),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"configsvr" : true,
"heartbeatIntervalMillis" : NumberLong(2000),
"majorityVoteCount" : 2,
"writeMajorityCount" : 2,
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"lastCommittedWallTime" : ISODate("2019-11-23T04:56:22.464Z"),
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"readConcernMajorityWallTime" : ISODate("2019-11-23T04:56:22.464Z"),
"appliedOpTime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"durableOpTime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"lastAppliedWallTime" : ISODate("2019-11-23T04:56:22.464Z"),
"lastDurableWallTime" : ISODate("2019-11-23T04:56:22.464Z")
},
"lastStableRecoveryTimestamp" : Timestamp(1574484952, 30),
"lastStableCheckpointTimestamp" : Timestamp(1574484952, 30),
"electionCandidateMetrics" : {
"lastElectionReason" : "electionTimeout",
"lastElectionDate" : ISODate("2019-11-23T04:55:51.134Z"),
"termAtElection" : NumberLong(1),
"lastCommittedOpTimeAtElection" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"lastSeenOpTimeAtElection" : {
"ts" : Timestamp(1574484951, 1),
"t" : NumberLong(-1)
},
"numVotesNeeded" : 1,
"priorityAtElection" : 1,
"electionTimeoutMillis" : NumberLong(10000),
"newTermStartDate" : ISODate("2019-11-23T04:55:52.141Z"),
"wMajorityWriteAvailabilityDate" : ISODate("2019-11-23T04:55:52.266Z")
},
"members" : [
{
"_id" : 0,
"name" : "worker2:27018",
"ip" : "192.168.255.134",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 722,
"optime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-23T04:56:22Z"),
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1574484951, 2),
"electionDate" : ISODate("2019-11-23T04:55:51Z"),
"configVersion" : 2,
"self" : true,
"lastHeartbeatMessage" : ""
},
{
"_id" : 1,
"name" : "worker2:27019",
"ip" : "192.168.255.134",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 13,
"optime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"optimeDurable" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2019-11-23T04:56:22Z"),
"optimeDurableDate" : ISODate("2019-11-23T04:56:22Z"),
"lastHeartbeat" : ISODate("2019-11-23T04:56:34.705Z"),
"lastHeartbeatRecv" : ISODate("2019-11-23T04:56:35.176Z"),
"pingMs" : NumberLong(0),
"lastHeartbeatMessage" : "",
"syncingTo" : "",
"syncSourceHost" : "",
"syncSourceId" : -1,
"infoMessage" : "",
"configVersion" : 2
}
],
"ok" : 1,
"$gleStats" : {
"lastOpTime" : {
"ts" : Timestamp(1574484982, 1),
"t" : NumberLong(1)
},
"electionId" : ObjectId("7fffffff0000000000000001")
},
"lastCommittedOpTime" : Timestamp(1574484982, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1574484982, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
},
"operationTime" : Timestamp(1574484982, 1)
}

5.刪除dbPath目錄並啓動mongs路由進程
cd luyou
mongos -f mongdb.conf
lsof -i:40000

6.分別進入分片節點目錄,啓動進程,完成副本集羣添加操作
cd shared && rm -rf dbpath && mongod -f mongdb.conf
lsof -i:27020 && lsof -i:27021 && lsof -i:27022

mongo --port 27020

rs.initiate()
rs.add("worker2:27021")
rs.add("worker2:27022")
rs.status()

7.在路由節點添加分片集羣並新增一個分片
sh.addShard("shard-rs/worker2:27020,worker2:27021,worker2:27022")

新增分片:
cd shard4
sh.addShard("worker2:27023")
查看返回狀態:

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5dd8bbd8bbb4a8ac81b4b0b6")
}
shards:
{ "_id" : "shard-rs", "host" : "shard-rs/worker2:27020,worker2:27021,worker2:27022", "state" : 1 }
{ "_id" : "shard0001", "host" : "worker2:27023", "state" : 1 }
active mongoses:
"4.2.1" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }

8.片鍵操作
a.爲herrywen這個數據庫開啓分片功能

sh.enableSharding("herrywen")
查看返回狀態:

MongoDB Enterprise mongos> sh.status()
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5dd8bbd8bbb4a8ac81b4b0b6")
}
shards:
{ "_id" : "shard-rs", "host" : "shard-rs/worker2:27020,worker2:27021,worker2:27022", "state" : 1 }
active mongoses:
"4.2.1" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
{ "_id" : "herrywen", "primary" : "shard-rs", "partitioned" : true, "version" : { "uuid" : UUID("56cf9d23-2f3a-4b53-8b5d-512f1f9e00c6"), "lastMod" : 1 } }

b. 開啓特定集合功能,並指定id爲片鍵
sh.shardCollection("herrywen.collections_1",{"_id":1})
查看返回結果

MongoDB Enterprise mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5dd8bbd8bbb4a8ac81b4b0b6")
}
shards:
{ "_id" : "shard-rs", "host" : "shard-rs/worker2:27020,worker2:27021,worker2:27022", "state" : 1 }
{ "_id" : "shard0001", "host" : "worker2:27023", "state" : 1 }
active mongoses:
"4.2.1" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Collections with active migrations:
herrywen.collections_1 started at Sat Nov 23 2019 14:15:13 GMT+0800 (CST)
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
2 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard-rs 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard-rs Timestamp(1, 0)
{ "_id" : "herrywen", "primary" : "shard-rs", "partitioned" : true, "version" : { "uuid" : UUID("56cf9d23-2f3a-4b53-8b5d-512f1f9e00c6"), "lastMod" : 1 } }
herrywen.collections_1
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard-rs 3
shard0001 4
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : 2 } on : shard0001 Timestamp(2, 0)
{ "_id" : 2 } -->> { "_id" : 28340 } on : shard-rs Timestamp(3, 1)
{ "_id" : 28340 } -->> { "_id" : 42509 } on : shard-rs Timestamp(2, 2)
{ "_id" : 42509 } -->> { "_id" : 61031 } on : shard-rs Timestamp(2, 3)
{ "_id" : 61031 } -->> { "_id" : 75200 } on : shard0001 Timestamp(3, 2)
{ "_id" : 75200 } -->> { "_id" : 94169 } on : shard0001 Timestamp(3, 3)
{ "_id" : 94169 } -->> { "_id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 4)

9.測試
a. # 修改chunk塊大小爲1M,默認64M

use config;
db.settings.find()
db.settings.save({_id:"chunksize",value:1})
MongoDB Enterprise mongos> db.settings.find()
{ "_id" : "chunksize", "value" : 1 }

b.查看當前集合中的數據量

MongoDB Enterprise mongos> use herrywen;
switched to db herrywen
MongoDB Enterprise mongos> db.collections_1.count()
0

c.爲了能看到分片效果,往herrywen.collections_1集合中寫入10000條數據

mongo --port 40000
MongoDB Enterprise mongos> for(var i=1;i<=100000;i++){
... db.collections_1.insert({"_id":i,"name":"copy"+i});
... }
打開另一個終端連接,查看是否在寫入:

[root@worker2 shard3]# mongo --port 40000
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:40000/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1101172d-02b7-4881-bc3a-1a360390db39") }
MongoDB server version: 4.2.1
Server has startup warnings:
2019-11-23T13:04:19.245+0800 I CONTROL [main]
2019-11-23T13:04:19.245+0800 I CONTROL [main] WARNING: Access control is not enabled for the database.
2019-11-23T13:04:19.245+0800 I CONTROL [main]
Read and write access to data and configuration is unrestricted.
2019-11-23T13:04:19.245+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2019-11-23T13:04:19.246+0800 I CONTROL [main]
MongoDB Enterprise mongos> use herrywen;
switched to db herrywen
MongoDB Enterprise mongos> db.collections_1.count();
41561
MongoDB Enterprise mongos> db.collections_1.count();
42971
MongoDB Enterprise mongos> db.collections_1.count();
43516
MongoDB Enterprise mongos> db.collections_1.count();
43776
MongoDB Enterprise mongos> db.collections_1.count();
44055
MongoDB Enterprise mongos> db.collections_1.count();
44291
MongoDB Enterprise mongos> db.collections_1.count();
44541
MongoDB Enterprise mongos> db.collections_1.count();
44775
MongoDB Enterprise mongos> db.collections_1.count();
45012
MongoDB Enterprise mongos> db.collections_1.count();
45257
MongoDB Enterprise mongos> db.collections_1.count();
45470

d.查看到數據也寫到其他分片中去了

MongoDB Enterprise mongos> sh.status();
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5dd8bbd8bbb4a8ac81b4b0b6")
}
shards:
{ "_id" : "shard-rs", "host" : "shard-rs/worker2:27020,worker2:27021,worker2:27022", "state" : 1 }
{ "_id" : "shard0001", "host" : "worker2:27023", "state" : 1 }
active mongoses:
"4.2.1" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
Collections with active migrations:
herrywen.collections_1 started at Sat Nov 23 2019 14:15:13 GMT+0800 (CST)
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
2 : Success
databases:
{ "_id" : "config", "primary" : "config", "partitioned" : true }
config.system.sessions
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard-rs 1
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard-rs Timestamp(1, 0)
{ "_id" : "herrywen", "primary" : "shard-rs", "partitioned" : true, "version" : { "uuid" : UUID("56cf9d23-2f3a-4b53-8b5d-512f1f9e00c6"), "lastMod" : 1 } }
herrywen.collections_1
shard key: { "_id" : 1 }
unique: false
balancing: true
chunks:
shard-rs 3
shard0001 4
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : 2 } on : shard0001 Timestamp(2, 0)
{ "_id" : 2 } -->> { "_id" : 28340 } on : shard-rs Timestamp(3, 1)
{ "_id" : 28340 } -->> { "_id" : 42509 } on : shard-rs Timestamp(2, 2)
{ "_id" : 42509 } -->> { "_id" : 61031 } on : shard-rs Timestamp(2, 3)
{ "_id" : 61031 } -->> { "_id" : 75200 } on : shard0001 Timestamp(3, 2)
{ "_id" : 75200 } -->> { "_id" : 94169 } on : shard0001 Timestamp(3, 3)
{ "_id" : 94169 } -->> { "_id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 4)

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章