Nosql Mongodb之旅(31)—Replica Sets+Sharding

前端之家收集整理的这篇文章主要介绍了Nosql Mongodb之旅(31)—Replica Sets+Sharding前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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上

[plain] view plain copy
  1. [root@localhostbin]#/Apps/mongo/bin/mongod--shardsvr--replSetshard1--port27017
  2. --dbpath/data/shard1_1--logpath/data/shard1_1/shard1_1.log--logappend--fork
  3. [root@localhostbin]#alloutputgoingto:/data/shard1_1/shard1_1.log
  4. forkedprocess:18923
在server B上
    --dbpath/data/shard1_2--logpath/data/shard1_2/shard1_2.log--logappend--fork
  1. forkedprocess:18859
  2. [root@localhostbin]#alloutputgoingto:/data/shard1_2/shard1_2.log
  3. [root@localhostbin]#
在Server C 上
    --dbpath/data/shard1_3--logpath/data/shard1_3/shard1_3.log--logappend--fork
  1. alloutputgoingto:/data/shard1_3/shard1_3.log
  2. forkedprocess:18768
  3. [root@localhostbin]#
用mongo 连接其中一台机器的27017 端口的mongod,初始化Replica Sets“shard1”,执行:
    [root@localhostbin]#./mongo--port27017
  1. MongoDBshellversion:1.8.1
  2. connectingto:127.0.0.1:27017/test
  3. >config={_id:'shard1',members:[
  4. ...{_id:0,host:'192.168.3.231:27017'},
  5. ...{_id:1,host:'192.168.3.232:27017'},
  6. ...{_id:2,host:'192.168.3.233:27017'}]
  7. ...}
  8. ……
  9. >rs.initiate(config)
  10. {
  11. "info":"Confignowsavedlocally.Shouldcomeonlineinaboutaminute.",108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "ok":1
  12. }
(2)配置shard2所用到的Replica Sets

在server A上

    [root@localhostbin]#/Apps/mongo/bin/mongod--shardsvr--replSetshard2--port27018
  1. --dbpath/data/shard2_1--logpath/data/shard2_1/shard2_1.log--logappend--fork
  2. alloutputgoingto:/data/shard2_1/shard2_1.log
  3. [root@localhostbin]#forkedprocess:18993
  4. [root@localhostbin]#
在server B上
    --dbpath/data/shard2_2--logpath/data/shard2_2/shard2_2.log--logappend--fork
  1. alloutputgoingto:/data/shard2_2/shard2_2.log
  2. forkedprocess:18923
  3. [root@localhostbin]#
在Server C上
    --dbpath/data/shard2_3--logpath/data/shard2_3/shard2_3.log--logappend--fork
  1. [root@localhostbin]#alloutputgoingto:/data/shard2_3/shard2_3.log
  2. forkedprocess:18824
  3. [root@localhostbin]#

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

    [root@localhostbin]#./mongo--port27018
  1. connectingto:127.0.0.1:27018/test
  2. >config={_id:'shard2',host:'192.168.3.231:27018'},host:'192.168.3.232:27018'},host:'192.168.3.233:27018'}]
  3. db.runCommand({enablesharding:"test"})
  4. db.runCommand({shardcollection:"test.users",key:{_id:1}})
  5. }
(3)配置3 台Config Server
在Server A、B、C上执行:
@H_554_301@
    /Apps/mongo/bin/mongod--configsvr--dbpath/data/config--port20000--logpath
  1. /data/config/config.log--logappend--fork
(4)配置3台Route Process
在Server A、B、C上执行:
    /Apps/mongo/bin/mongos--configdb
  1. 192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000--port30000--chunkSize1
  2. --logpath/data/mongos.log--logappend--fork
(5)配置Shard Cluster
连接到其中一台机器的端口30000 的mongos 进程,并切换到admin 数据库做以下配置
    [root@localhostbin]#./mongo--port30000
  1. connectingto:127.0.0.1:30000/test
  2. >useadmin
  3. switchedtodbadmin
  4. >db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:
  5. 27017"});
  6. {"shardAdded":"shard1","ok":1}
  7. >db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:
  8. 27018"});
  9. {"shardAdded":"shard2","ok":1}
  10. >
激活数据库及集合的分片
    db.runCommand({enablesharding:"test"})
  1. 数据库,以便添加测试数据
      usetest
    1. for(vari=1;i<=200000;i++)db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});
    2. db.users.stats()
    3. {
    4. "sharded":true,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "ns":"test.users",108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "count":200000,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "size":25600384,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "avgObjSize":128,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "storageSize":44509696,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "nindexes":2,248); line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "nchunks":15,108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "shards":{
    5. "shard0000":{
    6. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:21px; margin:0px!important; padding:0px 3px 0px 10px!important"> "shard0001":{
    7. ……
    8. }
    9. }
    可以看到Sharding搭建成功了,跟我们期望的结果一致,至此我们就将Replica Sets与Sharding结合的架构也学习完毕了! 原文链接:https://www.f2er.com/nosql/204145.html

    猜你在找的NoSQL相关文章