欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

什么是分片

高數(shù)據(jù)量和吞吐量的數(shù)據(jù)庫應(yīng)用會(huì)對(duì)單機(jī)的性能造成較大壓力,大的查詢量會(huì)將單機(jī)的CPU耗盡,大的數(shù)據(jù)量對(duì)單機(jī)的存儲(chǔ)壓力較大,最終會(huì)耗盡系統(tǒng)的內(nèi)存而將壓力轉(zhuǎn)移到磁盤IO上。
MongoDB分片是使用多個(gè)服務(wù)器存儲(chǔ)數(shù)據(jù)的方法,以支持巨大的數(shù)據(jù)存儲(chǔ)和對(duì)數(shù)據(jù)進(jìn)行操作。分片技術(shù)可以滿足MongoDB數(shù)據(jù)量大量增長(zhǎng)的需求,當(dāng)一臺(tái)MongoDB服務(wù)器不足以存儲(chǔ)海量數(shù)據(jù)或者不足以提供可接受的讀寫吞吐量時(shí),我們就可以通過在多臺(tái)服務(wù)器上分割數(shù)據(jù),使得數(shù)據(jù)庫系統(tǒng)能存儲(chǔ)和處理更多的數(shù)據(jù)。

創(chuàng)新互聯(lián)建站"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!創(chuàng)新互聯(lián)建站具備承接各種類型的成都網(wǎng)站建設(shè)、成都網(wǎng)站制作項(xiàng)目的能力。經(jīng)過10多年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。

MongoDB分片優(yōu)勢(shì)

分片為應(yīng)對(duì)高吞吐量與大數(shù)據(jù)量提夠了方法

  • 使用分片減少了每個(gè)分片需要處理的請(qǐng)求數(shù),因此,通過水平擴(kuò)展,群集可以提高自己的存儲(chǔ)容量。比如,當(dāng)插入一條數(shù)據(jù)時(shí),應(yīng)用只需要訪問存儲(chǔ)這條數(shù)據(jù)的分片。
  • 使用分片減少了每個(gè)分片村存儲(chǔ)的數(shù)據(jù)
    分片的優(yōu)勢(shì)在于提供類似線性增長(zhǎng)的架構(gòu),提高數(shù)據(jù)可用性,提高大型數(shù)據(jù)庫查詢服務(wù)器的性能。當(dāng)MongoDB單點(diǎn)數(shù)據(jù)庫服務(wù)器存儲(chǔ)成為瓶頸、單點(diǎn)數(shù)據(jù)庫服務(wù)器的性能成為瓶頸或需要部署大型應(yīng)用以充分利用內(nèi)存時(shí),可以使用分片技術(shù)。

    MongoDB分片群集的組成

  • Shard:分片服務(wù)器,用于存儲(chǔ)實(shí)際的數(shù)據(jù)塊,實(shí)際生產(chǎn)環(huán)境中一個(gè)shard server 角色可以由幾臺(tái)服務(wù)器組成一個(gè)Peplica Set 承擔(dān),防止主機(jī)單點(diǎn)故障。
  • Config Server:配置服務(wù)器,存儲(chǔ)了整個(gè)分片群集的配置信息,其中包括chunk信息。
  • Routers:前端路由,客戶端由此接入,且讓整個(gè)群集看上去像單一數(shù)據(jù)庫,前端應(yīng)用可以透明使用。

環(huán)境準(zhǔn)備

  • 系統(tǒng)版本:CenTos 7
  • 軟件版本:MongoDB4.0
  • 關(guān)閉防火墻及selinux
    systemctl stop firewalld.service
    setenforce 0
IP:172.16.10.26IP:172.16.10.27IP:172.16.10.29
mongos(27017) mongos(27017) mongos(27017)
config(30000) config(30000) config(30000)
shard1主節(jié)點(diǎn)(40001) shard1副節(jié)點(diǎn)(40001) shard1仲裁節(jié)點(diǎn)(40001)
shard2仲裁節(jié)點(diǎn)(40002) shard2主節(jié)點(diǎn)(40002) shard2副節(jié)點(diǎn)(40002)
shard1副節(jié)點(diǎn)(40003) shard1仲裁節(jié)點(diǎn)(40003) shard1主節(jié)點(diǎn)(40003)

部署MongoDB分片群集

