如何转换单机mongod到复制集的工作模式
需要给服务器为192.168.56.10(PRIMARY),增加一个容灾的副本,SECONDARY地址为192.168.56.11(已安装好mongod软件)
在PRIMARY上干净的关闭数据库
> db.shutdownServer()
2016-01-16T21:52:02.997+0800 I NETWORK DBClientCursor::init call() failed
server should be down...
修改PRIMARY上的配置参数,增加如下内容
replSet=rep-test
定义以副本集的方式启动mongodb,本例子以rep-test命名
[root@c1 mongodb]# /opt/app/mongodb/sbin/mongod -f /opt/app/mongodb/conf/mongod63105.conf
修改后保存,再初始化replset
> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "c1:63105",
"ok" : 1
}
rep-test:PRIMARY>
rep-test:PRIMARY> rs.status()
{
"set" : "rep-test",
"date" : ISODate("2016-01-16T14:12:21.053Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "c1:63105",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 581,
"optime" : Timestamp(1452953513, 1),
"optimeDate" : ISODate("2016-01-16T14:11:53Z"),
"electionTime" : Timestamp(1452953513, 2),
"electionDate" : ISODate("2016-01-16T14:11:53Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
命令提示符变增加了PRIMARY.通过rs.status()可以看到当前的配置.
接下来我们需把192.168.56.11增加进来,以PRIMARY上的参数文件启动这个mongodb.
在PRIMARY上增加这个成员
rep-test:PRIMARY> rs.add("192.168.56.11:63105")
{ "ok" : 1 }
在secondary上的日志开始同步原有的数据库内容,
...
2016-02-25T21:04:16.679+0800 I REPL [rsSync] oplog sync 3 of 3
2016-02-25T21:04:16.680+0800 I REPL [rsSync] initial sync finishing up
2016-02-25T21:04:16.680+0800 I REPL [rsSync] replSet set minValid=569a51cf:1
2016-02-25T21:04:16.734+0800 I REPL [rsSync] initial sync done.
.......
rep-test:PRIMARY> rs.status()
{
"set" : "rep-test",
"date" : ISODate("2016-01-16T14:23:21.484Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "c1:63105",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1241,
"optime" : Timestamp(1452954063, 1),
"optimeDate" : ISODate("2016-01-16T14:21:03Z"),
"electionTime" : Timestamp(1452953513, 2),
"electionDate" : ISODate("2016-01-16T14:11:53Z"),
"configVersion" : 2,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.56.11:63105",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 137,
"optime" : Timestamp(1452954063, 1),
"optimeDate" : ISODate("2016-01-16T14:21:03Z"),
"lastHeartbeat" : ISODate("2016-01-16T14:23:20.354Z"),
"lastHeartbeatRecv" : ISODate("2016-01-16T14:23:19.593Z"),
"pingMs" : 0,
"configVersion" : 2
}
],
"ok" : 1
}
现在分别可以看到PRIMARY和SECONDARY的状态了.