一.mongodb副本集方式集群搭建

  1. 以三台设备三个节点为例,192.168.1.136 当作副本集主节点,192.168.1.137作为副本集副本节点,192.168.1.138作为仲裁点。
  2. 分别在每台机器上安装mongodb并建立mongodb副本集数据存放文件夹
    cd /opt
    tar xvzf mongodb-linux-x86_64-2.4.8.tgz
    mkdir -p /opt/mongodb/cfg
    mkdir -p /opt/mongodb/log
    mkdir -p /opt/mongodb/bin
    mkdir -p /data/mongodb
  3. 配置每台设备的mongodb启动配置文件
    #master.conf  
    dbpath=/data/mongodb 
    logpath=/opt/mongodb/log/master.log  
    pidfilepath=/opt/mongodb/master.pid  
    directoryperdb=true  
    logappend=true  
    replSet=ctnq
    bind_ip=192.168.1.136  
    port=27017  
    oplogSize=10000  
    fork=true  
    noprealloc=true

    副本节点和仲裁点配置相同,只修改对应ip命名为slaver.conf,arbiter.conf即可

  4. 启动所有节点的mongodb
    monood -f master.conf  
    mongod -f slaver.conf  
    mongod -f arbiter.conf
  5. 初始化副本集
    mongo 127.0.0.1:27017
    use admin
    cfg = { _id:"ctnq", members:[
    ... {_id:0,host:"192.168.1.136:27017",priority:2},
    ... {_id:1,host:"192.168.1.137:27017",priority:1},
    ... {_id:2,host:"192.168.1.138:27017",arbiterOnly:true}]
    ... }
    rs.initiate(cfg);
    rs.status();

    检查输出状态,如无异常,一个副本集搭建完成

二. 利用nginx-gridfs读取mongodb副本集资源

  1. nginx-gridfs插件安装
    cd /usr/local/
    git clonegit://github.com/mdirolf/nginx-gridfs.git
    cd nginx-gridfs
    git submodule init
    git submodule update
    cd /usr/local/src/nginx-1.10
    ./configure --prefix=/usr/local/nginx --add-module=/usr/local/nginx-gridfs/
  2. nginx-gridfs插件配置说明(conf/nginx.conf)
    • gridfs: nginx识别插件的关键字
    • edusns: db名
    • root_collection: 选择collection,如root_collection=blog, mongod就会去找blog.files与blog.chunks两个块,默认是fs
    • field: 查询字段,保证mongdb里有这个字段名,支持_id, filename, 可省略, 默认是_id
    • type: 解释field的数据类型,支持objectid, int, string, 可省略, 默认是int
    • user: 用户名, 可省略
    • pass: 密码, 可省略
    • mongo: mongodb url

    如果不指定 field,默认为 MongoDB 的自增ID,且type为int,基于副本集配置,如下

    location /images/ {
        gridfs my_app field=filename type=string;
        mongo "foo"
            192.168.1.136:27017
            192.168.1.137:27017;
    }

    注意:由于MongoDB比nginx先启动的限制,在主节点mongodb掉点之后,要重新启动一下nginx,否则nginx-gridfs初始化的时候不能正确链接MOngoDB数据库。解决方式,做个监控脚本监控mongodb主节点状态,如果发现状态异常,nginx将自动reload。脚本如下:

    vi /usr/local/nginx/sbin/nginxrestart.sh
    #!/bin/bash
    # author wangzhaobin
    # time 20161208
    # program : 判断mongodb主节点进程是否存在,如果不存在重新启动nginx
    
    ps -ef |grep master.cfg |grep -v "grep" |wc -l
    
    if [ $? == 0 ]
    then
        /usr/local/nginx/sbin/nginx -s reload
    fi

    将此脚本加入定时任务,每隔十秒钟监测一次mongodb主节点状态

    crontab -e
    *  * * * * /bin/bash /usr/local/nginx/sbin/nginxrestart.sh
    * * * * * sleep 10; /bin/bash /usr/local/nginx/sbin/nginxrestart.sh
    * * * * * sleep 20; /bin/bash /usr/local/nginx/sbin/nginxrestart.sh
    * * * * * sleep 30; /bin/bash /usr/local/nginx/sbin/nginxrestart.sh
    * * * * * sleep 40; /bin/bash /usr/local/nginx/sbin/nginxrestart.sh
    * * * * * sleep 50; /bin/bash /usr/local/nginx/sbin/nginxrestart.sh
分类: MongoDB

8 条评论

hz88app · 2025-12-19 20:08

Downloaded the Hz88app and it’s surprisingly good. Runs smooth and the games are engaging. If you’re looking for a new app to try, hit up hz88app, you wont be disappointed!

ojwin4 · 2025-12-24 10:50

What’s up, players! I gave ojwin4 a look the other day and it’s got a clean layout. The games loaded quickly, and no lag. It’s nice! If you feel like it, visit ojwin4!

phtaya01 · 2026-01-14 19:07

phtaya01 https://www.phtaya01.org

77jili · 2026-01-14 20:45

77jili https://www.77jilig.net

nustar online · 2026-01-15 03:09

nustar online https://www.etnustar-online.com

matbet maç izle · 2026-01-31 14:05

Yo, Matbet has got you covered for watching the game. No need to scramble for dodgy streams anymore. Check it out matbet maç izle.

axiebet888 · 2026-01-31 14:06

Lately, I’ve been hitting up Axiebet888 through WinningPlusCasino. The games are solid, and the payouts came through without a problem, legit! Here’s the link: axiebet888

wowph11 com login · 2026-01-31 14:06

Struggling with the wowph11 com login. Hopefully it’s smooth sailing once I’m in! Heard the games are fire. wowph11 com login

发表回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注