MongoDB副本集的在线迁移
查看当前集群的状态:
{ "setName" : "gechongrepl", "setVersion" : 11, "ismaster" : true, "secondary" : false, "hosts" : [ "192.168.91.135:27017", "192.168.91.133:27017" ], "arbiters" : [ "192.168.91.144:27017" ], "primary" : "192.168.91.135:27017", "me" : "192.168.91.135:27017", "maxBsonObjectSize" : 16777216, "maxMessageSizeBytes" : 48000000, "maxWriteBatchSize" : 1000, "localTime" : ISODate("2015-06-17T02:29:52.582Z"), "maxWireVersion" : 2, "minWireVersion" : 0, "ok" : 1 }
当前的副本集的状态为:
Primary:192.168.91.135:27017
Arbiters:192.168.91.144:27017
Secondary:192.168.91.133:27017
需求1:在副本集正常工作的情况下再添加一个节点。
1:模拟业务数据
gechongrepl:PRIMARY> db.test.count() 109000
gechongrepl:PRIMARY> for (i=0;i<10000;i++) db.test.insert({uid:i,uname:'testmoveonline'+i}); WriteResult({ "nInserted" : 1 }) gechongrepl:PRIMARY> db.test.count() 119000 gechongrepl:PRIMARY>
查看从节点的状态:
gechongrepl:SECONDARY> rs.slaveOk() gechongrepl:SECONDARY> db.test.count() 119000
2:把从节点关掉
gechongrepl:SECONDARY> use admin switched to db admin gechongrepl:SECONDARY> db.shutdownServer() #查看当前的副本集的状态 gechongrepl:PRIMARY> rs.status() #再次插入数据 gechongrepl:PRIMARY> for (i=0;i<10000;i++) db.test.insert({uid:i,uname:'testmoveonline12345'+i}); WriteResult({ "nInserted" : 1 }) gechongrepl:PRIMARY> db.test.count() 129000
3:根据配置文件的配置,scpdata目录到新节点上。新节点IP为:192.168.91.132
[root@localhost mongodb]# scp -r ./data/ [email protected]:/root/software/mongodb/ [email protected]'s password: local.0 100% 64MB 21.3MB/s 00:03 local.1 100% 2047MB 21.8MB/s 01:34 test.0 100% 64MB 21.3MB/s 00:03 test.ns 100% 16MB 16.0MB/s 00:00 mongod.lock 100% 0 0.0KB/s 00:00 local.ns 100% 16MB 16.0MB/s 00:00 [root@localhost mongodb]# ls bin data GNU-AGPL-3.0 log mongod.conf README THIRD-PARTY-NOTICES [root@localhost mongodb]# scp -r ./log/ [email protected]:/root/software/mongodb/ [email protected]'s password: mongod 100% 1101KB 1.1MB/s 00:00 [root@localhost mongodb]#
查看192.168.91.132主机上的MongoDB目录已经存在scp过来的文件
[root@localhost mongodb]# ls bin data GNU-AGPL-3.0 log mongod.conf README THIRD-PARTY-NOTICES
4:用scp过来的物理文件启动新节点
[root@localhost mongodb]# cd bin/ [root@localhost bin]# ./mongod --config /root/software/mongodb/mongod.conf about to fork child process, waiting until server is ready for connections. forked process: 30944 child process started successfully, parent exiting
5:在集群几点中加入新节点即可
gechongrepl:PRIMARY> rs.add("192.168.91.132:27017") { "_id" : 6, "name" : "192.168.91.132:27017", "health" : 1, "state" : 6, "stateStr" : "UNKNOWN", "uptime" : 9, "optime" : Timestamp(0, 0), "optimeDate" : ISODate("1970-01-01T00:00:00Z"), "lastHeartbeat" : ISODate("2015-06-17T02:49:43Z"), "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"), "pingMs" : 0, "lastHeartbeatMessage" : "still initializing" }
6:remove刚才关掉的节点即可
gechongrepl:PRIMARY> rs.remove("192.168.91.133:27017")
如此再次重复这样的步骤即可把集群慢慢的转移到其他服务器上。