mongoDB-3.x Master
Slave Replication
官方文档:
注意:虽然,3.x仍然支持master-slave模式,但因为不能自动Failover等相关限制,官方推荐使用Repica
Set模式
IMPORTANT
Replica
sets
环境:
CentOS6.5 x64
mongoDB-3.2.0
安装请参看mongoDB-3.x安装配置(单机版)
可以有多台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
调试信息一览
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"}
);
db.sources.update( { host : "192.168.192.10:27010"
},
rs.printSlaveReplicationInfo ()
测试一:master宕机
master mongo shell执行
db.adminCommand({shutdown : 1, force : true})
测试二.Failing over to a
Slave
To permanently failover from a unavailable or
damaged
-
Shut down
A. -
Stop
mongod on B. -
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. -
Restart
mongod on B with the --master option.
NOTE
This is a one time
operation, and is not reversible.
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