• mongodb 副本集+分片集群搭建


    创建程序所需的目录
    for i in 17 18 19 20 21 22 23 24 25 26 
      do 
      mkdir -p /mongodb/280$i/conf  
      mkdir -p /mongodb/280$i/data  
      mkdir -p /mongodb/280$i/log
    done
     
     
    编辑shard集群配置文件
     
    cat > /mongodb/28021/conf/mongod.conf <<'EOF'
    systemLog:
      destination: file
      path: /mongodb/28021/log/mongodb.log   
      logAppend: true
    storage:
      journal:
        enabled: true
      dbPath: /mongodb/28021/data
      directoryPerDB: true
      #engine: wiredTiger
      wiredTiger:
        engineConfig:
          cacheSizeGB: 1
          directoryForIndexes: true
        collectionConfig:
          blockCompressor: zlib
        indexConfig:
          prefixCompression: true
    net:
      bindIp: 10.0.0.152
      port: 28021
    replication:
      oplogSizeMB: 2048
      replSetName: sh1
    sharding:
      clusterRole: shardsvr
    processManagement: 
      fork: true
    EOF
     
    复制shard集群配置文件
    for i in 22 23 24 25 26  
      do  
       cp /mongodb/28021/conf/mongod.conf /mongodb/280$i/conf/
    done
     
    修改配置文件端口
    for i in 22 23 24 25 26  
      do 
        sed -i "s#28021#280$i#g" /mongodb/280$i/conf/mongod.conf
    done
     
    修改配置文件复制集名称(replSetName)
    for i in 24 25 26  
      do 
        sed -i "s#sh1#sh2#g" /mongodb/280$i/conf/mongod.conf
    done
     
    启动shard集群
    for i in 21 22 23 24 25 26
      do  
        mongod -f /mongodb/280$i/conf/mongod.conf 
    done
     
    配置复制集1
    mongo --host 10.0.0.152 --port 28021 admin
     
    #配置复制集
    config = {_id: 'sh1', members: [
                              {_id: 0, host: '10.0.0.152:28021'},
                              {_id: 1, host: '10.0.0.152:28022'},
                              {_id: 2, host: '10.0.0.152:28023',"arbiterOnly":true}]
               }  
     
    # 初始化配置
    rs.initiate(config)  
     
    配置复制集2
    mongo --host 10.0.0.152 --port 28024 admin
     
    #配置复制集
    config = {_id: 'sh2', members: [
                              {_id: 0, host: '10.0.0.152:28024'},
                              {_id: 1, host: '10.0.0.152:28025'},
                              {_id: 2, host: '10.0.0.152:28026',"arbiterOnly":true}]
               }
               
    # 初始化配置
    rs.initiate(config)
     
     
    config集群配置
     
    创建主节点配置文件
     
    cat > /mongodb/28018/conf/mongod.conf <<'EOF'
    systemLog:
      destination: file
      path: /mongodb/28018/log/mongodb.conf
      logAppend: true
    storage:
      journal:
        enabled: true
      dbPath: /mongodb/28018/data
      directoryPerDB: true
      #engine: wiredTiger
      wiredTiger:
        engineConfig:
          cacheSizeGB: 1
          directoryForIndexes: true
        collectionConfig:
          blockCompressor: zlib
        indexConfig:
          prefixCompression: true
    net:
      bindIp: 10.0.0.152
      port: 28018
    replication:
      oplogSizeMB: 2048
      replSetName: configReplSet
    sharding:
      clusterRole: configsvr
    processManagement: 
      fork: true
    EOF
     
    将配置文件分发到从节点
    for i in 19 20 
      do  
       cp /mongodb/28018/conf/mongod.conf /mongodb/280$i/conf/
    done
     
    修改配置文件端口信息
    for i in 19 20  
      do 
        sed -i "s#28018#280$i#g" /mongodb/280$i/conf/mongod.conf
    done
     
    启动configserver集群
    for i in 19 20  
      do 
        sed -i "s#28018#280$i#g" /mongodb/280$i/conf/mongod.conf
    done
     
    配置configserver复制集
    mongo --host 10.0.0.152 --port 28018 admin
     
    #配置复制集信息
    config = {_id: 'configReplSet', members: [
                              {_id: 0, host: '10.0.0.152:28018'},
                              {_id: 1, host: '10.0.0.152:28019'},
                              {_id: 2, host: '10.0.0.152:28020'}]
               }
     
    # 初始化配置
    rs.initiate(config)   
    注:configserver 使用复制集不用有arbiter节点。3.4版本以后config必须为复制集
     
    mongos节点配置
     
    修改配置文件
    cat > /mongodb/28017/conf/mongos.conf <<'EOF'
    systemLog:
      destination: file
      path: /mongodb/28017/log/mongos.log
      logAppend: true
    net:
      bindIp: 10.0.0.152
      port: 28017
    sharding:
      configDB: configReplSet/10.0.0.152:28108,10.0.0.152:28019,10.0.0.152:28020
    processManagement: 
      fork: true
    EOF 
     
    启动mongos
    mongos -f /mongodb/28017/conf/mongos.conf 
     
    登陆到mongos
    mongo 10.0.0.152:28017/admin
     
    添加分片节点
     
    db.runCommand( { addshard : "sh1/10.0.0.152:28021,10.0.0.152:28022,10.0.0.152:28023",name:"shard1"} )
    db.runCommand( { addshard : "sh2/10.0.0.152:28024,10.0.0.152:28025,10.0.0.152:28026",name:"shard2"} )
     
    列出分片
    mongos> db.runCommand( { listshards : 1 } )
    {
        "shards" : [
            {
                "_id" : "shard2",
                "host" : "sh2/10.0.0.152:28024,10.0.0.152:28025"
            },
            {
                "_id" : "shard1",
                "host" : "sh1/10.0.0.152:28021,10.0.0.152:28022"
            }
        ],
        "ok" : 1
    }
     
    整体状态查看
    mongos> sh.status();
     
    至此MongoDB的分片集群就搭建完成。
     
     
    数据库分片配置
     
    激活数据库分片功能
     
    语法:( { enablesharding : "数据库名称" } )
    mongos> db.runCommand( { enablesharding : "test" } )
     
    指定分片建对集合分片,范围片键--创建索引
    mongos> use test 
    mongos> db.vast.ensureIndex( { id: 1 } )
    mongos> use admin
    mongos> db.runCommand( { shardcollection : "test.vast",key : {id: 1} } )
     
    集合分片验证
    mongos> use test
    mongos> for(i=0;i<20000;i++){ db.vast1.insert({"id":i,"name":"clsn","age":70,"date":new Date()}); }
    mongos> db.vast.stats()
  • 相关阅读:
    洛谷P1422 小玉家的电费
    洛谷P1425 小鱼的游泳时间
    洛谷P1421 小玉买文具
    洛谷P1001 A+B Problem
    洛谷P1000 超级玛丽游戏
    Android Hook框架adbi的分析(2)--- inline Hook的实现
    Android Apk加固的初步实现思路(dex整体加固)
    Android Hook框架adbi的分析(1)---注入工具hijack
    从苏宁电器到卡巴斯基(后传)第05篇:聊聊我对WannaCry产生的感慨
    Android APK程序的smali动态调试
  • 原文地址:https://www.cnblogs.com/liang545621/p/7422953.html
Copyright © 2020-2023  润新知