溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

MongoDB分片

發(fā)布時(shí)間:2020-07-31 21:03:46 來(lái)源:網(wǎng)絡(luò) 閱讀:378 作者:恒小白 欄目:MongoDB數(shù)據(jù)庫(kù)

簡(jiǎn)介:
分片(sharding)是指將數(shù)據(jù)庫(kù)拆分,將其分散在不同的機(jī)器上的過(guò)程。將數(shù)據(jù)分散到不同的機(jī)器上,不需要功能強(qiáng)大的服務(wù)器就可以存儲(chǔ)更多的數(shù)據(jù)和處理更大的負(fù)載。基本思想就是將集合切成小塊,這些塊分散到若干片里,每個(gè)片只負(fù)責(zé)總數(shù)據(jù)的一部分,最后通過(guò)一個(gè)均衡器來(lái)對(duì)各個(gè)分片進(jìn)行均衡(數(shù)據(jù)遷移)。通過(guò)一個(gè)名為mongos的路由進(jìn)程進(jìn)行操作,mongos知道數(shù)據(jù)和片的對(duì)應(yīng)關(guān)系(通過(guò)配置服務(wù)器)。大部分使用場(chǎng)景都是解決磁盤(pán)空間的問(wèn)題,對(duì)于寫(xiě)入有可能會(huì)變差,查詢則盡量避免跨分片查詢。
使用分片的時(shí)機(jī):
1,機(jī)器的磁盤(pán)不夠用了。使用分片解決磁盤(pán)空間的問(wèn)題。
2,單個(gè)mongod已經(jīng)不能滿足寫(xiě)數(shù)據(jù)的性能要求。通過(guò)分片讓寫(xiě)壓力分散到各個(gè)分片上面,使用分片服務(wù)器自身的資源。
3,想把大量數(shù)據(jù)放到內(nèi)存里提高性能。和上面一樣,通過(guò)分片使用分片服務(wù)器自身的資源。
分片架構(gòu)圖:
MongoDB分片

