高數(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)。
分片為應(yīng)對(duì)高吞吐量與大數(shù)據(jù)量提夠了方法
systemctl stop firewalld.service
setenforce 0| IP:172.16.10.26 | IP:172.16.10.27 | IP: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) |
群集部署的搭建思路,利用三臺(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ù)操作,步驟完全一致。
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 //解壓即完成安裝
路由服務(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
useradd -M -u 8000 -s /sbin/nologin mongo
chown -R mongo.mongo /usr/local/mongodb
chown -R mongo.mongo /data/mongodb
echo "PATH=/usr/local/mongodb/bin:$PATH" >> /etc/profile
source /etc/profile
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í)的,重啟失效
#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ù)
scp /etc/mongodb/config.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/config.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/config.conf //三臺(tái)服務(wù)器操作一致
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ù)制集

#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
scp /etc/mongodb/shard1.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/shard1.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/shard1.conf //三臺(tái)服務(wù)器操作一致
在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)。
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)
#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
scp /etc/mongodb/shard2.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/shard2.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/shard2.conf //三臺(tái)服務(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)
#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
scp /etc/mongodb/shard3.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/shard3.conf root@172.16.10.29:/etc/mongodb/
mongod -f /etc/mongodb/shard3.conf //三臺(tái)服務(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);
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
scp /etc/mongodb/mongos.conf root@172.16.10.27:/etc/mongodb/
scp /etc/mongodb/mongos.conf root@172.16.10.29:/etc/mongodb/
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ì)添加
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" })
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 })
mongos>sh.enableSharding("python")
//數(shù)據(jù)庫分片就有針對(duì)性,可以自定義需要分片的庫或者表,畢竟也不是所有數(shù)據(jù)都是需要分片操作的
創(chuàng)建索引的規(guī)則是不能一致性太高,要具有唯一性,例如序號(hào),比如性別這一類重復(fù)性太高的就不適合做索引
mongos> db.user.createIndex({"id":1}) //以”id“為索引
mongos> sh.shardCollection("python.user",{"id":1})
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)
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)