master-slave复制模式大多场景下都被replicat sets代替。官方也建议使用replicat sets。
master-slave复制不支持自动failover。
master-slave部署
1.启动master节点
# ./mongod --master --dbpath /mongodb320/master --port=27017
启动后,会创建local.oplog.$main集合,存放操作日志
2.启动slave节点
# ./mongod --slave --source 12.12.12.195:27017 --dbpath /mongodb320/slave --port=27018 或者启动后设置 # ./mongod --dbpath /mongodb320/slave --port=27018 # ./mongo --port=27018 > use localu switched to db local > db.sources.find() > db.sources.insert({"host":"12.12.12.195:27017"})db.sources.insert({"host":"12.12.12.195:27017"}) WriteResult({ "nInserted" : 1 }) > db.sources.find() { "_id" : ObjectId("56e77cfa282f6e26c2c2b5c8"), "host" : "12.12.12.195:27017" } >
3.查看master-slave状态
#登陆master节点 > rs.printReplicationInfo() configured oplog size: 1023.9609375MB log length start to end: 904secs (0.25hrs) oplog first event time: Tue Mar 15 2016 10:45:17 GMT+0800 (CST) oplog last event time: Tue Mar 15 2016 11:00:21 GMT+0800 (CST) now: Tue Mar 15 2016 11:00:27 GMT+0800 (CST) #登陆slave节点 > rs.printReplicationInfo() this is a slave, printing slave replication info. source: 12.12.12.195:27017 syncedTo: Tue Mar 15 2016 11:04:21 GMT+0800 (CST) 6 secs (0 hrs) behind the freshest member (no primary available at the moment) > rs.printSlaveReplicationInfo()rs.printSlaveReplicationInfo() source: 12.12.12.195:27017 syncedTo: Tue Mar 15 2016 11:16:21 GMT+0800 (CST) 4 secs (0 hrs) behind the freshest member (no primary available at the moment) >