在mongodb1.6之后就不在推荐使用mongodb的主从复制而是推荐使用Replica Set (副本集),但是在一定条件主从复制还是比较好用,在前期数据量很低的情况是很好的选择。
1.首先配置master服务器:
#r0.conf
dbpath=/sdb1/liujijun/data/data/r0/
logpath=/sdb1/liujijun/data/log/r0.log
#ikeyFile=/sdb1/liujijun/data/key/r0
#upgrade=true
port=28010
oplogSize=500
directoryperdb=true
#replSet=rs1
master=true
fork=true
auth=true
logappend=true
maxConns=800
2:配置slave服务器:
#r1.conf
dbpath=/home/mongodb/data/
logpath=/home/mongodb/log/log.log
port=28015
directoryperdb=true
source=192.168.0.8:28010
oplogSize=100
auth=true
slave=true
fork=true
logappend=true
maxConns=800
然后分别启动主从服务器就可以了。
关于安全:
这是mongodb的官网上的介绍:
Security
When security is enabled, one must configure a user account for the local database that exists on both servers.
The slave-side of a replication connection first looks for a user repl in local.system.users. If present, that user is used to authenticate against the local database on the source side of the connection. If repl user does not exist, the first user object in local.system.users is tried.
The local database works like the admin database: an account for local has access to the entire server.
Example security configuration when security is enabled:
$ mongo <slavehostname>/admin -u <existingadminusername> -p<adminpassword>
> use local
> db.addUser('repl', <replpassword>);
^c
$ mongo <masterhostname>/admin -u <existingadminusername> -p<adminpassword>
> use local
> db.addUser('repl', <replpassword>);