具體操作:
------------------------------安裝MongoDB 3.2-------------------------------
[root@localhost ~]# mkdir /abc
[root@localhost ~]# mount.cifs //192.168.100.1/rhel7 /abc
Password for root@//192.168.100.1/rhel7:
[root@localhost ~]# cd /abc
[root@localhost abc]# cd MongoDB/
[root@localhost MongoDB]# ls
mongodb-linux-x86_64-3.2.1.tgz mongodb-linux-x86_64-rhel70-4.0.0.tgz
[root@localhost MongoDB]# tar zxvf mongodb-linux-x86_64-3.2.1.tgz -C /opt/
[root@localhost MongoDB]# cd /opt/
[root@localhost opt]# mv mongodb-linux-x86_64-3.2.1/ /usr/local/mongodb
[root@localhost opt]# cd /usr/local/mongodb/bin/
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongo /usr/bin/mongo
[root@localhost bin]# ln -s /usr/local/mongodb/bin/mongod /usr/bin/mongod
[root@localhost bin]# mongo
[root@localhost bin]# mkdir -p /data/mongodb/mongodb{1,2,3,4}
[root@localhost bin]# cd /data/mongodb/
[root@localhost mongodb]# mkdir logs
[root@localhost mongodb]# cd logs/
[root@localhost logs]# touch mongodb{1,2,3,4}.log
[root@localhost logs]# chmod 777 *.log
[root@localhost logs]# ulimit -u 25000
[root@localhost logs]# ulimit -n 25000
[root@localhost logs]# cd /usr/local/mongodb/bin/
-----------------------------------配置服務(wù)器-------------------------------------
[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

----------------------某節(jié)點(diǎn)內(nèi)存不足時(shí),從其他節(jié)點(diǎn)分配內(nèi)存------------------------
[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
[root@localhost bin]# mongod -f mongodb1.conf
[root@localhost bin]# mongo --port 37017
[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
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true

-----------------------分片服務(wù)器-----------------------------------
[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
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true

[root@localhost bin]# mongod -f mongodb2.conf
[root@localhost bin]# mongod -f mongodb3.conf
[root@localhost bin]# ./mongos --help
----------------------------啟動(dòng)路由服務(wù)器-------------------------------------
[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.120.129:37017 --chunkSize 1

-------------------------------啟用分片服務(wù)器-----------------------------------------
[root@localhost bin]# mongo
MongoDB shell version: 3.2.1
connecting to: test
Server has startup warnings:
2018-09-14T14:47:11.104+0800 I CONTROL [main] ** WARNING: You are running this process as the root user, which is not recommended.
2018-09-14T14:47:11.104+0800 I CONTROL [main]
mongos> show dbs
config 0.031GB
mongos> sh.status() #shards下為空,沒(méi)有分片服務(wù)器
mongos> sh.addShard("192.168.120.129:47017")
{ "shardAdded" : "shard0000", "ok" : 1 }
mongos> sh.addShard("192.168.120.129:47018")
{ "shardAdded" : "shard0001", "ok" : 1 }
mongos> sh.status() #有兩個(gè)分片被添加
shards:
{ "_id" : "shard0000", "host" : "192.168.120.129:47017" }
{ "_id" : "shard0001", "host" : "192.168.120.129:47018" }
-------------------------------分片功能------------------------------------------
#創(chuàng)建kgc庫(kù),寫(xiě)入數(shù)據(jù)
mongos> show dbs
config 0.031GB
mongos> use kgc
switched to db kgc
mongos> db.users.insert({"id":1,"name":"zhangsan"})
WriteResult({ "nInserted" : 1 })
mongos> for(var i=2;i<=10000;i++)db.users.insert({"id":i,"name":"jack"+i})
WriteResult({ "nInserted" : 1 })
mongos> show dbs
config 0.031GB
kgc 0.078GB
#查看數(shù)據(jù)前五行
mongos> db.users.find().limit(5)
#查看數(shù)據(jù)庫(kù)分片信息
mongos> sh.status()
databases:
{ "_id" : "kgc", "primary" : "shard0000", "partitioned" : false }
#啟用數(shù)據(jù)庫(kù)分片
mongos> sh.enableSharding("kgc")
{ "ok" : 1 }
mongos> sh.status()
databases:
{ "_id" : "kgc", "primary" : "shard0000", "partitioned" : true }
#對(duì)users表創(chuàng)建索引
mongos> db.users.createIndex({"id":1})
#表分片
mongos> sh.shardCollection("kgc.users",{"id":1})
{ "collectionsharded" : "kgc.users", "ok" : 1 }
mongos> sh.status()

------------------------------分片管理--------------------------------------
mongos> use kgc
switched to db kgc
mongos> for(var i=1;i<=50000;i++)db.users2.insert({"id":i,"name":"jerry"+i})
WriteResult({ "nInserted" : 1 })
#創(chuàng)建索引
mongos> db.users2.createIndex({"id":1})
#分片
mongos> sh.shardCollection("kgc.users2",{"id":1})
{ "collectionsharded" : "kgc.users2", "ok" : 1 }
mongos> db.users2.stats()
#添加標(biāo)簽
mongos> sh.addShardTag("shard0000","sales00")
mongos> sh.addShardTag("shard0001","sales01")
mongos> sh.status()
shards:
{ "_id" : "shard0000", "host" : "192.168.120.129:47017", "tags" : [ "sales00" ] }
{ "_id" : "shard0001", "host" : "192.168.120.129:47018", "tags" : [ "sales01" ] }
#連接配置服務(wù)器
[root@localhost bin]# mongo --port 37017
configsvr> use config
switched to db config
configsvr> show collections

[root@localhost bin]# cd /usr/local/mongodb/bin/
[root@localhost bin]# cp -p mongodb3.conf mongodb4.conf
[root@localhost bin]# vim mongodb4.conf

port=47019
dbpath=/data/mongodb/mongodb4
logpath=/data/mongodb/logs/mongodb4.log
logappend=true
fork=true
maxConns=5000
storageEngine=mmapv1
shardsvr=true

#啟動(dòng)
[root@localhost bin]# mongod -f mongodb4.conf
[root@localhost bin]# mongo
#添加分片
mongos> sh.addShard("192.168.120.129:47019")
#查看狀態(tài)
mongos> sh.status()
chunks:
shard0000 4
shard0001 4
shard0002 3
#刪除分片
mongos> use admin
switched to db admin
mongos> db.runCommand({"removeshard":"192.168.120.129:47019"})
#查看狀態(tài)
mongos> sh.status()
chunks:
shard0000 6
shard0001 5

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI