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库的数据很大,导致一直处于初始化的同步数据阶段。 

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/2a88b5422f8a9257285c348463a50116.html