http://www.2cto.com/database/201309/240552.html

MongoDB分布式操作——分片操作
描述:
像其它分布式数据库一样,MongoDB同样支持分布式操作,且MongoDB将分布式已经集成到数据库中,其分布式体系如下图所示:
所谓的片,其实就是一个单独的MongoDB进程,它可以运行在不同的节点上,也可以运行在同一节点但监听不同的端口。同一个集合的数据可能在每个片中都存储的有数据,但它们在逻辑上整合成一个整体。分片的依据往往是根据表中的某一列或者是集合中的某一 组属性。且分片是系统自动的,但是分片的依据需要用户自己指定,分片的依据称之为“片键”。常见的片键选择为“随机片键”或者是“递增片键”。如果使用递增片键,数据的分布则会按照一定的顺序来写入数据,即当一个节点写满才会写入到下一个片键。而如果使用随机片键,数据则可能在任何一个片中都有数据。
对于分布式中片的管理(片与片之间的通信),MongoDB中通过mongos进程来进行管理,客户端对数据的操作,都由mongos进程来处理。对于节点、分片的管理,MongoDB由配置服务器来管理,配置服务器其实也是一个单独MongoDB进程。
那么,一般在什么时候使用分片呢?在单个节点的磁盘不足时会分片,另外,当单个mongod不能满足写数据的性能要求时,可以使用分片;当单台服务器内存太小,将大量的数据放到内存中速度会很慢,为了提高性能,我们可以使用分片操作。
以下是对于MongoDB分片操作的实验:
操作环境:
Win8  64位操作系统,虚拟机CentOS 32位操作系统。MongoDB 2.4.5
实验步骤:
(1)启动配置服务器(放置一些元数据):
[plain] 
[root@h3 dbs]# mongod -dbpath /var/lib/mongo/dbs/config --nojournal --port 20000    
Mon Aug 12 13:36:25.999 [initandlisten] MongoDB starting : pid=22951 port=20000   dbpath=/var/lib/mongo/dbs/config 32-bit host=h3  
Mon Aug 12 13:36:26.001 [initandlisten]  
Mon Aug 12 13:36:26.002 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary  .  
Mon Aug 12 13:36:26.003 [initandlisten] **       32 bit builds are limited to le  ss than 2GB of data (or less with --journal).  
Mon Aug 12 13:36:26.004 [initandlisten] **       Note that journaling defaults t  o off for 32 bit and is currently off.  
Mon Aug 12 13:36:26.005 [initandlisten] **       See http://dochub.mongodb.org/c  ore/32bit  
Mon Aug 12 13:36:26.005 [initandlisten]  
Mon Aug 12 13:36:26.006 [initandlisten] db version v2.4.5  
Mon Aug 12 13:36:26.007 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840  383ec3506602b  
Mon Aug 12 13:36:26.007 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.  6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49  
Mon Aug 12 13:36:26.008 [initandlisten] allocator: system  
Mon Aug 12 13:36:26.009 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/c  onfig", nojournal: true, port: 20000 }  
Mon Aug 12 13:36:26.071 [FileAllocator] allocating new datafile /var/lib/mongo/d  bs/config/local.ns, filling with zeroes...  
Mon Aug 12 13:36:26.072 [FileAllocator] creating directory /var/lib/mongo/dbs/co  nfig/_tmp  
Mon Aug 12 13:36:26.582 [FileAllocator] done allocating datafile /var/lib/mongo/  dbs/config/local.ns, size: 16MB,  took 0.504 secs  
Mon Aug 12 13:36:26.585 [FileAllocator] allocating new datafile /var/lib/mongo/d  bs/config/local.0, filling with zeroes...  
Mon Aug 12 13:36:27.725 [FileAllocator] done allocating datafile /var/lib/mongo/  dbs/config/local.0, size: 16MB,  took 1.138 secs  
Mon Aug 12 13:36:27.757 [initandlisten] command local.$cmd command: { create: "s  tartup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37   1714ms  
Mon Aug 12 13:36:27.764 [websvr] admin web console waiting for connections on po  rt 21000  
Mon Aug 12 13:36:27.771 [initandlisten] waiting for connections on port 20000  
(2)启动mongos:
[plain] 
[root@h3 ~]# mongos --port 30000  --configdb localhost:20000  
Mon Aug 12 13:40:13.042 warning: running with 1 config server should be done only for testing purposes and is not recommended for production  
Mon Aug 12 13:40:13.075 [mongosMain] MongoS version 2.4.5 starting: pid=22968 port=30000 32-bit host=h3 (--help for usage)  
Mon Aug 12 13:40:13.076 [mongosMain] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b  
Mon Aug 12 13:40:13.076 [mongosMain] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49  
Mon Aug 12 13:40:13.076 [mongosMain] options: { configdb: "localhost:20000", port: 30000 }  
Mon Aug 12 13:40:13.250 [LockPinger] creating distributed lock ping thread for localhost:20000 and process h3:30000:1376286013:1804289383 (sleeping for 30000ms)  
Mon Aug 12 13:40:17.105 [mongosMain] distributed lock 'configUpgrade/h3:30000:1376286013:1804289383' acquired, ts : 52087541ff5991dd77979f2c  
Mon Aug 12 13:40:17.126 [mongosMain] starting upgrade of config server from v0 to v4  
Mon Aug 12 13:40:17.128 [mongosMain] starting next upgrade step from v0 to v4  
Mon Aug 12 13:40:17.130 [mongosMain] about to log new metadata event: { _id: "h3-2013-08-12T05:40:17-52087541ff5991dd77979f2d", server: "h3", clientAddr: "N/A", time: new Date(1376286017130), what: "starting upgrade of config database", ns: "config.version", details: { from: 0, to: 4 } }  
Mon Aug 12 13:40:17.138 [mongosMain] writing initial config version at v4  
Mon Aug 12 13:40:17.232 [mongosMain] about to log new metadata event: { _id: "h3-2013-08-12T05:40:17-52087541ff5991dd77979f2f", server: "h3", clientAddr: "N/A", time: new Date(1376286017232), what: "finished upgrade of config database", ns: "config.version", details: { from: 0, to: 4 } }  
Mon Aug 12 13:40:17.237 [mongosMain] upgrade of config server to v4 successful  
Mon Aug 12 13:40:17.244 [mongosMain] distributed lock 'configUpgrade/h3:30000:1376286013:1804289383' unlocked.  
Mon Aug 12 13:40:17.474 [websvr] admin web console waiting for connections on port 31000  
Mon Aug 12 13:40:17.476 [Balancer] about to contact config servers and shards  
Mon Aug 12 13:40:17.492 [Balancer] config servers and shards contacted successfully  
Mon Aug 12 13:40:17.494 [Balancer] balancer id: h3:30000 started at Aug 12 13:40:17  
Mon Aug 12 13:40:17.527 [mongosMain] waiting for connections on port 30000  
(3)添加分片1:
[plain] 
[root@h3 ~]# cd /var/lib/mongo/dbs/  
[root@h3 dbs]# mkdir shard1  
[root@h3 dbs]# mongod --dbpath /var/lib/mongo/dbs/shard1  --port 10000 --nojournal  
Mon Aug 12 13:43:10.560 [initandlisten] MongoDB starting : pid=23016 port=10000   
dbpath=/var/lib/mongo/dbs/shard1 32-bit host=h3  
Mon Aug 12 13:43:10.562 [initandlisten]  
Mon Aug 12 13:43:10.562 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.  
Mon Aug 12 13:43:10.563 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).  
Mon Aug 12 13:43:10.564 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.  
Mon Aug 12 13:43:10.565 [initandlisten] **       See http://dochub.mongodb.org/core/32bit  
Mon Aug 12 13:43:10.566 [initandlisten]  
Mon Aug 12 13:43:10.566 [initandlisten] db version v2.4.5  
Mon Aug 12 13:43:10.567 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b  
Mon Aug 12 13:43:10.568 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49  
Mon Aug 12 13:43:10.568 [initandlisten] allocator: system  
Mon Aug 12 13:43:10.569 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/shard1", nojournal: true, port: 10000 }  
Mon Aug 12 13:43:10.626 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/shard1/local.ns, filling with zeroes...  
Mon Aug 12 13:43:10.628 [FileAllocator] creating directory /var/lib/mongo/dbs/shard1/_tmp  
Mon Aug 12 13:43:11.547 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/shard1/local.ns, size: 16MB,  took 0.907 secs  
Mon Aug 12 13:43:11.549 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/shard1/local.0, filling with zeroes...  
Mon Aug 12 13:43:13.339 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/shard1/local.0, size: 16MB,  took 1.787 secs  
Mon Aug 12 13:43:13.427 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:372801ms  
Mon Aug 12 13:43:13.472 [initandlisten] waiting for connections on port 10000  
Mon Aug 12 13:43:13.497 [websvr] admin web console waiting for connections on port 11000  
(4)添加分片2:
[plain] 
[root@h3 dbs]# cd /var/lib/mongo/dbs/  
[root@h3 dbs]# mkdir shard2  
[root@h3 dbs]# mongod --dbpath /var/lib/mongo/dbs/shard2 --port 10001 --nojournal  
Mon Aug 12 13:48:25.549 [initandlisten] MongoDB starting : pid=23129 port=10001 dbpath=/var/lib/mongo/dbs/shard2 32-bit host=h3  
Mon Aug 12 13:48:25.550 [initandlisten]  
Mon Aug 12 13:48:25.550 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.  
Mon Aug 12 13:48:25.550 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).  
Mon Aug 12 13:48:25.550 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.  
Mon Aug 12 13:48:25.550 [initandlisten] **       See http://dochub.mongodb.org/core/32bit  
Mon Aug 12 13:48:25.551 [initandlisten]  
Mon Aug 12 13:48:25.551 [initandlisten] db version v2.4.5  
Mon Aug 12 13:48:25.552 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b  
Mon Aug 12 13:48:25.552 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49  
Mon Aug 12 13:48:25.552 [initandlisten] allocator: system  
Mon Aug 12 13:48:25.552 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/shard2", nojournal: true, port: 10001 }  
Mon Aug 12 13:48:25.570 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/shard2/local.ns, filling with zeroes...  
Mon Aug 12 13:48:25.572 [FileAllocator] creating directory /var/lib/mongo/dbs/shard2/_tmp  
Mon Aug 12 13:48:26.956 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/shard2/local.ns, size: 16MB,  took 1.378 secs  
Mon Aug 12 13:48:26.957 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/shard2/local.0, filling with zeroes...  
Mon Aug 12 13:48:28.023 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/shard2/local.0, size: 16MB,  took 1.065 secs  
Mon Aug 12 13:48:28.038 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 2469ms  
Mon Aug 12 13:48:28.041 [websvr] admin web console waiting for connections on port 11001  
Mon Aug 12 13:48:28.042 [initandlisten] waiting for connections on port 10001  
(5)添加分片3:
[plain] 
[root@h3 ~]# cd /var/lib/mongo/dbs/  
[root@h3 dbs]# mkdir shard3  
[root@h3 dbs]# mongod --dbpath /var/lib/mongo/dbs/shard3 --port 10002 --nojournal  
Mon Aug 12 13:50:43.735 [initandlisten] MongoDB starting : pid=23142 port=10002 dbpath=/var/lib/mongo/dbs/shard3 32-bit host=h3  
Mon Aug 12 13:50:43.737 [initandlisten]  
Mon Aug 12 13:50:43.737 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.  
Mon Aug 12 13:50:43.738 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).  
Mon Aug 12 13:50:43.739 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.  
Mon Aug 12 13:50:43.739 [initandlisten] **       See http://dochub.mongodb.org/core/32bit  
Mon Aug 12 13:50:43.740 [initandlisten]  
Mon Aug 12 13:50:43.740 [initandlisten] db version v2.4.5  
Mon Aug 12 13:50:43.740 [initandlisten] git version: a2ddc68ba7c9cee17bfe69ed840383ec3506602b  
Mon Aug 12 13:50:43.740 [initandlisten] build info: Linux bs-linux32.10gen.cc 2.6.21.7-2.fc8xen #1 SMP Fri Feb 15 12:39:36 EST 2008 i686 BOOST_LIB_VERSION=1_49  
Mon Aug 12 13:50:43.740 [initandlisten] allocator: system  
Mon Aug 12 13:50:43.741 [initandlisten] options: { dbpath: "/var/lib/mongo/dbs/shard3", nojournal: true, port: 10002 }  
Mon Aug 12 13:50:43.756 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/shard3/local.ns, filling with zeroes...  
Mon Aug 12 13:50:43.756 [FileAllocator] creating directory /var/lib/mongo/dbs/shard3/_tmp  
Mon Aug 12 13:50:45.079 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/shard3/local.ns, size: 16MB,  took 1.32 secs  
Mon Aug 12 13:50:45.099 [FileAllocator] allocating new datafile /var/lib/mongo/dbs/shard3/local.0, filling with zeroes...  
Mon Aug 12 13:50:45.948 [FileAllocator] done allocating datafile /var/lib/mongo/dbs/shard3/local.0, size: 16MB,  took 0.848 secs  
Mon Aug 12 13:50:45.957 [initandlisten] command local.$cmd command: { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0  reslen:37 2202ms  
Mon Aug 12 13:50:45.960 [initandlisten] waiting for connections on port 10002  
Mon Aug 12 13:50:45.961 [websvr] admin web console waiting for connections on port 11002  
(6)添加刚刚启动的三个片:
[plain] 
[root@h3 dbs]# mongo localhost:30000/admin  
MongoDB shell version: 2.4.5  
connecting to: localhost:30000/admin  
mongos> db.runCommand({addshard : "localhost:10000", allowLocal : true});  
{ "shardAdded" : "shard0000", "ok" : 1 }  
mongos> db.runCommand({addshard : "localhost:10001", allowLocal : true});  
{ "shardAdded" : "shard0001", "ok" : 1 }  
mongos> db.runCommand({addshard : "localhost:10002", allowLocal : true});  
{ "shardAdded" : "shard0002", "ok" : 1 }  
mongos>  
(7)打开数据库foo的分片功能:
[plain] 
mongos> db.runCommand({"enablesharding" : "foo"});  
{ "ok" : 1 }  
mongos>  
(8)打开数据库foo中数据集合bar的分片功能:
[plain] 
mongos> db.runCommand({"shardcollection" : "foo.bar", "key" : {"_id" : 1}});  
{ "collectionsharded" : "foo.bar", "ok" : 1 }  
mongos>  
(9)查看所有的分片:
[plain] 
mongos> use config  
switched to db config  
mongos> db.shards.find();  
{ "_id" : "shard0000", "host" : "localhost:10000" }  
{ "_id" : "shard0001", "host" : "localhost:10001" }  
{ "_id" : "shard0002", "host" : "localhost:10002" }  
mongos>  
(10)查看databases集合:
[plain] 
mongos> db.databases.find();  
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }  
{ "_id" : "foo", "partitioned" : true, "primary" : "shard0000" }  
mongos>  
(11) 查看chucks集合:
[plain] 
mongos> db.chunks.find();  
{ "_id" : "foo.bar-_id_MinKey", "lastmod" : Timestamp(1, 0), "lastmodEpoch" : ObjectId("52087d0a1072e2912d4242ea"), "ns" : "foo.bar", "min" : { "_id" : { "$minKey" : 1 } }, "max" : { "_id" : { "$maxKey" : 1 } }, "shard" : "shard0000" }  
mongos>  
(12)获得概要信息:
[plain] 
mongos> db.printShardingStatus();  
--- Sharding Status ---  
sharding version: {  
"_id" : 1,  
"version" : 3,  
"minCompatibleVersion" : 3,  
"currentVersion" : 4,  
"clusterId" : ObjectId("52087bf41072e2912d4242b8")  
}  
shards:  
{  "_id" : "shard0000",  "host" : "localhost:10000" }  
{  "_id" : "shard0001",  "host" : "localhost:10001" }  
{  "_id" : "shard0002",  "host" : "localhost:10002" }  
databases:  
{  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }  
{  "_id" : "foo",  "partitioned" : true,  "primary" : "shard0000" }  
foo.bar  
shard key: { "_id" : 1 }  
chunks:  
shard0000       1  
{ "_id" : { "$minKey" : 1 } } -->> { "_id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 0)  
mongos>  
(13)移除分片:
[plain] 
mongos> use admin  
switched to db admin  
mongos> db.runCommand({"removeshard" : "localhost:10000"});  
{  
"msg" : "draining started successfully",  
"state" : "started",  
"shard" : "shard0000",  
"note" : "you need to drop or movePrimary these databases",  
"dbsToMove" : [  
"foo"  
],  
"ok" : 1  
}  
mongos>  
总结:对MongoDB的分片操作执行如下顺序:
(1)启动配置服务器;
(2)启动mongos进程;
(3)添加mongo实例(片);
(4)对数据库或者数据集使用分片操作。

MongoDB分布式操作——分片操作相关推荐

  1. MongoDB分布式(分片存储)部署

    分别开启一个Config和两个Shard D:\mongodb1\bin\mongod --configsvr --dbpath D:\mongodb1\db\ --port 27020 D:\mon ...

  2. 用python向mongodb插入数据_Python操作MongoDB数据库(一)

    Python操作MongoDB数据库(一) 前言 干货文章继续出发!隔的时间有些久了哈,对 MongoDB 安装回顾的同学,可以看下windows环境 <初识 MongoDB 数据库>.这 ...

  3. mongodb启动以及基本命令操作

    mongodb启动以及基本命令操作 介绍: MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和 ...

  4. mongodb 超详细的操作集锦

    mongodb 超详细的操作集锦 数据库相关操作 集合相关操作 mongo中的操作符 CRUD相关操作 运算符操作 可以直接用命令行进行操作,也可以使用可视化工具,可视化工具推荐使用Robo,Robo ...

  5. MongoDB搭建和简单操作(windows环境)

    昨天看到fish的MongoDB,但遇到不少问题,于是自己一步一步尝试,将遇到之问题和解决方法记录总结 首先感谢fish!昨天忘了放链接:http://www.cnblogs.com/fish-li/ ...

  6. mongoDB中的聚合操作

    本文来说下mongoDB中的聚合操作 文章目录 概述 概述 MongoDB 中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似 SQL 语句中的 ...

  7. MongoDB中的索引操作

    本文来说下MongoDB中的索引操作 文章目录 概述 createIndex() 方法 语法 createIndex() 可选参数 索引操作 测试实例 本文小结 概述 索引通常能够极大的提高查询的效率 ...

  8. python分片的步长_Python的分片操作

    在列表中有一种操作叫做分片 一般形式: l2=[4,6,1,7,9] e = l2[2:4] print(e) 该代码运行结果如下:[1, 7] 从下标来看可以很容易的知道下标二对应的正好是元素3,值 ...

  9. MongoDB数据库操作---mongoose操作

    Mongoose认知概念 Mongoose是MongoDB的一个对象模型工具,其可以在一部环境下执行.同时它也是针对MongoDB操作的一个队形模型库,封装了MongoDB对文档的一些增删改查等常用方 ...

最新文章

  1. Java反射原理剖析一
  2. 长sql和短sql加java区别_Mybatis下动态sql中##和$$的区别讲解
  3. ThinkPHP开发中遇到的小坑
  4. 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)
  5. 楼梯计算机公式,各种楼梯面积的计算公式汇总
  6. Vue.set()详解
  7. 源于零售业,如何赋能零售业?亚马逊云科技再发布四项新服务
  8. Chrome 20对于HTML5最新支持的动态:颜色输入,网络信息API,CSS着色器
  9. 设计模式之单例模式8种实现方式,其一:饿汉式-静态常量饿汉式
  10. Proliant server setup redhat7.1(DL560)
  11. 刷新iframe内容
  12. tensorflow sigmoid 如何计算训练数据的正确率_初探 TensorFlow.js
  13. 华为云计算HICE学习总结
  14. win10如何安装mysql_win10安装mysql详细步骤
  15. 配置局域网内电脑快速传输数据
  16. PyQt5基础使用!(三)
  17. 20个数据分析师必会的营销模型,赶紧收藏
  18. 苹果画画软件_iPad Pro绘画软件Procreate零基础教程
  19. 计算机硬件的配置的图片,鲁大师怎么生成电脑配置图
  20. loopback回环接口

热门文章

  1. Git之常见零碎问题的原因分析和解决方案
  2. 【数据结构与算法】之深入解析“不同的二叉搜索树”的求解思路与算法示例
  3. iOS之Xcode运行时环境变量汇总说明
  4. SpaceX完成“星舰”空中悬停,距载人探火星还远吗?
  5. python 在互联网应用是如此强大
  6. 【机器视觉】 reset_fuzzy_measure算子
  7. 【Linux系统编程】进程间通信之命名管道
  8. 【Linux】Linux中正则表达式
  9. php截取字符串 xml,php截取字符串并保留完整xml标签的函数代码_php技巧
  10. 卸载idea2020不干净_强制卸载软件程序、以及清理注册表