MongoDB分片

簡介:
分片(sharding)是指將數據庫拆分,將其分散在不同的機器上的過程。將數據分散到不同的機器上,不需要功能強大的服務器就可以存儲更多的數據和處理更大的負載。基本思想就是將集合切成小塊,這些塊分散到若干片裏,每個片只負責總數據的一部分,最後通過一個均衡器來對各個分片進行均衡(數據遷移)。通過一個名爲mongos的路由進程進行操作,mongos知道數據和片的對應關係(通過配置服務器)。大部分使用場景都是解決磁盤空間的問題,對於寫入有可能會變差,查詢則儘量避免跨分片查詢。
使用分片的時機:
1,機器的磁盤不夠用了。使用分片解決磁盤空間的問題。
2,單個mongod已經不能滿足寫數據的性能要求。通過分片讓寫壓力分散到各個分片上面,使用分片服務器自身的資源。
3,想把大量數據放到內存裏提高性能。和上面一樣,通過分片使用分片服務器自身的資源。
分片架構圖:
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/
-----------------------------------配置服務器-------------------------------------
[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

----------------------某節點內存不足時,從其他節點分配內存------------------------
[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" }
-------------------------------分片功能------------------------------------------
#創建kgc庫,寫入數據
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
#查看數據前五行
mongos> db.users.find().limit(5)
#查看數據庫分片信息
mongos> sh.status()
databases:
{ "_id" : "kgc", "primary" : "shard0000", "partitioned" : false }
#啓用數據庫分片
mongos> sh.enableSharding("kgc")
{ "ok" : 1 }
mongos> sh.status()
databases:
{ "_id" : "kgc", "primary" : "shard0000", "partitioned" : true }
#對users表創建索引
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 })
#創建索引
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")
#查看狀態
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"})
#查看狀態
mongos> sh.status()
chunks:
shard0000 6
shard0001 5

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章