MongoDB分片
简介:
分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程。将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移)。通过一个名为mongos的路由进程进行操作,mongos知道数据和片的对应关系(通过配置服务器)。大部分使用场景都是解决磁盘空间的问题,对于写入有可能会变差,查询则尽量避免跨分片查询。
使用分片的时机:
1,机器的磁盘不够用了。使用分片解决磁盘空间的问题。
2,单个mongod已经不能满足写数据的性能要求。通过分片让写压力分散到各个分片上面,使用分片服务器自身的资源。
3,想把大量数据放到内存里提高性能。和上面一样,通过分片使用分片服务器自身的资源。
分片架构图:
创新互联从2013年开始,是专业互联网技术服务公司,拥有项目成都网站建设、做网站网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元昌乐做网站,已为上家服务,为昌乐各地企业和个人服务,联系电话:028-86922220
具体操作:
------------------------------安装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
当前名称:MongoDB分片
网页路径:http://pwwzsj.com/article/gjijpj.html