MongoDB复制集之将现有的单节点服务器转换为复制集

服务器情况:

  现有的单节点 Primary     192.168.126.9:27017

  新增的节点    Secondry  192.168.126.8:27017

   仲裁节点      ARBITER   192.168.126.8:27018

    mongo 版本 3.2.4

1.停止单节点Primary的Mongo服务

 > use admin;
switched to db admin
> db.shutdownServer()

2.重新启动Primary节点,使用--replSet的选项加上新复制集的名字 repl1

 /usr/local/mongodb/bin/mongod -dbpath /sdb1/mongodb/data --fork --port 27017 --logpath /sdb1/mongodb/log/mongo.log --replSet repl1 --logappend 

3.连接Primary数据库

4. 创建复制集配置信息对象

 config = { _id:"repl1", members:[
{_id:0,host:"192.168.126.9:27017"}
]
}

5.初始化复制集并查看复制集的运行状态

 > rs.initiate(config)
{ "ok" : 1 } repl1:SECONDARY> rs.status()
{
"set" : "repl1",
"date" : ISODate("2016-06-23T16:05:10.228Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.126.9:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3524,
"optime" : {
"ts" : Timestamp(1466697870, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-06-23T16:04:30Z"),
"infoMessage" : "could not find member to sync from",
"electionTime" : Timestamp(1466697868, 2),
"electionDate" : ISODate("2016-06-23T16:04:28Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1

6.启动 Secondry Mongo节点服务

root@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/data --fork --port 27017 --logpath=/sdb1/mongodb/log/mongo.log --logappend --replSet repl1
about to fork child process, waiting until server is ready for connections. forked process: 2848 child process started successfully, parent exiting

7.在Primary节点将 Secondry Mongo节点添加到复制集里

repl1:PRIMARY> rs.add("192.168.126.8:27017")
{ "ok" : 1 }

8.启动 ARBITER Mongo仲裁节点

root@linux-mongo2:/home/ubuntu# mkdir -p /sdb1/mongodb/arb
root@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/arb --fork --port 27018 --logpath=/sdb1/mongodb/arb/mongo.log --logappend --relpSet replSet repl1
Error parsing command line: unrecognised option '--relpSet'
try '/usr/local/mongodb/bin/mongod --help' for more information
root@linux-mongo2:/home/ubuntu# /usr/local/mongodb/bin/mongod -dbpath=/sdb1/mongodb/arb --fork --port 27018 --logpath=/sdb1/mongodb/arb/mongo.log --logappend -replSet repl1
about to fork child process, waiting until server is ready for connections.
forked process: 3229
child process started successfully, parent exiting

9.在主节点将仲裁节点添加在复制集中,并查看最终的复制集状态

 repl1:PRIMARY> rs.addArb("192.168.126.8:27018")
{ "ok" : 1 } repl1:PRIMARY> rs.status()
{
"set" : "repl1",
"date" : ISODate("2016-06-23T16:27:55.358Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.126.9:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 4889,
"optime" : {
"ts" : Timestamp(1466699268, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2016-06-23T16:27:48Z"),
"electionTime" : Timestamp(1466697868, 2),
"electionDate" : ISODate("2016-06-23T16:04:28Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.126.8:27017",
"health" : 1,
"state" : 5,
"stateStr" : "STARTUP2",
"uptime" : 1005,
"optime" : {
"ts" : Timestamp(0, 0),
"t" : NumberLong(-1)
},
"optimeDate" : ISODate("1970-01-01T00:00:00Z"),
"lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),
"lastHeartbeatRecv" : ISODate("2016-06-23T16:27:54.561Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.126.9:27017",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "192.168.126.8:27018",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 6,
"lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),
"lastHeartbeatRecv" : ISODate("2016-06-23T16:27:53.632Z"),
"pingMs" : NumberLong(1),
"configVersion" : 3
}
],
"ok" : 1
}

注意: 这里副本集 192.168.126.8 的状态是 "STARTUP2" ,是由于Primary库的数据很大,导致一直处于初始化的同步数据阶段。 

详细说明如下

"_id" :  #集群中节点编号
"name" : #成员服务器名称及端口
"health" : #表示成员中的健康状态(0:down;1:up)
"state" : #为1~11,表示成员的当前状态
"stateStr" : #描述该成员是主库(PRIMARY)还是备库(SECONDARY)
"uptime" : #该成员在线时间(秒)
"optime" : #成员最后一次应用日志(oplog)的信息
"optimeDate" : #成员最后一次应用日志(oplog)的时间
"electionTime" : #当前primary从操作日志中选举信息
"electionDate" : #当前primary被选定为primary的日期
"configVersion" : #mongodb版本
"self" : #为true 表示当前节点 其中 state 复制集状态:
复制集状态:
STARTUP:刚加入到复制集中,配置还未加载
STARTUP2:配置已加载完,初始化状态
RECOVERING:正在恢复,不适用读
ARBITER: 仲裁者
DOWN:节点不可到达
UNKNOWN:未获取其他节点状态而不知是什么状态,一般发生在只有两个成员的架构,脑裂
REMOVED:移除复制集
ROLLBACK:数据回滚,在回滚结束时,转移到RECOVERING或SECONDARY状态
FATAL:出错。查看日志grep "replSet FATAL"找出错原因,重新做同步
PRIMARY:主节点
SECONDARY:备份节点
上一篇:忘记root密码的解决方法——进入单用户模式修改


下一篇:autojs之开启悬浮窗权限