1、 向 Mongodb 集群添加新的成员节点向集群添加新节点有两种方法:方 法第一种方法 拷贝其他节点的数据文件到新的节点第二种方法 向已有的 replica set 添加新成员一第一种方法(拷贝其他节点的数据文件到新的节点)我们在启动 mongod 服务之前一定要拷贝数据文件到本机。下面是空库添加到一个 replica set 中的日志记录:第一步:# 连接 Primary 库Tue Nov 1 14:22:57 initandlisten connection accepted from 10.250.7.220:54235 #2Tue Nov 1 14:22:57 initandliste
2、n connection accepted from 10.250.7.220:54236 #3Tue Nov 1 14:22:57 initandlisten connection accepted from 10.250.7.220:54237 #4Tue Nov 1 14:22:57 rsStart trying to contact 10.250.7.220:27018Tue Nov 1 14:22:57 rsStart trying to contact 10.250.7.220:27019Tue Nov 1 14:22:57 rsStart trying to contact 10
3、.250.7.220:27020第二步:#从 primary 库复制 replica set 的配置信息,并保存到本地 -dbpath 指定的文件目录#Tue Nov 1 14:22:57 rsStart replSet got config version 2 from a remote, saving locallyTue Nov 1 14:22:57 rsStart replSet info saving a newer config version to local.system.replsetTue Nov 1 14:22:57 FileAllocator allocating ne
4、w datafile /opt/mongodata/r1/local.ns, filling with zeroes.Tue Nov 1 14:22:57 FileAllocator creating directory /opt/mongodata/r1/_tmpTue Nov 1 14:22:57 FileAllocator done allocating datafile /opt/mongodata/r1/local.ns, size: 16MB, took 0.118 secsTue Nov 1 14:22:57 FileAllocator allocating new datafi
5、le /opt/mongodata/r1/local.0, filling with zeroes.Tue Nov 1 14:23:01 FileAllocator done allocating datafile /opt/mongodata/r1/local.0, size: 64MB, took 3.807 secsTue Nov 1 14:23:01 FileAllocator allocating new datafile /opt/mongodata/r1/local.1, filling with zeroes.Tue Nov 1 14:23:02 rsStart replSet
6、 saveConfigLocally doneTue Nov 1 14:23:02 FileAllocator done allocating datafile /opt/mongodata/r1/local.1, size: 128MB, took 1.068 secsTue Nov 1 14:23:02 rsStart replSet STARTUP2Tue Nov 1 14:23:02 rsMgr replSet total number of votes is even - add arbiter or give one member an extra voteTue Nov 1 14
7、:23:02 rsSync *第三步:#创建 oplog 日志文件#Tue Nov 1 14:23:02 rsSync creating replication oplog of size: 944MB.Tue Nov 1 14:23:02 FileAllocator allocating new datafile /opt/mongodata/r1/local.2, filling with zeroes.Tue Nov 1 14:23:04 rsHealthPoll replSet info member 10.250.7.220:27018 is upTue Nov 1 14:23:04
8、 rsHealthPoll replSet member 10.250.7.220:27018 is now in state SECONDARYTue Nov 1 14:23:04 rsHealthPoll replSet info member 10.250.7.220:27019 is upTue Nov 1 14:23:04 rsHealthPoll replSet member 10.250.7.220:27019 is now in state SECONDARYTue Nov 1 14:23:04 rsHealthPoll replSet info member 10.250.7
9、.220:27020 is upTue Nov 1 14:23:04 rsHealthPoll replSet member 10.250.7.220:27020 is now in state PRIMARYTue Nov 1 14:23:49 FileAllocator done allocating datafile /opt/mongodata/r1/local.2, size: 1024MB, took 46.28 secsTue Nov 1 14:23:50 rsSync *第四步:#应用主库的日志,复制数据文件#Tue Nov 1 14:23:50 rsSync replSet
10、initial sync pendingTue Nov 1 14:23:50 rsSync replSet syncing to: 10.250.7.220:27020Tue Nov 1 14:23:50 rsSync build index local.me _id: 1 Tue Nov 1 14:23:50 rsSync build index done 0 records 0.003 secsTue Nov 1 14:23:50 rsSync replSet initial sync drop all databasesTue Nov 1 14:23:50 rsSync dropAllD
11、atabasesExceptLocal 1Tue Nov 1 14:23:50 rsSync replSet initial sync clone all databasesTue Nov 1 14:23:50 rsSync replSet initial sync cloning db: testTue Nov 1 14:23:50 FileAllocator allocating new datafile /opt/mongodata/r1/test.ns, filling with zeroes.Tue Nov 1 14:23:51 FileAllocator done allocati
12、ng datafile /opt/mongodata/r1/test.ns, size: 16MB, took 0.8 secsTue Nov 1 14:23:51 FileAllocator allocating new datafile /opt/mongodata/r1/test.0, filling with zeroes.Tue Nov 1 14:23:55 FileAllocator done allocating datafile /opt/mongodata/r1/test.0, size: 64MB, took 3.643 secsTue Nov 1 14:23:55 Fil
13、eAllocator allocating new datafile /opt/mongodata/r1/test.1, filling with zeroes.Tue Nov 1 14:23:55 rsSync build index test.yql _id: 1 Tue Nov 1 14:23:56 rsSync build index done 1 records 0.006 secsTue Nov 1 14:23:56 rsSync replSet initial sync query minValidTue Nov 1 14:23:56 rsSync replSet initial
14、 oplog application from 10.250.7.220:27020 starting at Nov 1 14:14:05:1 to Nov 1 14:14:05:1Tue Nov 1 14:23:57 rsSync replSet initial sync finishing upTue Nov 1 14:23:57 rsSync replSet set minValid=4eaf8e2d:1Tue Nov 1 14:23:57 rsSync build index local.replset.minvalid _id: 1 Tue Nov 1 14:23:57 rsSync
15、 build index done 0 records 0.023 secsTue Nov 1 14:23:57 rsSync replSet initial sync doneTue Nov 1 14:23:58 rsSync replSet syncing to: 10.250.7.220:27020Tue Nov 1 14:23:58 rsSync replSet SECONDARYTue Nov 1 14:24:02 FileAllocator done allocating datafile /opt/mongodata/r1/test.1, size: 128MB, took 7.
16、428 secsTue Nov 1 14:24:16 clientcursormon mem (MB) res:16 virt:2848 mapped:1312Tue Nov 1 14:24:42 initandlisten connection accepted from 127.0.0.1:21141 #5Tue Nov 1 14:29:16 clientcursormon mem (MB) res:16 virt:2849 mapped:1312Tue Nov 1 14:34:16 clientcursormon mem (MB) res:16 virt:2913 mapped:1312
17、Tue Nov 1 14:39:16 clientcursormon mem (MB) res:16 virt:2913 mapped:1312第五步:=下面是删除节点以后的日志=Tue Nov 1 14:39:28 conn2 end connection 10.250.7.220:54235Tue Nov 1 14:39:29 rsMgr replset msgReceivedNewConfig version: version: 3Tue Nov 1 14:39:29 rsMgr replSet info saving a newer config version to local.sy
18、stem.replsetTue Nov 1 14:39:29 rsMgr replSet saveConfigLocally doneTue Nov 1 14:39:29 rsMgr replSet error self not present in the repl set configuration:Tue Nov 1 14:39:29 rsMgr _id: “myset“, version: 3, members: _id: 0, host: “10.250.7.220:27018“ , _id: 1, host: “10.250.7.220:27019“ , _id: 2, host:
19、 “10.250.7.220:27020“ -这是拷贝 replica set myset 中其他节点的数据文件!(记住是数据文件,一定不要mongod.lock ,每个 mongod 进程都需要自己的数据目录,如果你要运行3个mongod 的实例,那么就需要3个独自的目录。mongod 启动的时候会在数据目录创建一个 mongod.lock 文件,阻止其他进程使用此目录.)第六步:#连接 primary 库Tue Nov 1 15:53:24 initandlisten connection accepted from 10.250.7.220:54761 #2Tue Nov 1 15:53
20、:28 initandlisten connection accepted from 10.250.7.220:54763 #3Tue Nov 1 15:53:28 initandlisten connection accepted from 10.250.7.220:54764 #4Tue Nov 1 15:53:34 clientcursormon mem (MB) res:31 virt:2606 mapped:1232Tue Nov 1 15:53:34 rsStart trying to contact 10.250.7.220:27018Tue Nov 1 15:53:34 rsS
21、tart trying to contact 10.250.7.220:27019Tue Nov 1 15:53:34 rsStart trying to contact 10.250.7.220:27020第七步:#拷贝配置文件,并保存。Tue Nov 1 15:53:34 rsStart replSet got config version 6 from a remote, saving locallyTue Nov 1 15:53:34 rsStart replSet info saving a newer config version to local.system.replsetTu
22、e Nov 1 15:53:34 rsStart replSet saveConfigLocally doneTue Nov 1 15:53:34 rsStart replSet STARTUP2Tue Nov 1 15:53:34 rsMgr replSet total number of votes is even - add arbiter or give one member an extra voteTue Nov 1 15:53:34 rsSync replSet SECONDARYTue Nov 1 15:53:34 rsHealthPoll replSet info membe
23、r 10.250.7.220:27018 is upTue Nov 1 15:53:34 rsHealthPoll replSet member 10.250.7.220:27018 is now in state SECONDARYTue Nov 1 15:53:34 rsHealthPoll replSet info member 10.250.7.220:27019 is upTue Nov 1 15:53:34 rsHealthPoll replSet member 10.250.7.220:27019 is now in state SECONDARYTue Nov 1 15:53:
24、34 rsHealthPoll replSet info member 10.250.7.220:27020 is upTue Nov 1 15:53:34 rsHealthPoll replSet member 10.250.7.220:27020 is now in state PRIMARYTue Nov 1 15:53:34 rsMgr replSet cant see a majority, will not try to elect selfTue Nov 1 15:53:38 rsSync replSet syncing to: 10.250.7.220:27020从上面的日志来
25、看,这种方法初始化的速度比较快。当然考虑拷贝数据文件的时间,在大量数据的时候,我们可以选择里面一种方案!二第二种方法(向已有的 replica set 添加新成员)向已有的 replica set 添加新成员是很简单的,新加的成员可以是空的。下面介绍一下如何添加新成员:添加新成员时,必须在新的机器上先起来一个 mongodb 服务,必须指出要加入的replica set 集群的名字基本语法如下(其他是默认的),然后再在 replica set 的 master 库上执行添加操作:./mongod -dbpath=/opt/mongodata/r1 -port 27017 -replSet my
26、set当然也可以使用下面的命令,将输出日志记录到/opt/mongodata/r1/rac3.27017.log 里面!mongodbrac3 bin$./mongod -dbpath=/opt/mongodata/r1 -port 27017 -replSet myset -logpath=/opt/mongodata/r1/rac3.27017.log -logappend “set“ : “myset“,“date“ : ISODate(“2011-11-01T06:13:01Z“),“myState“ : 1,“syncingTo“ : “10.250.7.220:27018“,“me
27、mbers“ : “_id“ : 0,“name“ : “10.250.7.220:27018“,“health“ : 1,“state“ : 2,“stateStr“ : “SECONDARY“,“uptime“ : 62668,“optime“ : “t“ : 1320066230000,“i“ : 1,“optimeDate“ : ISODate(“2011-10-31T13:03:50Z“),“lastHeartbeat“ : ISODate(“2011-11-01T06:13:01Z“),“pingMs“ : 0,“_id“ : 1,“name“ : “10.250.7.220:27
28、019“,“health“ : 1,“state“ : 2,“stateStr“ : “SECONDARY“,“uptime“ : 63887,“optime“ : “t“ : 1320066230000,“i“ : 1,“optimeDate“ : ISODate(“2011-10-31T13:03:50Z“),“lastHeartbeat“ : ISODate(“2011-11-01T06:13:01Z“),“pingMs“ : 0,“_id“ : 2,“name“ : “10.250.7.220:27020“,“health“ : 1,“state“ : 1,“stateStr“ : “
29、PRIMARY“,“optime“ : “t“ : 1320066230000,“i“ : 1,“optimeDate“ : ISODate(“2011-10-31T13:03:50Z“),“self“ : true,“ok“ : 1PRIMARY rs.add(“10.250.7.241:27017“); “ok“ : 1 -添加成功添加之后,mongodb 会自动执行新加节点的恢复操作,所以第一次查询 rs.status()时候,新加节点的信息为空,等待恢复完成之后再次查询就有了基本信息了。PRIMARY rs.status();省略 “_id“ : 2,“name“ : “10.250.
30、7.220:27020“,“health“ : 1,“state“ : 1,“stateStr“ : “PRIMARY“,“optime“ : “t“ : 1320128045000,“i“ : 1,“optimeDate“ : ISODate(“2011-11-01T06:14:05Z“),“self“ : true,“_id“ : 3,“name“ : “10.250.7.241:27017“,“health“ : 0,“state“ : 8,“stateStr“ : “(not reachable/healthy)“,“uptime“ : 0,“optime“ : “t“ : 0,“i“
31、 : 0,“optimeDate“ : ISODate(“1970-01-01T00:00:00Z“),“lastHeartbeat“ : ISODate(“2011-11-01T06:14:07Z“),“pingMs“ : 0,“errmsg“ : “still initializing“,“ok“ : 1PRIMARY rs.status();“set“ : “myset“,“date“ : ISODate(“2011-11-01T06:16:08Z“),“myState“ : 1,“syncingTo“ : “10.250.7.220:27018“,“members“ : .省略“_id
32、“ : 3,“name“ : “10.250.7.241:27017“,“health“ : 1,“state“ : 2,“stateStr“ : “SECONDARY“,“uptime“ : 117,“optime“ : “t“ : 1320128045000,“i“ : 1,“optimeDate“ : ISODate(“2011-11-01T06:14:05Z“),“lastHeartbeat“ : ISODate(“2011-11-01T06:16:07Z“),“pingMs“ : 0,“ok“ : 1PRIMARY NOTE:这里只是测试了使用空库来添加新节点的,当然也可以从其他节点拷贝数据文件过来,这样可以更快的和主库进行同步操作,从而节省恢复时间!