mongoDB-3.x Master Slave Replication

mongoDB-3.x Master Slave Replication

官方文档:

注意:虽然,3.x仍然支持master-slave模式,但因为不能自动Failover等相关限制,官方推荐使用Repica Set模式

IMPORTANT

Replica sets replace master-slave replication for most use cases. If possible, use replica sets rather than master-slave replication for all new production deployments. This documentation remains to support legacy deployments and for archival purposes only.


环境:
CentOS6.5 x64
mongoDB-3.2.0


可以有多台slave

Master: 192.168.192.10
mongod --master --port 27010 --fork --httpinterface --rest --jsonp --setParameter enableLocalhostAuthBypass=0 --pidfilepath /opt/mongodb/mongod-ms.pid --dbpath /opt/mongodb/db-ms --logpath /opt/mongodb/log/mongod-ms.log  --logappend --logRotate rename --timeStampFormat ctime
mongoDB-3.x <wbr>Master <wbr>Slave <wbr>Replication

调试信息一览

rs.printReplicationInfo()


Slave: 192.168.192.11
mongod --slave --source 192.168.192.10:27010 --port 27010 --fork --httpinterface --rest --jsonp --setParameter enableLocalhostAuthBypass=0 --pidfilepath /opt/mongodb/mongod-ms.pid --dbpath /opt/mongodb/db-ms --logpath /opt/mongodb/log/mongod-ms.log  --logappend --logRotate rename --timeStampFormat ctime
注意:--source参数只需要在第一次启动时加上,之后重启只需要--slave参数
在不指定--source的参数的情况下,在mongo shell下手动指定master
use local
db.sources.find()
db.sources.insert( { host: "192.168.192.10:27010"} );
mongoDB-3.x <wbr>Master <wbr>Slave <wbr>Replication

更新master
db.sources.update( { host : "192.168.192.10:27010" },
                   { $set : { host : "192.168.192.10:27018" } } )


rs.printSlaveReplicationInfo()

测试一:master宕机
master mongo shell执行
db.adminCommand({shutdown : 1, force : true})
mongoDB-3.x <wbr>Master <wbr>Slave <wbr>Replication

mongoDB-3.x <wbr>Master <wbr>Slave <wbr>Replication

mongoDB-3.x <wbr>Master <wbr>Slave <wbr>Replication

master宕机后,仍然可以查询,但不能写入

测试二.Failing over to a Slave

To permanently failover from a unavailable or damaged master (A in the following example) to a slave (B):

  1. Shut down A.

  2. Stop mongod on B.

  3. Back up and move all data files that begin with local on B from the dbPath.

    WARNING

    Removing local.* is irrevocable and cannot be undone. Perform this step with extreme caution.

  4. Restart mongod on B with the --master option.

NOTE

This is a one time operation, and is not reversible. A cannot become a slave of B until it completes a full resync.

mongod --shutdown --dbpath /opt/mongodb/db-ms/
mongod --master  --port 27010 --fork --httpinterface --rest --jsonp --setParameter enableLocalhostAuthBypass=0 --pidfilepath /opt/mongodb/mongod-ms.pid --dbpath /opt/mongodb/db-ms --logpath /opt/mongodb/log/mongod-ms.log  --logappend --logRotate rename --timeStampFormat ctime
mongoDB-3.x <wbr>Master <wbr>Slave <wbr>Replication


posted @ 2016-01-07 13:41  李庆喜  阅读(285)  评论(0编辑  收藏  举报