rails for mongodb Replica Set 配置

配置 mongodb

sudo vi /etc/mongodb.conf

port = 27017        #//监听端口  
fork = true         #//后台运行  
pidfilepath = /var/run/mongodb/mongodb.pid    # 进程PID文件  
logpath = /var/log/mongodb/mongodb.log        # 日志文件  
dbpath =/var/lib/mongodb           # db存放目录  
journal = true                   # 存储模式  
nohttpinterface = true           # 禁用http  
directoryperdb=true              # 一个数据库一个文件夹  
logappend=true                   # 追加方式写日志  
replSet=repmore                  # 集群名称,自定义  
oplogSize=1000                   # oplog大小  

sudo vi /etc/mongodb_2.conf

port = 27018  
fork = true  
pidfilepath = /var/run/mongodb/mongodb_2.pid  
logpath = /var/log/mongodb/mongodb_2.log  
dbpath =/var/lib/mongodb_2  
journal = true  
nohttpinterface = true  
directoryperdb=true  
logappend=true  
replSet=repmore  
oplogSize=1000  

sudo vi /etc/mongodb_3.conf

port = 27019  
fork = true  
pidfilepath = /var/run/mongodb/mongodb_3.pid  
logpath = /var/log/mongodb/mongodb_3.log  
dbpath =/var/lib/mongodb_3  
journal = true  
nohttpinterface = true  
oplogSize = 1000  
directoryperdb=true  
logappend=true  
replSet=repmore  

创建目录

sudo mkdir -p /var/run/mongodb/
sudo mkdir -p /var/lib/mongodb_2
sudo mkdir -p /var/lib/mongodb_3

启动服务器

sudo mongod -f /etc/mongodb.conf &
sudo mongod -f /etc/mongodb_2.conf &
sudo mongod -f /etc/mongodb_3.conf &

配置并初始化replica set

$ mongo
> config = {_id:"repmore",members:[{_id:0,host:'127.0.0.1:27017',priority :2},{_id:1,host:'127.0.0.1:27018',priority:1},{_id:2,host:'127.0.0.1:27019',priority:1}]}  
> rs.initiate(config);  

{  
    "info" : "Config now saved locally.  Should come online in about a minute.",  
    "ok" : 1  
}  

导入数据库

mongorestore -d qiangda_prod ./qiangda_prod

$ du -sh /var/lib/mongodb/
3.5G    /var/lib/mongodb/

$ du -sh /var/lib/mongodb_2/
3.5G    /var/lib/mongodb_2/

$ du -sh /var/lib/mongodb_3/
3.5G    /var/lib/mongodb_3/

配置rails mondoid.yml

vi config/mongoid.yml

development:
  sessions:
    default:
      database: dev
      hosts:
        - 127.0.0.1:27017
        - 127.0.0.1:27018
        - 127.0.0.1:27019

ref:

Comments