美文网首页
Monogodb复制(副本集)配置

Monogodb复制(副本集)配置

作者: Odven | 来源:发表于2020-05-02 18:13 被阅读0次

    1) 规划

    三台服务器    一主一从一仲裁
    cat >> /etc/hosts <<EOF
    192.168.192.100  node1
    192.168.192.101  node2
    192.168.192.102  node3
    EOF
    
    mkdir /data/mongodb/{etc,log,data} -p
    

    2)配置

    tar -xf mongodb-linux*.taz
    cd mongodb-linux*
    cp -r bin  /data/mongodb/
    
    cat > /data/mongodb/etc/mongodb.conf<<EOF 
    systemLog:
      destination: file
      path: /data/mongodb/log/mongodb.log   
      logAppend: true
    storage:
      journal:
        enabled: true
      dbPath: /data/mongodb/data
      directoryPerDB: true
      #engine: wiredTiger
      wiredTiger:
        engineConfig:
          cacheSizeGB: 1
          directoryForIndexes: true
        collectionConfig:
          blockCompressor: zlib
        indexConfig:
          prefixCompression: true
    net:
      bindIp: 192.168.192.100,127.0.0.1
      port: 27017
    replication:
      oplogSizeMB: 2048
      replSetName: sh1
    #sharding:
    #  clusterRole: shardsvr
    processManagement: 
      fork: true
    EOF
    

    3) 启动副本集、

    /data/mongodb/bin/mongo --port 27017 admin
    
    config = {_id: 'sh1', members: [
                              {_id: 0, host: 'node1:27017'},
                              {_id: 1, host: 'node2:27017'},
                              {_id: 2, host:'node3:27017',"arbiterOnly":true}]
               }
    
    rs.initiate(config)
    
    或
    rs.initiate()
    rs.add("node2:27017")
    rs.addArb("node3:27017")
    

    4) 修改权重

    rs.config()  # 查看索引顺序
    
    cfg = rs.config()  # 获取副本集的配置,默认权重都是1
    
    cfg.members[0].priority = 100  # 把索引为0的机器的权重变为100
    cfg.members[1].priority = 50  # 把索引为1的机器的权重变为50
    
    rs.reconfig(cfg)  # 更新mongodb副本集的配置
    

    相关文章

      网友评论

          本文标题:Monogodb复制(副本集)配置

          本文链接:https://www.haomeiwen.com/subject/mgzkghtx.html