MongoDB分片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB分片相关的知识,希望对你有一定的参考价值。

简介:
分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程。将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移)。通过一个名为mongos的路由进程进行操作,mongos知道数据和片的对应关系(通过配置服务器)。大部分使用场景都是解决磁盘空间的问题,对于写入有可能会变差,查询则尽量避免跨分片查询。
使用分片的时机:
1,机器的磁盘不够用了。使用分片解决磁盘空间的问题。
2,单个mongod已经不能满足写数据的性能要求。通过分片让写压力分散到各个分片上面,使用分片服务器自身的资源。
3,想把大量数据放到内存里提高性能。和上面一样,通过分片使用分片服务器自身的资源。
分片架构图:
技术分享图片

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

----------------------某节点内存不足时,从其他节点分配内存------------------------
[[email protected] bin]# sysctl -w vm.zone_reclaim_mode=0
vm.zone_reclaim_mode = 0
[[email protected] bin]# echo never > /sys/kernel/mm/transparent_hugepage/enabled
[[email protected] bin]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[[email protected] bin]# mongod -f mongodb1.conf
[[email protected] bin]# mongo --port 37017
[[email protected] bin]# cp -p mongodb1.conf mongodb2.conf
[[email protected] 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

-----------------------分片服务器-----------------------------------
[[email protected] bin]# cp -p mongodb2.conf mongodb3.conf
[[email protected] 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

[[email protected] bin]# mongod -f mongodb2.conf
[[email protected] bin]# mongod -f mongodb3.conf
[[email protected] bin]# ./mongos --help
----------------------------启动路由服务器-------------------------------------
[[email protected] bin]# ./mongos --port 27017 --fork --logpath=/usr/local/mongodb/bin/route.log --configdb 192.168.120.129:37017 --chunkSize 1

-------------------------------启用分片服务器-----------------------------------------
[[email protected] 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" ] }
#连接配置服务器
[[email protected] bin]# mongo --port 37017
configsvr> use config
switched to db config
configsvr> show collections

[[email protected] bin]# cd /usr/local/mongodb/bin/
[[email protected] bin]# cp -p mongodb3.conf mongodb4.conf
[[email protected] 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

#启动
[[email protected] bin]# mongod -f mongodb4.conf
[[email protected] 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

以上是关于MongoDB分片的主要内容,如果未能解决你的问题,请参考以下文章

mongoDB分片集群

MongoDB——MongoDB分片集群(Sharded Cluster)两种搭建方式

Docker——基于Docker搭建MongoDB分片集群

Mongodb集群架构之分片架构

MongoDB分片搭建

MongoDB——MongoDB分片集群(Sharded Cluster)