MongoDB Auto-Sharding 解决了海量存储和动态扩容的问题,但离实际生产环境所需的高可靠、高可用还有些距离,所以有了"Replica Sets + Sharding"的解决方案。

shard:

使用Replica Sets,确保每个数据节点都具有备份,自动容错转移,自动回复能力。

config:

使用3个配置服务器,确保元数据的完整性。

route:

使用3个路由进程,实现负载均衡,提高客户端接入性能。

配置Replica Sets + Sharding 架构图:

配置Replica Sets + Sharding

  (1)配置shard1所用到的Replica Sets

在server A上

  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
  2. --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
  3. [root@localhost bin]# all output going to: /data/shard1_1/shard1_1.log
  4. forked process: 18923

在server B上

  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
  2. --dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork
  3. forked process: 18859
  4. [root@localhost bin]# all output going to: /data/shard1_2/shard1_2.log
  5. [root@localhost bin]#

在Server C 上

  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
  2. --dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork
  3. all output going to: /data/shard1_3/shard1_3.log
  4. forked process: 18768
  5. [root@localhost bin]#

用mongo 连接其中一台机器的27017 端口的mongod,初始化Replica Sets“shard1”,执行:

  1. [root@localhost bin]# ./mongo --port 27017
  2. MongoDB shell version: 1.8.1
  3. connecting to: 127.0.0.1:27017/test
  4. > config = {_id: 'shard1', members: [
  5. ... {_id: 0, host: '192.168.3.231:27017'},
  6. ... {_id: 1, host: '192.168.3.232:27017'},
  7. ... {_id: 2, host: '192.168.3.233:27017'}]
  8. ... }
  9. ……
  10. > rs.initiate(config)
  11. {
  12. "info" : "Config now saved locally. Should come online in about a minute.",
  13. "ok" : 1
  14. }

(2)配置shard2所用到的Replica Sets

在server A上

  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
  2. --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
  3. all output going to: /data/shard2_1/shard2_1.log
  4. [root@localhost bin]# forked process: 18993
  5. [root@localhost bin]#

在server B上

  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
  2. --dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --fork
  3. all output going to: /data/shard2_2/shard2_2.log
  4. forked process: 18923
  5. [root@localhost bin]#

在Server C上

  1. [root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
  2. --dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork
  3. [root@localhost bin]# all output going to: /data/shard2_3/shard2_3.log
  4. forked process: 18824
  5. [root@localhost bin]#

用mongo 连接其中一台机器的27018 端口的mongod,初始化Replica Sets “shard2”,执行:

  1. [root@localhost bin]# ./mongo --port 27018
  2. MongoDB shell version: 1.8.1
  3. connecting to: 127.0.0.1:27018/test
  4. > config = {_id: 'shard2', members: [
  5. ... {_id: 0, host: '192.168.3.231:27018'},
  6. ... {_id: 1, host: '192.168.3.232:27018'},
  7. ... {_id: 2, host: '192.168.3.233:27018'}]
  8. ... }
  9. ……
  10. > rs.initiate(config)
  11. {
  12. "info" : "Config now saved locally. Should come online in about a minute.",
  13. "ok" : 1
  14.  
  15. db.runCommand({ enablesharding:"test" })
  16. db.runCommand({ shardcollection: "test.users", key: { _id:1 }})
  17.  
  18. }

  (3)配置3 台Config Server
    在Server A、B、C上执行:

  1. /Apps/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath
  2. /data/config/config.log --logappend --fork

(4)配置3台Route Process
    在Server A、B、C上执行:

  1. /Apps/mongo/bin/mongos --configdb
  2. 192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000 --port 30000 --chunkSize 1
  3. --logpath /data/mongos.log --logappend --fork

(5)配置Shard Cluster
    连接到其中一台机器的端口30000 的mongos 进程,并切换到admin 数据库做以下配置

  1. [root@localhost bin]# ./mongo --port 30000
  2. MongoDB shell version: 1.8.1
  3. connecting to: 127.0.0.1:30000/test
  4. > use admin
  5. switched to db admin
  6. >db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:
  7. 27017"});
  8. { "shardAdded" : "shard1", "ok" : 1 }
  9. >db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:
  10. 27018"});
  11. { "shardAdded" : "shard2", "ok" : 1 }
  12. >

激活数据库及集合的分片

  1. db.runCommand({ enablesharding:"test" })
  2. db.runCommand({ shardcollection: "test.users", key: { _id:1 }})

(6)验证Sharding正常工作
    连接到其中一台机器的端口30000 的mongos 进程,并切换到test 数据库,以便添加测试数据

  1. use test
  2. for(var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});
  3. db.users.stats()
  4. {
  5. "sharded" : true,
  6. "ns" : "test.users",
  7. "count" : 200000,
  8. "size" : 25600384,
  9. "avgObjSize" : 128,
  10. "storageSize" : 44509696,
  11. "nindexes" : 2,
  12. "nchunks" : 15,
  13. "shards" : {
  14. "shard0000" : {
  15. ……
  16. },
  17. "shard0001" : {
  18. ……
  19. }
  20. },
  21. "ok" : 1
  22. }

