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服务 

1 > use admin;  
2 switched to db admin  
3 > 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. 创建复制集配置信息对象

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

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

1 > rs.initiate(config)
 2 { "ok" : 1 }
 3 
 4 repl1:SECONDARY> rs.status()
 5 {
 6         "set" : "repl1",
 7         "date" : ISODate("2016-06-23T16:05:10.228Z"),
 8         "myState" : 1,
 9         "term" : NumberLong(1),
10         "heartbeatIntervalMillis" : NumberLong(2000),
11         "members" : [
12                 {
13                         "_id" : 0,
14                         "name" : "192.168.126.9:27017",
15                         "health" : 1,
16                         "state" : 1,
17                         "stateStr" : "PRIMARY",
18                         "uptime" : 3524,
19                         "optime" : {
20                                 "ts" : Timestamp(1466697870, 1),
21                                 "t" : NumberLong(1)
22                         },
23                         "optimeDate" : ISODate("2016-06-23T16:04:30Z"),
24                         "infoMessage" : "could not find member to sync from",
25                         "electionTime" : Timestamp(1466697868, 2),
26                         "electionDate" : ISODate("2016-06-23T16:04:28Z"),
27                         "configVersion" : 1,
28                         "self" : true
29                 }
30         ],
31         "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.在主节点将仲裁节点添加在复制集中,并查看最终的复制集状态

1 repl1:PRIMARY> rs.addArb("192.168.126.8:27018")
 2 { "ok" : 1 }
 3 
 4 repl1:PRIMARY> rs.status()
 5 {
 6         "set" : "repl1",
 7         "date" : ISODate("2016-06-23T16:27:55.358Z"),
 8         "myState" : 1,
 9         "term" : NumberLong(1),
10         "heartbeatIntervalMillis" : NumberLong(2000),
11         "members" : [
12                 {
13                         "_id" : 0,
14                         "name" : "192.168.126.9:27017",
15                         "health" : 1,
16                         "state" : 1,
17                         "stateStr" : "PRIMARY",
18                         "uptime" : 4889,
19                         "optime" : {
20                                 "ts" : Timestamp(1466699268, 1),
21                                 "t" : NumberLong(1)
22                         },
23                         "optimeDate" : ISODate("2016-06-23T16:27:48Z"),
24                         "electionTime" : Timestamp(1466697868, 2),
25                         "electionDate" : ISODate("2016-06-23T16:04:28Z"),
26                         "configVersion" : 3,
27                         "self" : true
28                 },
29                 {
30                         "_id" : 1,
31                         "name" : "192.168.126.8:27017",
32                         "health" : 1,
33                         "state" : 5,
34                         "stateStr" : "STARTUP2",
35                         "uptime" : 1005,
36                         "optime" : {
37                                 "ts" : Timestamp(0, 0),
38                                 "t" : NumberLong(-1)
39                         },
40                         "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
41                         "lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),
42                         "lastHeartbeatRecv" : ISODate("2016-06-23T16:27:54.561Z"),
43                         "pingMs" : NumberLong(0),
44                         "syncingTo" : "192.168.126.9:27017",
45                         "configVersion" : 3
46                 },
47                 {
48                         "_id" : 2,
49                         "name" : "192.168.126.8:27018",
50                         "health" : 1,
51                         "state" : 7,
52                         "stateStr" : "ARBITER",
53                         "uptime" : 6,
54                         "lastHeartbeat" : ISODate("2016-06-23T16:27:54.499Z"),
55                         "lastHeartbeatRecv" : ISODate("2016-06-23T16:27:53.632Z"),
56                         "pingMs" : NumberLong(1),
57                         "configVersion" : 3
58                 }
59         ],
60         "ok" : 1
61 }

注意: 这里副本集 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:备份节点

更多MongoDB相关教程见以下内容

MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里

相关推荐