簡介:
分片(sharding)是指將數(shù)據(jù)庫拆分,將其分散在不同的機器上的過程。將數(shù)據(jù)分散到不同的機器上,不需要功能強大的服務器就可以存儲更多的數(shù)據(jù)和處理更大的負載?;舅枷刖褪菍⒓锨谐尚K,這些塊分散到若干片里,每個片只負責總數(shù)據(jù)的一部分,最后通過一個均衡器來對各個分片進行均衡(數(shù)據(jù)遷移)。通過一個名為mongos的路由進程進行操作,mongos知道數(shù)據(jù)和片的對應關(guān)系(通過配置服務器)。大部分使用場景都是解決磁盤空間的問題,對于寫入有可能會變差,查詢則盡量避免跨分片查詢。
使用分片的時機:
1,機器的磁盤不夠用了。使用分片解決磁盤空間的問題。
2,單個mongod已經(jīng)不能滿足寫數(shù)據(jù)的性能要求。通過分片讓寫壓力分散到各個分片上面,使用分片服務器自身的資源。
3,想把大量數(shù)據(jù)放到內(nèi)存里提高性能。和上面一樣,通過分片使用分片服務器自身的資源。
分片架構(gòu)圖:
具體操作:
------------------------------安裝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/
-----------------------------------配置服務器-------------------------------------
[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é)點內(nèi)存不足時,從其他節(jié)點分配內(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
-----------------------分片服務器-----------------------------------
[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
----------------------------啟動路由服務器-------------------------------------
[root@localhost bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.120.129:37017 --chunkSize 1
-------------------------------啟用分片服務器-----------------------------------------
[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下為空,沒有分片服務器
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() #有兩個分片被添加
shards:
{ "_id" : "shard0000", "host" : "192.168.120.129:47017" }
{ "_id" : "shard0001", "host" : "192.168.120.129:47018" }
-------------------------------分片功能------------------------------------------
#創(chuàng)建kgc庫,寫入數(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ù)庫分片信息
mongos> sh.status()
databases:
{ "_id" : "kgc", "primary" : "shard0000", "partitioned" : false }
#啟用數(shù)據(jù)庫分片
mongos> sh.enableSharding("kgc")
{ "ok" : 1 }
mongos> sh.status()
databases:
{ "_id" : "kgc", "primary" : "shard0000", "partitioned" : true }
#對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()
#添加標簽
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" ] }
#連接配置服務器
[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
#啟動
[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
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。