群集部署的搭建思路,利用三臺(tái)服務(wù)器,分別安裝mongodb數(shù)據(jù)庫,每臺(tái)服務(wù)器創(chuàng)建五個(gè)實(shí)例(mongos、configs、shard1、shard2、shard3)。三臺(tái)不同的服務(wù)器上的相同名稱的實(shí)例,創(chuàng)建為一個(gè)復(fù)制集,分別包括主節(jié)點(diǎn),副節(jié)點(diǎn),仲裁節(jié)點(diǎn)。mongos不需創(chuàng)建復(fù)制集,config不需指定主副節(jié)點(diǎn)及仲裁節(jié)點(diǎn),但是要?jiǎng)?chuàng)建復(fù)制集。三臺(tái)服務(wù)器的操作步驟略有差別,但是大多是都是重復(fù)操作,步驟完全一致。

安裝MongoDB數(shù)據(jù)庫

安裝支持軟件和mongodb
yum install openssl-devel -y
tar zxf mongodb-linux-x86_64-rhel70-4.0.0.tgz -C /usr/local
mv /usr/local/mongodb-linux-x86_64-rhel70-4.0.0 /usr/local/mongodb  //解壓即完成安裝
創(chuàng)建數(shù)據(jù)存儲(chǔ)目錄和日志存儲(chǔ)目錄

路由服務(wù)器不存儲(chǔ)數(shù)據(jù),因此就不需要?jiǎng)?chuàng)建數(shù)據(jù)存儲(chǔ)目錄,只需創(chuàng)建config、shard1、shaed2、shard3即可,日志文件創(chuàng)建完成之后還需要給予權(quán)限。

mkdir -p /data/mongodb/logs/
mkdir /etc/mongodb/
mkdir /data/mongodb/config/
mkdir /data/mongodb/shard{1,2,3}
touch /data/mongodb/logs/shard{1,2,3}.log
touch /data/mongodb/logs/mongos.log
touch /data/mongodb/logs/config.log
chmod 777 /data/mongodb/logs/*.log
創(chuàng)建管理用戶,修改目錄權(quán)限
useradd -M -u 8000 -s /sbin/nologin mongo
chown -R mongo.mongo /usr/local/mongodb
chown -R mongo.mongo /data/mongodb
設(shè)置環(huán)境變量
echo "PATH=/usr/local/mongodb/bin:$PATH" >> /etc/profile
source /etc/profile
系統(tǒng)內(nèi)存優(yōu)化
ulimit -n 25000
ulimit -u 25000
sysctl -w vm.zone_reclaim_mode=0
echo never > /sys/kernel/mm/transparent_hugepage/enabled
echo never > /sys/kernel/mm/transparent_hugepage/defrag  //*注意*這些優(yōu)化都是臨時(shí)的,重啟失效

部署配置服務(wù)器

創(chuàng)建配置文件
#vim /etc/mongodb/config.conf
pidfilepath = /data/mongodb//logs/config.pid           //pid文件位置
dbpath = /data/mongodb/config/                             //數(shù)據(jù)文件存放位置
logpath = /data/mongodb//logs/config.log               //日志文件位置
logappend = true                 
bind_ip = 0.0.0.0                                                     //監(jiān)聽地址
port = 30000                                                           //端口號(hào)
fork = true 
replSet=configs                                                      //復(fù)制集名稱
configsvr = true
maxConns=20000                                                  //最大連接數(shù)
將配置文件發(fā)送到其他服務(wù)器
scp /etc/mongodb/config.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/config.conf root@172.16.10.29:/etc/mongodb/

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

啟動(dòng)config實(shí)例
mongod -f /etc/mongodb/config.conf  //三臺(tái)服務(wù)器操作一致
配置復(fù)制集(任一臺(tái)操作即可)
mongo --port 30000                    //建議三臺(tái)服務(wù)器都進(jìn)入數(shù)據(jù)庫,方便查看角色變更
config={_id:"configs",members:[{_id:0,host:"172.16.10.26:30000"},{_id:1,host:"172.16.10.27:30000"},{_id:2,host:"172.16.10.29:30000"}]}    //創(chuàng)建復(fù)制集
rs.initiate(config)                //初始化復(fù)制集

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道
MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

部署shard1分片服務(wù)器

創(chuàng)建配置文件
#vim /etc/mongodb/shard1.conf
pidfilepath = /data/mongodb//logs/shard1.pid
dbpath = /data/mongodb/shard1/
logpath = /data/mongodb//logs/shard1.log
logappend = true
journal = true
quiet = true
bind_ip = 0.0.0.0
port = 40001
fork = true
replSet=shard1
shardsvr = true
maxConns=20000
將配置文件發(fā)送到其他服務(wù)器
scp /etc/mongodb/shard1.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/shard1.conf root@172.16.10.29:/etc/mongodb/
啟動(dòng)shard1實(shí)例
mongod -f /etc/mongodb/shard1.conf  //三臺(tái)服務(wù)器操作一致
配置shard1復(fù)制集

在shard分片服務(wù)器的創(chuàng)建中,需要注意的點(diǎn)是,不是在任一臺(tái)服務(wù)器上創(chuàng)建都能成功的,如果選擇在預(yù)先設(shè)置為仲裁節(jié)點(diǎn)的服務(wù)器上創(chuàng)建復(fù)制集會(huì)報(bào)錯(cuò)。以shard1分片服務(wù)器為例,可以在172.16.10.26和172.16.10.27服務(wù)器上創(chuàng)建復(fù)制集,在172.16.10.29上創(chuàng)建則會(huì)失敗,因?yàn)樵趶?fù)制集創(chuàng)建之前,172.16.10.29已經(jīng)被設(shè)置為仲裁節(jié)點(diǎn)。
MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

mongo --port 40001    //建議三臺(tái)服務(wù)器都進(jìn)入數(shù)據(jù)庫,方便查看角色變更
use admin
config={_id:"shard1",members:[{_id:0,host:"172.16.10.26:40001",priority:2},{_id:1,host:"172.16.10.27:40001",priority:1},{_id:2,host:"172.16.10.29:40001",arbiterOnly:true}]}
rs.initiate(config)

部署shard2分片服務(wù)器

創(chuàng)建配置文件
#vim /etc/mongodb/shard2.conf
pidfilepath = /data/mongodb//logs/shard2.pid
dbpath = /data/mongodb/shard2/
logpath = /data/mongodb//logs/shard2.log
logappend = true
journal = true
quiet = true
bind_ip = 0.0.0.0
port = 40002
fork = true
replSet=shard2
shardsvr = true
maxConns=20000
將配置文件發(fā)送到其他服務(wù)器
scp /etc/mongodb/shard2.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/shard2.conf root@172.16.10.29:/etc/mongodb/
啟動(dòng)shard2實(shí)例
mongod -f /etc/mongodb/shard2.conf  //三臺(tái)服務(wù)器操作一致
配置shard復(fù)制集(非仲裁節(jié)點(diǎn)服務(wù)器)
mongo --port 40002    //建議三臺(tái)服務(wù)器都進(jìn)入數(shù)據(jù)庫,方便查看角色變更
use admin
config={_id:"shard2",members:[{_id:0,host:"172.16.10.26:40002",arbiterOnly:true},{_id:1,host:"172.16.10.27:40002",priority:2},{_id:2,host:"172.16.10.29:40002",priority:1}]}
rs.initiate(config)

部署shard3分片服務(wù)器

創(chuàng)建配置文件
#vim /etc/mongodb/shard3.conf
pidfilepath = /data/mongodb//logs/shard3.pid
dbpath = /data/mongodb/shard3/
logpath = /data/mongodb//logs/shard3.log
logappend = true
journal = true
quiet = true
bind_ip = 0.0.0.0
port = 40003
fork = true
replSet=shard3
shardsvr = true
maxConns=20000
將配置文件發(fā)送到其他服務(wù)器
scp /etc/mongodb/shard3.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/shard3.conf root@172.16.10.29:/etc/mongodb/
啟動(dòng)shard3實(shí)例
mongod -f /etc/mongodb/shard3.conf  //三臺(tái)服務(wù)器操作一致
配置shard復(fù)制集(非仲裁節(jié)點(diǎn)服務(wù)器)
mongo --port 40003    //建議三臺(tái)服務(wù)器都進(jìn)入數(shù)據(jù)庫,方便查看角色變更
use admin
config={_id:"shard3",members:[{_id:0,host:"172.16.10.26:40003",priority:1},{_id:1,host:"172.16.10.27:40003",arbiterOnly:true},{_id:2,host:"172.16.10.29:40003",priority:2}]}
rs.initiate(config);

部署路由服務(wù)器

創(chuàng)建配置文件
pidfilepath = /data/mongodb/logs/mongos.pid
logpath=/data/mongodb/logs/mongos.log
logappend = true
bind_ip = 0.0.0.0
port = 27017
fork = true
configdb = configs/172.16.10.26:30000,172.16.10.27:30000,172.16.10.29:30000
maxConns=20000
將配置文件發(fā)送到其他服務(wù)器
scp /etc/mongodb/mongos.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/mongos.conf root@172.16.10.29:/etc/mongodb/
啟動(dòng)mongos實(shí)例
mongos -f /etc/mongodb/mongos.conf  //三臺(tái)服務(wù)器操作一致*注意*這里是“mongos”而非“mongod”
啟用分片功能
mongo    //因?yàn)槟J(rèn)端口即是27017,所以此處不接端口號(hào)
mongos> use admin
mongos> sh.addShard("shard1/172.16.10.26:40001,172.16.10.27:40001,172.16.10.29:40001")
mongos> sh.addShard("shard2/172.16.10.26:40002,172.16.10.27:40002,172.16.10.29:40002")
mongos> sh.status()           //查看群集狀態(tài)
//此處先添加兩各分片服務(wù)器,還有一個(gè),待會(huì)添加

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

測(cè)試服務(wù)器分片功能

設(shè)置分片chunk大小
mongos> use config
switched to db config
mongos> db.settings.save({"_id":"chunksize","value":1})   //設(shè)置塊大小為1M是方便實(shí)驗(yàn),不然就需要插入海量數(shù)據(jù)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "chunksize" })
模擬寫入數(shù)據(jù)
mongos> use python
switched to db python
mongos> show collections
mongos> for(i=1;i<=50000;i++){db.user.insert({"id":i,"name":"jack"+i})}
//在python庫的user表中循環(huán)寫入五萬條數(shù)據(jù)
WriteResult({ "nInserted" : 1 })
啟用數(shù)據(jù)庫分片
mongos>sh.enableSharding("python")
//數(shù)據(jù)庫分片就有針對(duì)性,可以自定義需要分片的庫或者表,畢竟也不是所有數(shù)據(jù)都是需要分片操作的

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

為表創(chuàng)建的索引

創(chuàng)建索引的規(guī)則是不能一致性太高,要具有唯一性,例如序號(hào),比如性別這一類重復(fù)性太高的就不適合做索引

mongos> db.user.createIndex({"id":1})   //以”id“為索引

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

啟用表分片
mongos> sh.shardCollection("python.user",{"id":1})

MongoDB(4.0)分片——大數(shù)據(jù)的處理之道

查看分片情況
mongos> sh.status()
--- Sharding Status --- 
            ···省略內(nèi)容
  shards:
        {  "_id" : "shard1",  "host" : "shard1/172.16.10.26:40001,172.16.10.27:40001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/172.16.10.27:40002,172.16.10.29:40002",  "state" : 1 }
            ···省略內(nèi)容
                        chunks:
                                shard1  3
                                shard2  3
                        { "id" : { "$minKey" : 1 } } -->> { "id" : 9893 } on : shard1 Timestamp(2, 0) 
                        { "id" : 9893 } -->> { "id" : 19786 } on : shard1 Timestamp(3, 0) 
                        { "id" : 19786 } -->> { "id" : 29679 } on : shard1 Timestamp(4, 0) 
                        { "id" : 29679 } -->> { "id" : 39572 } on : shard2 Timestamp(4, 1) 
                        { "id" : 39572 } -->> { "id" : 49465 } on : shard2 Timestamp(1, 4) 
                        { "id" : 49465 } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 5) 
手動(dòng)添加分片服務(wù)器,查看分片情況是否發(fā)生變化
mongos> use admin
switched to db admin
mongos> sh.addShard("172.16.10.26:40003,172.16.10.27:40003,172.16.10.29:40003")
mongos> sh.status()
--- Sharding Status --- 
           ···省略內(nèi)容
  shards:
        {  "_id" : "shard1",  "host" : "shard1/172.16.10.26:40001,172.16.10.27:40001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/172.16.10.27:40002,172.16.10.29:40002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/172.16.10.26:40003,172.16.10.29:40003",  "state" : 1 }
           ···省略內(nèi)容
                        chunks:
                                shard1  2
                                shard2  2
                                shard3  2
                        { "id" : { "$minKey" : 1 } } -->> { "id" : 9893 } on : shard3 Timestamp(6, 0) 
                        { "id" : 9893 } -->> { "id" : 19786 } on : shard1 Timestamp(6, 1) 
                        { "id" : 19786 } -->> { "id" : 29679 } on : shard1 Timestamp(4, 0) 
                        { "id" : 29679 } -->> { "id" : 39572 } on : shard3 Timestamp(5, 0) 
                        { "id" : 39572 } -->> { "id" : 49465 } on : shard2 Timestamp(5, 1) 
                        { "id" : 49465 } -->> { "id" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 5) 

服務(wù)器又對(duì)數(shù)據(jù)進(jìn)行重新分片,當(dāng)你再次移除一個(gè)分片服務(wù)器,此時(shí)又會(huì)對(duì)數(shù)據(jù)再次進(jìn)行分片處理,MongoDB對(duì)數(shù)據(jù)的處理非常靈活

網(wǎng)站欄目:MongoDB(4.0)分片——大數(shù)據(jù)的處理之道
網(wǎng)頁地址:http://chinadenli.net/article18/gespdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷做網(wǎng)站標(biāo)簽優(yōu)化營銷型網(wǎng)站建設(shè)App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)