- 一、副本的概念
- 1.主從復制和副本集區別
- 2、副本集的兩種型別
- 3、三種角色:
- 4、缺點
- 二、分片概念
- 1.資料塊
- 2.平衡器
- 三、分片集群包含的組件
- 四、構建分片群集
- 拓撲
- 1.解壓軟體包
- 2.創建日志檔案及資料目錄
- 3.進行一些快取優化
- 4.撰寫組態檔
- 5.建立軟連接,便于呼叫命令
- 6.啟動實體
- 7.配置分片服務器
- 8.啟動路由服務器
- 9.啟用分片服務器
- 10 分片功能管理初步了解
- 五、分片管理示例
一、副本的概念
1.主從復制和副本集區別
主從集群和制本集最大的區別就是副本集沒有固定的“主節點";整個集群會選出一個主節點當其掛掉后,又在剩下的從節點中選中其他節點為"主節點",副本集總有一個活躍點(主primary和一 個或多個備份節點(從secondary),
2、副本集的兩種型別
副本集有兩種型別三種角色
兩種型別:
主節點(Primary) 型別:資料操作的主要連接點,可讀寫.
次要(輔助、從)節點(Secondaries) 型別:資料冗余備份節點,可以讀或選舉,
3、三種角色:
主要成員(Primary) :主要接收所有寫操作,就是主節點,
副本成員(Replicate) :從主節點通過復制操作以維護相同的資料集,即備份資料,可寫操作,但可以讀操作但需要配置,是默認的一種從節點型別,
仲裁者(Arbiter) :不保留任何資料的副本,只具有投票選舉作用,當然也可以將仲裁服務器維護為副本集的一部分,即副本成員同時也可以是仲裁者,也是一種從節點型別,
4、缺點
相當于只從復制 master宕機后就涼了 ,為了實作高可用,就不得不介紹一下分片的概念了
二、分片概念
1.資料塊
- 塊也叫區間,可能存在一分片一區間和一分片多區間兩種情況,
-
一分片一區間:資料不會在片之間自動移動來保持分片的資料的均勻性,需要手動拆分分片來移動資料,
-
一分片多區間情況:一個資料塊默認64MB,當資料塊達到64MB時就會創建新的塊,當然前提是當前的粒度還允許再拆分,平衡器會保證每個分片資料塊的均勻,但是移動塊也遵循分片的原則,塊之間的資料集不能有交集,
比如一個塊[50-100)現在拆分成兩個塊,那么默認會拆分成[50-75)[75-100)兩個塊,如果當前分片塊比其它分片的塊大于9那么可能[75-100)改塊會被移動到新的分片當中,
2.平衡器
平衡器(balancer)負責資料的遷移,它會周期性地檢查分片間是否存在不平衡,如果存在,則會開啟塊的遷移,不平衡的表現指,一個分片明顯比其他分片擁有更多的塊,假如有一些集合達到了閾值,平衡器則會開始做塊遷移,它會從負載比較比較大的分片中選擇一個塊,并詢問該分片是否需要在遷移之前對塊進行拆分,完成必要的拆分后,就會將塊遷移至數量比較少的機器上,
一、副本的概念
主從復制和副本集區別
主從集群和制本集最大的區別就是副本集沒有固定的“主節點";整個集群會選出一個主節點當其掛掉后,又在剩下的從節點中選中其他節點為"主節點",副本集總有一個活躍點(主primary和一 個或多個備份節點(從secondary),
1、副本集的兩種型別
副本集有兩種型別三種角色
兩種型別:
主節點(Primary) 型別:資料操作的主要連接點,可讀寫.
次要(輔助、從)節點(Secondaries) 型別:資料冗余備份節點,可以讀或選舉,
2、三種角色:
主要成員(Primary) :主要接收所有寫操作,就是主節點,
副本成員(Replicate) :從主節點通過復制操作以維護相同的資料集,即備份資料,可寫操作,但可以讀操作但需要配置,是默認的一種從節點型別,
仲裁者(Arbiter) :不保留任何資料的副本,只具有投票選舉作用,當然也可以將仲裁服務器維護為副本集的一部分,即副本成員同時也可以是仲裁者,也是一種從節點型別,
4、缺點
相當于只從復制 master宕機后就涼了 ,為了實作高可用,就不得不介紹一下分片的概念了
三、分片集群包含的組件
MongoDB分片群集包含以下組件: .
●分片(存盤)shard:每個分片包含分片資料的子集, 每個分片都可以部署為副本集,
●mongos (路由) : mongos充當查詢路由器,在客戶端應用程式和分片集群之間提供介面,
●config servers (“調度"的配置) : 配置服務器存盤群集的元資料和配置設定,從MongoDB 3.4開始,必須將配置服務器部署為副本集(CSRS),

四、構建分片群集
拓撲

1.解壓軟體包
[root@localhost ~]# tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/
[root@localhost ~]# mv /opt/mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb ##移動到/usr/local下便于管理
2.創建日志檔案及資料目錄
[root@localhost ~]# mkdir -p /data/mongodb/mongodb{1,2,3,4}
[root@localhost ~]# mkdir /data/mongodb/logs
[root@localhost ~]# touch /data/mongodb/logs/mongodb{1,2,3,4}.1og
[root@localhost ~]# chmod -R 777 /data/mongodb/logs/*.1og ##一定要授予777權限
3.進行一些快取優化
[root@localhost ~]# ulimit -n 25000 ##每個行程可以同時打開的最大檔案數
[root@localhost ~]# ulimit -u 25000 ##可以運行的最大并發行程數
##### 下面3條優化,當某節點記憶體不足時,從其他節點分配記憶體 ########
[root@localhost bin]# sysctl -w vm.zone_reclaim_mode=0 ##禁止大頁面記憶體的出現
vm.zone_reclaim_mode = 0
[root@localhost bin]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@localhost bin]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
4.撰寫組態檔
[root@localhost ~]# cd /usr/local/mongodb/bin/ ##組態檔就放到/usr/local/mongodb/bin/目錄下
[root@localhost bin]# vim mongodb1.conf
port=37017
dbpath=/data/mongodb/mongodb1
logpath=/data/mongodb/logs/mongodb1.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
configsvr=true ##指定這是屬于配置服務器的模式
5.建立軟連接,便于呼叫命令
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo
6.啟動實體
[root@localhost bin]# mongod -f /usr/local/mongodb/bin/mongodb1.conf
about to fork child process, waiting until server is ready for connections.
forked process: 2099
child process started successfully, parent exiting
7.配置分片服務器
[root@localhost bin]# cp -p mongodb1.conf mongodb2.conf
[root@localhost bin]# vim mongodb2.conf
port=47017 ##修改埠號
dbpath=/data/mongodb/mongodb2 ##修改資料目錄位置
logpath=/data/mongodb/logs/mongodb2.log ##修改日志檔案路徑
shardsvr=true ##configsvr改成shardsvr
[root@localhost bin]# cp -p mongodb2.conf mongodb3.conf
[root@localhost bin]# vim mongodb3.conf
port=47018 ##修改埠號
dbpath=/data/mongodb/mongodb3 ##修改資料目錄位置
logpath=/data/mongodb/logs/mongodb3.log ##修改日志檔案路徑
[root@localhost bin]# cp -p mongodb2.conf mongodb4.conf
[root@localhost bin]# vim mongodb4.conf
port=47019 ##修改埠號
dbpath=/data/mongodb/mongodb4 ##修改資料目錄位置
logpath=/data/mongodb/logs/mongodb4.log ##修改日志檔案路徑
#######################依次啟動實體####################################
[root@localhost bin]# mongod -f /usr/local/mongodb/bin/mongodb2.conf ##'啟動實體2'
about to fork child process, waiting until server is ready for connections.
forked process: 2393
child process started successfully, parent exiting
[root@localhost bin]# mongod -f /usr/local/mongodb/bin/mongodb3.conf ##'啟動實體3'
about to fork child process, waiting until server is ready for connections.
forked process: 2408
child process started successfully, parent exiting
[root@localhost bin]# mongod -f /usr/local/mongodb/bin/mongodb4.conf ##'啟動實體4'
about to fork child process, waiting until server is ready for connections.
forked process: 2423
child process started successfully, parent exiting
8.啟動路由服務器
[root@localhost bin]# ./mongos --help ##mongos是開啟路由模式的一個腳本,--help查看幫助資訊
[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.10.10:37017 --chunkSize 1
##--port指定對方訪問埠(入口),--fork后臺運行;--logpath日志檔案位置;--configdb指定處理請求的配置服務器;--chunkSize指定資料塊(最后這個配置只是用來測驗的,不加也行,默認空間是200M大小)
2020-11-09T01:36:31.297+0800 W SHARDING [main] Running a sharded cluster with fewer than 3 config servers should only be done for testing purposes and is not recommended for production.
about to fork child process, waiting until server is ready for connections.
forked process: 37738
child process started successfully, parent exiting
[root@localhost bin]# mongo ##mongo命令直接進入路由服務器(默認埠是27017)
MongoDB shell version: 3.2.1
connecting to: test
Server has startup warnings:
2020-11-09T01:36:31.307+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2020-11-09T01:36:31.307+0800 I CONTROL [main]
mongos>
9.啟用分片服務器
sh.addShard()命令添加分片服務器
[root@localhost bin]# mongo ##進入路由服務器實際上相當于進入了配置服務器
mongos> show dbs ##只能查看到一個配置config庫
config 0.031GB
mongos> sh.status() ##查看該服務器下是否有分片服務器
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5fa82c9fef682337a84b13a1")
}
shards: ##現在這個版塊下是空的,說明并沒有分片服務器
active mongoses:
"3.2.1" : 1
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:
mongos> sh.addShard("192.168.10.10:47017") ##添加分片服務器192.168.10.10:47017
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.10.10:47018") ##添加分片服務器192.168.10.10:47018
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status() ##再次查看分片集群狀態
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5fa82c9fef682337a84b13a1")
}
shards: ##在shards下多次兩個節點
{ "_id" : "shard0000", "host" : "192.168.10.10:47017" }
{ "_id" : "shard0001", "host" : "192.168.10.10:47018" }
active mongoses:
"3.2.1" : 1
balancer:
10 分片功能管理初步了解
mongos> use school ##進入school庫
mongos> for(var i=1;i<=10000;i++)db.info.insert({"id":i,"team":"member"+i}) ##插入10000條資料,用于驗證分片
mongos> db.info.find().limit(3) ##查看頭3行資料
{ "_id" : ObjectId("5fa83a28597e4c3f6bb3f3c6"), "id" : 1, "team" : "member1" }
{ "_id" : ObjectId("5fa83a29597e4c3f6bb3f3c7"), "id" : 2, "team" : "member2" }
{ "_id" : ObjectId("5fa83a29597e4c3f6bb3f3c8"), "id" : 3, "team" : "member3" }
############# 也可以通過在MongoDB外用檔案匯入資料######################
./mongoimport -d school -c info --file /opt/test.txt
#################################################################
mongos> show dbs ##插入資料后新建了一個school庫
config 0.031GB
school 0.078GB
mongos> use school ##進入school庫
mongos> show collections ##查看集合,已經有資料了
info
system.indexes
mongos> sh.status()
……省略部分
databases:
{ "_id" : "school", "primary" : "shard0000", "partitioned" : false } ##資料并沒有進行分片,提示false
mongos> sh.enableSharding("school") ##對school庫開啟分片處理
mongos> sh.status()
……省略部分
databases:
{ "_id" : "school", "primary" : "shard0000", "partitioned" : true } ##顯示ture,開啟了分片處理,但是到這一步只是開啟了對庫的分片,對集合并沒有分片
#對集合進行分片需要先創建索引
mongos> db.info.createIndex({"id":1}) ##給info集合創建以“id”為索引,從1開始
mongos> sh.shardCollection("school.info",{"id":1}) ##對集合info分片
mongos> sh.status() ##再次查看資訊,可以看出10000條資料分成了三段1~4682,4682~9364,9364~最后
……省略部分
databases:
{ "_id" : "school", "primary" : "shard0000", "partitioned" : true }
school.info
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 3
{ "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0000 Timestamp(1, 0)
{ "id" : 4682 } -->> { "id" : 9364 } on : shard0000 Timestamp(1, 1)
{ "id" : 9364 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 2)
總結:一共三個步驟,1.配置路由服務器,2.構建配置服務器,3.構建分片服務器
五、分片管理示例
集合分片
mongos> use school
mongos> sh.enableSharding("school") ##開啟school庫的分片功能
mongos> for(var i=1;i<=50000;i++)db.test.insert({"id":i,"name":"test"+i}) ##插入50000條資料
mongos> show tables ##查看集合
info
system.indexes
test
mongos> db.test.count() ##統計有5000條記錄
50000
mongos> db.test.find().limit(3) ##查看開頭三條資料,資料插入成功
{ "_id" : ObjectId("5fa8483f3a9549125843e1f8"), "id" : 1, "name" : "test1" }
{ "_id" : ObjectId("5fa8483f3a9549125843e1f9"), "id" : 2, "name" : "test2" }
{ "_id" : ObjectId("5fa8483f3a9549125843e1fa"), "id" : 3, "name" : "test3" }
mongos> db.test.createIndex({"id":1}) ##建立索引,以"id"為索引,從1開始
mongos> sh.shardCollection("school.test",{"id":1}) ##根據索引對test集合進行分片,索引建議最好是能作為主鍵的欄位(非空且唯一),例如像性別這樣的鍵就不適合做索引,除了男就是女兩種值,
mongos> sh.status() ##查看分片資訊
--- Sharding Status ---
……省略部分
shards:
{ "_id" : "shard0000", "host" : "192.168.10.10:47017" } ##192.168.10.10:47017節點的id號是shard0000
{ "_id" : "shard0001", "host" : "192.168.10.10:47018" } ##192.168.10.10:47018節點的id號是shard0001
databases:
{ "_id" : "school", "primary" : "shard0000", "partitioned" : true }
school.info ##info集合的分片資訊
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 3
shard0001 2
{ "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0001 Timestamp(2, 0)
{ "id" : 4682 } -->> { "id" : 9364 } on : shard0000 Timestamp(3, 1)
{ "id" : 9364 } -->> { "id" : 13407 } on : shard0000 Timestamp(2, 2)
{ "id" : 13407 } -->> { "id" : 18945 } on : shard0000 Timestamp(2, 3)
{ "id" : 18945 } -->> { "id" : { "$maxKey" : 1 } } on : shard0001 Timestamp(3, 0)
school.test ##test集合的分片資訊
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 6 ##6段分片放在節點00上
shard0001 5 ##5段分片放在節點01上
{ "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0001 Timestamp(2, 0) ##這段分片放在shard01節點上
{ "id" : 4682 } -->> { "id" : 9364 } on : shard0001 Timestamp(3, 0) ##這段分片也是放在shard01節點上
{ "id" : 9364 } -->> { "id" : 14046 } on : shard0001 Timestamp(4, 0)
{ "id" : 14046 } -->> { "id" : 18728 } on : shard0001 Timestamp(5, 0)
{ "id" : 18728 } -->> { "id" : 23410 } on : shard0001 Timestamp(6, 0)
{ "id" : 23410 } -->> { "id" : 28092 } on : shard0000 Timestamp(6, 1) ##這段分片放在shard00節點上
{ "id" : 28092 } -->> { "id" : 32774 } on : shard0000 Timestamp(1, 6)
{ "id" : 32774 } -->> { "id" : 37456 } on : shard0000 Timestamp(1, 7)
{ "id" : 37456 } -->> { "id" : 42138 } on : shard0000 Timestamp(1, 8)
{ "id" : 42138 } -->> { "id" : 46820 } on : shard0000 Timestamp(1, 9)
{ "id" : 46820 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 10)
添加標簽
mongos> sh.addShardTag("shard0000","slave1") ##shard0000是節點id號,通過sh.status()可以查到,設定給節點標簽slave1
mongos> sh.addShardTag("shard0001","slave2") ##給shard0001節點打上標簽salve2
mongos> sh.status() ##查看分片資訊
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5fa82c9fef682337a84b13a1")
}
shards:
{ "_id" : "shard0000", "host" : "192.168.10.10:47017", "tags" : [ "slave1" ] } ##后面追加了標簽資訊
{ "_id" : "shard0001", "host" : "192.168.10.10:47018", "tags" : [ "slave2" ] }
……省略部分
添加新節點,查看資料分片情況
注意點:我們所有的操作都通過路由服務器請求配置服務器,有配置服務器完成的
[root@localhost ~]# mongo --port 37017 ##進入配置服務器
configsvr> use config ##進入config庫
switched to db config
configsvr> show tables ##可看到這幾個集合,我們所有的資訊都可以在這些集合中看到
……省略部分
chunks ##分片資料塊資訊
collections ##集合資訊
databases ##資料庫資訊
……省略部分
configsvr> db.databases.find() ##可以查看到我們新建的資料庫
{ "_id" : "school", "primary" : "shard0000", "partitioned" : true }
configsvr> db.collections.find() ##可以查看集合資訊
configsvr> db.databases.find() ##可以查看分片(資料塊)資訊
[root@localhost ~]# mongo ##進入路由服務器
mongos> sh.addShard("192.168.10.10:47019") ##再次添加一個分片服務器(上個實驗中創建好了的實體)
mongos> sh.status() ##查看分片資訊
-……省略部分
shards: ###添加分片節點后,自動更新資訊,可以看到多出了一個分片節點
{ "_id" : "shard0000", "host" : "192.168.10.10:47017", "tags" : [ "slave1" ] }
{ "_id" : "shard0001", "host" : "192.168.10.10:47018", "tags" : [ "slave2" ] }
{ "_id" : "shard0002", "host" : "192.168.10.10:47019" }
……省略部分
databases: ##由下面資訊可以看出,分片資料段重新進行了分配,新加的節點與舊節點之間平均分配舊資料
{ "_id" : "school", "primary" : "shard0000", "partitioned" : true }
…………省略部分
school.test
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 4
shard0001 4
shard0002 3
{ "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0002 Timestamp(9, 0)
{ "id" : 4682 } -->> { "id" : 9364 } on : shard0001 Timestamp(9, 1)
{ "id" : 9364 } -->> { "id" : 14046 } on : shard0001 Timestamp(4, 0)
{ "id" : 14046 } -->> { "id" : 18728 } on : shard0001 Timestamp(5, 0)
{ "id" : 18728 } -->> { "id" : 23410 } on : shard0001 Timestamp(6, 0)
{ "id" : 23410 } -->> { "id" : 28092 } on : shard0002 Timestamp(7, 0)
{ "id" : 28092 } -->> { "id" : 32774 } on : shard0002 Timestamp(8, 0)
{ "id" : 32774 } -->> { "id" : 37456 } on : shard0000 Timestamp(8, 1)
{ "id" : 37456 } -->> { "id" : 42138 } on : shard0000 Timestamp(1, 8)
{ "id" : 42138 } -->> { "id" : 46820 } on : shard0000 Timestamp(1, 9)
{ "id" : 46820 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 10)
洗掉分片節點
mongos> use admin ###需要先進admin庫
mongos> db.runCommand({"removeshard":"192.168.10.10:47019"}) ##洗掉分片節點
mongos> sh.status()
--- Sharding Status ---
……省略部分
shards:
{ "_id" : "shard0000", "host" : "192.168.10.10:47017", "tags" : [ "slave1" ] }
{ "_id" : "shard0001", "host" : "192.168.10.10:47018", "tags" : [ "slave2" ] }
{ "_id" : "shard0002", "host" : "192.168.10.10:47019", "draining" : true } ##顯示這臺節點停機
……省略部分
databases: ##由下面資訊可以看出,洗掉后,資料再次從新分配給存活的兩臺節點
{ "_id" : "school", "primary" : "shard0000", "partitioned" : true }
school.test
shard key: { "id" : 1 }
unique: false
balancing: true
chunks:
shard0000 6
shard0001 5
{ "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0000 Timestamp(10, 0)
{ "id" : 4682 } -->> { "id" : 9364 } on : shard0001 Timestamp(9, 1)
{ "id" : 9364 } -->> { "id" : 14046 } on : shard0001 Timestamp(4, 0)
{ "id" : 14046 } -->> { "id" : 18728 } on : shard0001 Timestamp(5, 0)
{ "id" : 18728 } -->> { "id" : 23410 } on : shard0001 Timestamp(6, 0)
{ "id" : 23410 } -->> { "id" : 28092 } on : shard0001 Timestamp(11, 0)
{ "id" : 28092 } -->> { "id" : 32774 } on : shard0000 Timestamp(12, 0)
{ "id" : 32774 } -->> { "id" : 37456 } on : shard0000 Timestamp(8, 1)
{ "id" : 37456 } -->> { "id" : 42138 } on : shard0000 Timestamp(1, 8)
{ "id" : 42138 } -->> { "id" : 46820 } on : shard0000 Timestamp(1, 9)
{ "id" : 46820 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 10)
總結:由此可以看出,MongoDB資料庫非常靈活,無論洗掉或者添加分片節點,資料自動進行劃分,十分有利于服務器的橫向擴展,因此在大資料處理上非常受歡迎
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/209068.html
標籤:其他
