1. Configure Configuration Server.

1.1. Create a directory: e.g. C:\data\dbs\config

1.2. Start config server

mongod --dbpath c:\data\dbs\config --port 20000

2. Start mongos connecting to config server

mongos --port 30000 --configdb  localhost:20000

3. Create two shards 

3.1 Create shard 1

3.1.1 Create folder - C:\data\dbs\shard1
3.1.2 mongod --dbpath c:\data\dbs\shard1 --port 10000

3.2 Create shard 2

3.2.1 Create folder - C:\data\dbs\shard2 
3.2.2 mongod --dbpath c:\data\dbs\shard2 --port 10001

4. Add shard to mongos

4.1 Connect to mongos

mongo  localhost:30000/admin

MongoDB shell version: 2.4.4
connecting to: localhost:30000/admin
mongos>

4.2. Add shard

mongos> db.runCommand({addShard:"localhost:10000", allowLocal:true})
{ "shardAdded" : "shard0000", "ok" : 1 }

mongos> db.runCommand({addShard:"localhost:10001", allowLocal:true})
{ "shardAdded" : "shard0001", "ok" : 1 }

5. Enable sharding on database (e.g. test)

mongos> db.runCommand({"enablesharding":"test"})
{ "ok" : 1 }

6. Enable sharding on collection 

mongos> db.runCommand({"shardcollection":"test.gem_deal_data", "key":{"_id":1}})
{ "collectionsharded" : "test.gem_deal_data", "ok" : 1 }

Then can use sharding in the application code. For example,

import pymongo

class MongoDb(object):
def __init__(self, host='localhost', port=27017):
self.conn = pymongo.MongoClient(host, port)
self.db = self.conn.test def close(self):
self.conn.close() def open_connection():
# Specify the port 30000 to connect to mongos
conn = pymongo.MongoClient('localhost', 30000)
return conn.db

7. (Updated on 2014-11-10)

If you want to shard the GridFS collection, please refer to  http://docs.mongodb.org/manual/tutorial/shard-gridfs-data/
Quick Notes:

* files collection is usually small in size, thus no need to shard this collection in general

* chunks collection

{files_id:1, n:1} and {files_id:1} are the only supported shard keys for the chunk collections of a GridFS store.

To shard the chunks collection by {files_id:1, n:1}, issue commands similar to the following:

** db.fs.chunks.ensureIndex({files_id:1, n:1})   <--- Maybe not need to create this manually, some driver like Pymongo would create this index automatically.

** db.runCommand({shardCollection:"test.fs.chunks", key:{files_id:1, n:1}})

You may also want to shard using just the file_id field, as in the following operation:

db.runCommand({shardCollection:"test.fs.chunks", key:{files_id:1}})

MongoDB add sharding -- Just a note的更多相关文章

  1. MongoDB分片(sharding)

    1.概念 分片(sharding)是指将数据拆分,将其分散存在不同的机器上的过程.有时也用分区(partitioning)来表示这个概念.将数据分散到不同的机器上,不需要功能强大的大型计算机就可以储存 ...

  2. Mongodb中Sharding集群

    随着mongodb数据量的增多,可能会达到单个节点的存储能力限制,以及application较大的访问量也会导致单个节点无法承担,所以此时需要构建集群环境,并通过sharding方案将整个数据集拆分成 ...

  3. MongoDB ReplacaSet & Sharding集群安装 配置 和 非集群情况的安装 配置 -摘自网络

    单台机器做sharding --单机配置集群服务(Sharding) --shard1_1 mongod --install --serviceName MongoDBServerShard1 --s ...

  4. 2.mongoDB add user in v3.0 问题的解决(Property 'addUser' of object admin is not a func)

    问题:创建mongodb帐户时,出错 > db.addUser('jyu', 'aerohive')  2015-08-05T20:03:02.767+0800 E QUERY    TypeE ...

  5. Mongodb Sharding 集群配置

    mongodb的sharding集群由以下3个服务组成: Shards  Server: 每个shard由一个或多个mongod进程组成,用于存储数据 Config  Server: 用于存储集群的M ...

  6. MongoDB sharding cluster Step by Step

    本篇讲述MongoDB的 Sharding Cluster 的详细步骤,按着做理论上不会有什么错误. 关于说着里边的参数.变量.和设置,没有用到很多,只用到了关键的一些,其他的可以参考MongoDB的 ...

  7. MongoDB高可用架构:Replica Sets+Sharding

    MongoDB的sharding解决了海量存储和动态扩容的问题.但是遇到单点故障就显得无能为力了.MongoDB的副本集可以很好的解决单点故障的问题.所以就有了Sharding+Replica Set ...

  8. MongoDB Sharding 机制分析

    MongoDB Sharding 机制分析 MongoDB 是一种流行的非关系型数据库.作为一种文档型数据库,除了有无 schema 的灵活的数据结构,支持复杂.丰富的查询功能外,MongoDB 还自 ...

  9. mongodb 3.0 版本分片部署步骤

    # linux 网络优化 1. 文件中/etc/sysctl.conf, 加入 net.core.somaxconn = 2048 fs.file-max = 2000000 fs.nr_open = ...

随机推荐

  1. 【Javascript Demo】无刷新预览所选择的图片

    1.效果如下,可测试 2.代码如下 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  2. php之快速入门学习-15(php函数)

    PHP 函数 PHP 的真正威力源自于它的函数. 在 PHP 中,提供了超过 1000 个内建的函数. PHP 内建函数 如需查看所有数组函数的完整参考手册和实例,请访问我们的 PHP 参考手册. P ...

  3. Hibernate(十三)命名查询-批量处理和调用存储过程

    一.命名查询 HQL语句混杂在代码之间将破坏代码的可读性 Hibernate允许在映射配置文件中定义字符串形式的查询语句,这种查询方式称为命名查询 二.建立数据表和持久化类 建立数据表 create ...

  4. Yahoo团队总结的关于网站性能优化的经验(转)

    英文原文:http://developer.yahoo.com/performance/rules.html 中文原文:http://www.ha97.com/2710.html 1.尽量减少HTTP ...

  5. Codeforces Round #265 (Div. 2) D. Restore Cube 立方体推断

    http://codeforces.com/contest/465/problem/D 给定8个点坐标.对于每一个点来说,能够任意交换x.y,z坐标的数值. 问说8个点能否够组成立方体. 暴力枚举就可 ...

  6. java 调整jvm堆大小上限

    针对单个类,eclipse中调整jvm的运行参数,加上这么一句: -Xmx80m 即可把堆上限调整到80m. 关键字: BEA JRockit

  7. 用 Qt Creator 开发非 Qt 的 C/C++ 程序

    在Windows还是习惯用VS2005但是现在到了Linux下,开发起来C/C++程序就没有那么得心应手的IDE了.虽然很多人推荐E开头那个主要作为Java开发的IDE,不过安上插件后感觉不大好,一个 ...

  8. MYSQL查询一周内的数据(最近7天的)

    select * from wap_content where week(created_at) = week(now) 如果你要严格要求是某一年的,那可以这样 查询一天: select * from ...

  9. hihoCoder 1014 Trie树 (Trie)

    #1014 : Trie树 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 小Hi和小Ho是一对好朋友.出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮 ...

  10. 基于JavaScript 声明全局变量的三种方式

    本文转自脚本之家:http://www.jb51.net/article/36548.htm JS中声明全局变量主要分为显式声明或者隐式声明下面分别介绍. 声明方式一: 使用var(关键字)+变量名( ...