本文共 6266 字,大约阅读时间需要 20 分钟。
简介:
分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程。将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处理更大的负载。基本思想就是将集合切成小块,这些块分散到若干片里,每个片只负责总数据的一部分,最后通过一个均衡器来对各个分片进行均衡(数据迁移)。通过一个名为mongos的路由进程进行操作,mongos知道数据和片的对应关系(通过配置服务器)。大部分使用场景都是解决磁盘空间的问题,对于写入有可能会变差,查询则尽量避免跨分片查询。使用分片的时机:1,机器的磁盘不够用了。使用分片解决磁盘空间的问题。2,单个mongod已经不能满足写数据的性能要求。通过分片让写压力分散到各个分片上面,使用分片服务器自身的资源。3,想把大量数据放到内存里提高性能。和上面一样,通过分片使用分片服务器自身的资源。分片架构图:具体操作:
------------------------------安装MongoDB 3.2-------------------------------[root@localhost ~]# mkdir /abc[root@localhost ~]# mount.cifs //192.168.100.1/rhel7 /abcPassword for root@//192.168.100.1/rhel7: [root@localhost ~]# cd /abc[root@localhost abc]# cd MongoDB/[root@localhost MongoDB]# lsmongodb-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.confport=37017
dbpath=/data/mongodb/mongodb1logpath=/data/mongodb/logs/mongodb1.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1configsvr=true----------------------某节点内存不足时,从其他节点分配内存------------------------
[root@localhost bin]# sysctl -w vm.zone_reclaim_mode=0vm.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.confport=47017
dbpath=/data/mongodb/mongodb2logpath=/data/mongodb/logs/mongodb2.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=true-----------------------分片服务器-----------------------------------
[root@localhost bin]# cp -p mongodb2.conf mongodb3.conf[root@localhost bin]# vim mongodb3.confport=47018
dbpath=/data/mongodb/mongodb3logpath=/data/mongodb/logs/mongodb3.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=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]# mongoMongoDB shell version: 3.2.1connecting to: testServer 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 dbsconfig 0.031GBmongos> 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 dbsconfig 0.031GBmongos> use kgcswitched to db kgcmongos> 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 dbsconfig 0.031GBkgc 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 kgcswitched to db kgcmongos> 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 37017configsvr> use configswitched to db configconfigsvr> show collections[root@localhost bin]# cd /usr/local/mongodb/bin/
[root@localhost bin]# cp -p mongodb3.conf mongodb4.conf[root@localhost bin]# vim mongodb4.confport=47019
dbpath=/data/mongodb/mongodb4logpath=/data/mongodb/logs/mongodb4.loglogappend=truefork=truemaxConns=5000storageEngine=mmapv1shardsvr=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 4shard0001 4shard0002 3#删除分片mongos> use adminswitched to db adminmongos> db.runCommand({"removeshard":"192.168.120.129:47019"})#查看状态mongos> sh.status()chunks:shard0000 6shard0001 5转载于:https://blog.51cto.com/13756916/2175916