可以看到Sharding搭建成功了,跟我们期望的结果一致,至此我们就将Replica Sets与Sharding结合的架构也学习完毕了!

MongoDB整理笔记のReplica Sets + Sharding的更多相关文章

  1. MongoDB整理笔记のReplica Sets

    MongoDB支持在多个机器中通过异步复制达到故障转移和实现冗余.多机器中同一时刻只有一台机器是用于写操作,正因为如此,MongoDB提供了数据一致性的保障.而担当primary角色的机器,可以把读的 ...

  2. MongoDB整理笔记のReplica oplog

    主从操作日志oplog MongoDB的Replica Set架构是通过一个日志来存储写操作的,这个日志就叫做"oplog".oplog.rs是一个固定长度的capped coll ...

  3. 利用Mongodb的复制集搭建高可用分片,Replica Sets + Sharding的搭建过程

    参考资料 reference:  http://mongodb.blog.51cto.com/1071559/740131  http://docs.mongodb.org/manual/tutori ...

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

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

  5. Mongo之架构部署(Replica Sets+Sharding)

    一.环境 要构建一个 MongoDB Sharding Cluster,需要三种角色: •Shard Server: mongod 实例,用于存储实际的数据块. •Config Server: mon ...

  6. MongoDB整理笔记の增加节点

    MongoDB Replica Sets 不仅提供高可用性的解决方案,它也同时提供负载均衡的解决方案,增减Replica Sets 节点在实际应用中非常普遍,例如当应用的读压力暴增时,3 台节点的环境 ...

  7. MongoDB整理笔记の管理Replica Sets

    一.读写分离 从库能进行查询,这样可以分担主库的大量的查询请求.   1.先向主库中插入一条测试数据 [root@localhost bin]# ./mongo --port 28010 MongoD ...

  8. MongoDB整理笔记のSharding分片

    这是一种将海量的数据水平扩展的数据库集群系统,数据分表存储在sharding 的各个节点上,使用者通过简单的配置就可以很方便地构建一个分布式MongoDB 集群.MongoDB 的数据分块称为 chu ...

  9. MongoDB整理笔记の管理Sharding

    1.列出所有的Shard Server > db.runCommand({ listshards: 1 }) --列出所有的Shard Server { "shards" : ...

随机推荐

  1. Newtonsoft.Json[C#]

    C# Newtonsoft.Json JsonSerializerSettings配置序列化操作 https://blog.csdn.net/u011127019/article/details/72 ...

  2. 微软TechNet关于TLS的细节的描述

    https://technet.microsoft.com/en-us/library/cc785811.aspx TLS协议太复杂了,RFC太长没时间看,这篇还可以,好歹知道个大概. 想知道全部细节 ...

  3. Python学习日记(一)——IDLE、运算符

    环境:win8.1+python2.7.8 一.名词解释: 1.IDLE:经常编程的同学相信对集成开发环境(Integrated Development Environment,IDE)应该非常熟悉了 ...

  4. Redis实战——安装

    借鉴来源:https://www.cnblogs.com/codersay/p/4301677.html redis官网地址:http://www.redis.io/ 最新版本: redis-4.0. ...

  5. Java安全框架 Apache Shiro学习-1-ini 配置

    简单登录流程: 1.  SecurityManager   2.  SecurityUtils.setSecurityManager 3.  SecurityUtils.getSubject     ...

  6. 爬虫解析:XPath总结

    1.加载 XML 文档 所有现代浏览器都支持使用 XMLHttpRequest 来加载 XML 文档的方法. 针对大多数现代浏览器的代码: var xmlhttp=new XMLHttpRequest ...

  7. javascript对变量和函数的声明提前‘hoist’

    hoist vt.升起,提起; vi.被举起或抬高; n.起重机,升降机; 升起; <俚>推,托,举; 原文地址:http://www.bootcss.com/article/variab ...

  8. Linux cloc

    一.简介 cloc是一个基于perl的.十分好用的代码统计工具,它所支持的语言还算十分丰富.不过,还是有很多用的较少的语言是不支持的.   二.安装配置 1)官网安装教程 http://cloc.so ...

  9. g2o 图优化

    http://www.cnblogs.com/gaoxiang12/p/5244828.html 扩展里面csparse

  10. [Training Video - 3] [Groovy in Detail] What is a groovy class ?

    log.info "starting" // we use class to create objects of a class Planet p1 = new Planet() ...