下载软件:

wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.0.6.tgz

1. 传统模式的主从复制配置:

master 配置:

port = 29017
dbpath = /usr/local/mongo/data/db_29017
logpath = /usr/local/mongo/data/db_29017/mongo.log
logappend = true
fork = true
pidfilepath = /usr/local/mongo/data/db_29017/mongo.pid
journal = true
oplogSize = 1024
master = true
autoresync = true

slave 配置 :

port = 29027
dbpath = /usr/local/mongo/data/db_29017
logpath = /usr/local/mongo/data/db_29017/mongo.log
logappend = true
fork = true
pidfilepath = /usr/local/mongo/data/db_29017/mongo.pid
journal = true
oplogSize = 1024
master = true
autoresync = true
slave = true
source = localhost:29017 #主服务器地址:端口号

2. mongodb 副本配置

选择三台主机

Host01 Host02 Host03

Host01 部署:

port = 29017
dbpath = /usr/local/mongo/data/db_29017
logpath = /usr/local/mongo/data/db_29017/mongo.log
logappend = true
fork = true
pidfilepath = /usr/local/mongo/data/db_29017/mongo.pid
journal = true
replSet = rs0
oplogSize = 1024

Host02 部署:

port = 29027
dbpath = /usr/local/mongo/data/db_29027
logpath = /usr/local/mongo/data/db_29027/mongo.log
logappend = true
fork = true
pidfilepath = /usr/local/mongo/data/db_29027/mongo.pid
journal = true
replSet = rs0
oplogSize = 1024

Host03 部署:

port = 29037
dbpath = /usr/local/mongo/data/db_29037
logpath = /usr/local/mongo/data/db_29037/mongo.log
logappend = true
fork = true
pidfilepath = /usr/local/mongo/data/db_29037/mongo.pid
journal = true
replSet = rs0
oplogSize = 1024

三台主机配置完成之后并启动服务,连接到其中任何一台主机进行如下配置,此例,我们连接Host1进行配置:

> rs.initiate();
> rs.add("Host2:29017");
> rs.add("Host3:29017");
> rs.status();

或者:
cfg = rs.conf()

cfg={
 "_id" : "rs0",
 "version" : 1,
 "members" : [
  {
   "_id" : 0,
   "host" : "Host1:29017",
  },
  {
   "_id" : 1,
   "host" : "Host2:29027",
  },
  {
   "_id" : 2,
   "host" : "Host3:29037"
  }
 ]}

rs.initiate(cfg)