zoukankan      html  css  js  c++  java
  • MongoDB 学习笔记之 分片和副本集混合运用

     分片和副本集混合运用:

    基本架构图:

    搭建详细配置:

    3个shard + 3个replicat set + 3个configserver + 3个Mongos

    shardrsname

    Primary

    Secondary

    Secondary

    port

    bigdata-sh-a

    bigdata-sh-a1

    bigdata-sh-a2

    bigdata-sh-a3

    28111

    bigdata-sh-b

    bigdata-sh-b2

    bigdata-sh-b1

    bigdata-sh-b3

    28112

    bigdata-sh-c

    bigdata-sh-c3

    bigdata-sh-c2

    bigdata-sh-c1

    28113

    bigdata-cs

    configserver1

    configserver2

    configserver3

    28200

     在三台Linux机器的mongo根目录下建立如下文件夹:

    Log:

    /usr/local/mongodb/logs/a1

    /usr/local/mongodb/logs/b1

    /usr/local/mongodb/logs/c1

    /usr/local/mongodb/logs/configserver1

    DB:

    /usr/local/mongodb/db/a1

    /usr/local/mongodb/db/b1

    /usr/local/mongodb/db/c1

    /usr/local/mongodb/db/configserver1

    bigdata-sh-a1.conf:

    # mongod config
    
    systemLog:
      destination: file
      logAppend: true
      path: /usr/local/mongodb/logs/a1/mongodb.log
    
    
    # Where and how to store data.
    storage:
      dbPath: /usr/local/mongodb/db/a1
      journal:
        enabled: true
    #  engine:
    #  mmapv1:
    #  wiredTiger:
    
    # how the process runs
    # fork : fork and run in background
    # pidFilePath:location of pidfile 
    processManagement:
      fork: true
      pidFilePath: /usr/local/mongodb/pidFile/mongod-a1.pid
    
    # network interfaces
    # Listen to local interface only, comment to listen on all interfaces.
    net:
      port: 28111
      bindIp: 0.0.0.0
    
    #security:  enabled  disabled
    #security:
    #  keyFile: /mongodb/keyfile
    #  clusterAuthMode: keyFile
    
    #operationProfiling:
    operationProfiling:
       slowOpThresholdMs: 1000
       mode: slowOp
    
    #replication:
    
    replication:
      replSetName: bigdata-sh-a
    
    #sharding:
    
    sharding:
      clusterRole: shardsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:

     bigdata-sh-configserver1.conf:

    # mongod config
    
    systemLog:
      destination: file
      logAppend: true
      path: /usr/local/mongodb/logs/configserver1/mongodb.log
    
    
    # Where and how to store data.
    storage:
      dbPath: /usr/local/mongodb/db/configserver1
      journal:
        enabled: true
    #  engine:
    #  mmapv1:
    #  wiredTiger:
    
    # how the process runs
    # fork : fork and run in background
    # pidFilePath:location of pidfile
    processManagement:
      fork: true
      pidFilePath: /usr/local/mongodb/pidFile/mongod-configserver1.pid
    
    # network interfaces
    # Listen to local interface only, comment to listen on all interfaces.
    net:
      port: 28200
      bindIp: 0.0.0.0
    
    
    #security:  enabled  disabled
    #security:
    #  keyFile: /mongodb/keyfile
    #  clusterAuthMode: keyFile
    
    #operationProfiling:
    operationProfiling:
       slowOpThresholdMs: 1000
       mode: slowOp
    
    #replication:
    
    replication:
      replSetName: bigdata-cs
    
    #sharding:
    
    sharding:
      clusterRole: configsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:

    配置完成后,启动服务:

    ./mongod -f ../conf/bigdata-sh-a1.conf

    ./mongod -f ../conf/bigdata-sh-b1.conf

    ./mongod -f ../conf/bigdata-sh-c1.conf

    ./mongod -f ../conf/bigdata-sh-configserver1.conf

    ./mongod -f ../conf/bigdata-sh-a2.conf

    ./mongod -f ../conf/bigdata-sh-b2.conf

    ./mongod -f ../conf/bigdata-sh-c2.conf

    ./mongod -f ../conf/bigdata-sh-configserver2.conf

    ./mongod -f ../conf/bigdata-sh-a3.conf

    ./mongod -f ../conf/bigdata-sh-b3.conf

    ./mongod -f ../conf/bigdata-sh-c3.conf

    ./mongod -f ../conf/bigdata-sh-configserver3.conf

    创建副本集:

    ./mongo hadoop1:28111/admin

    use admin

    rs.initiate()

    rs.add("hadoop2:28111")

    rs.add("hadoop3:28111")

    ./mongo hadoop2:28112/admin

    rs.initiate()

    rs.add("hadoop1:28112")

    rs.add("hadoop3:28112")

    ./mongo hadoop3:28113/admin

    rs.initiate()

    rs.add("hadoop2:28113")

    rs.add("hadoop1:28113")

    ./mongo hadoop1:28200/admin

    rs.initiate()

    rs.add("hadoop2:28200")

    rs.add("hadoop3:28200")

    在三台linux上启动Mongos:

    ./mongos --port 28300 --configdb bigdata-cs/hadoop1:28200,hadoop2:28200,hadoop3:28200 --fork --logpath /usr/local/mongodb/logs/mongos/mongos.log --bind_ip 0.0.0.0

    连接其中一台Mongos,增加分片:

    ./mongo hadoop1:28300/admin

    use admin

    sh.addShard("bigdata-sh-a/hadoop1:28111,hadoop2:28111,hadoop3:28111")

    sh.addShard("bigdata-sh-b/hadoop1:28112,hadoop2:28112,hadoop3:28112")

    sh.addShard("bigdata-sh-c/hadoop1:28113,hadoop2:28113,hadoop3:28113")

    sh.enableSharding("shop")

    sh.shardCollection("shop.goods",{"goodid": 1});

    插入大量数据进行验证:

    for(var i=80002; i<=90001; i++){ db.goods.insert({goodid: i, name: "My first shard"}) }

    大功告成!

  • 相关阅读:
    Java操作符,<<、>>等
    abstract的method是否可同时是static,是否可同时是native,是否可同时是synchronized?
    使用JDBC连接各种数据库
    pt-online-schema-change的原理解析与应用说明
    MySQL Online DDL的改进与应用
    细细探究MySQL Group Replicaiton — 配置维护故障处理全集
    关于binary log那些事——认真码了好长一篇
    梳理下MySQL崩溃恢复过程
    说说MySQL中的Redo log Undo log都在干啥
    SQL SERVER大话存储结构(5)_SQL SERVER 事务日志解析
  • 原文地址:https://www.cnblogs.com/AK47Sonic/p/7399794.html
Copyright © 2011-2022 走看看