节点一:10.4.4.4:27017
节点二:10.4.4.5:27017
节点三:10.4.4.6:27017
创建三个节点组成的副本集,竟然产生以下结果:
./mongo 10.4.4.4:27017
查看rs
rs.status()
{
"set" : "mongodb_set",
"date" : ISODate("2014-04-04T06:44:10Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.4.4.4:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 903,
"optime" : Timestamp(1396592962, 1),
"optimeDate" : ISODate("2014-04-04T06:29:22Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.4.4.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 883,
"optime" : Timestamp(1396593341, 1416),
"optimeDate" : ISODate("2014-04-04T06:35:41Z"),
"lastHeartbeat" : ISODate("2014-04-04T06:44:09Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0
}
],
"ok" : 1
}
这个状态之一存在,直到关闭当前连接。
./mongo 10.4.4.5:27017
查看rs
rs.status(){
"set" : "mongodb_set",
"date" : ISODate("2014-04-04T06:43:45Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.4.4.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 878,
"optime" : Timestamp(1396593341, 1416),
"optimeDate" : ISODate("2014-04-04T06:35:41Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.4.4.6:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 858,
"optime" : Timestamp(1396593341, 1416),
"optimeDate" : ISODate("2014-04-04T06:35:41Z"),
"lastHeartbeat" : ISODate("2014-04-04T06:43:45Z"),
"lastHeartbeatRecv" : ISODate("2014-04-04T06:43:45Z"),
"pingMs" : 0,
"syncingTo" : "10.4.4.5:27017"
}
],
"ok" : 1
}
以上现象非常奇怪,副本集id都是一致的,结果产生了两个副本集,都是独立的,而且更奇怪的是,其中有个一副本集具有两个primary。
测试以下两个副本集是否相通:
在第二个副本集上执行数据插入
for(i=1;i<10000;++i){
db.person.insert({
id: i,
name: "mongodb"+i
} )
}
在第二个副本集上查询,结果:
>db.person.find()
{ "_id" : ObjectId("533e51da0fb65d4623fe3607"), "id" : 1, "name" : "mongodb1" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3608"), "id" : 2, "name" : "mongodb2" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3609"), "id" : 3, "name" : "mongodb3" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360a"), "id" : 4, "name" : "mongodb4" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360b"), "id" : 5, "name" : "mongodb5" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360c"), "id" : 6, "name" : "mongodb6" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360d"), "id" : 7, "name" : "mongodb7" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360e"), "id" : 8, "name" : "mongodb8" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360f"), "id" : 9, "name" : "mongodb9" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3610"), "id" : 10, "name" : "mongodb10" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3611"), "id" : 11, "name" : "mongodb11" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3612"), "id" : 12, "name" : "mongodb12" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3613"), "id" : 13, "name" : "mongodb13" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3614"), "id" : 14, "name" : "mongodb14" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3615"), "id" : 15, "name" : "mongodb15" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3616"), "id" : 16, "name" : "mongodb16" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3617"), "id" : 17, "name" : "mongodb17" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3618"), "id" : 18, "name" : "mongodb18" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3619"), "id" : 19, "name" : "mongodb19" }
{ "_id" : ObjectId("533e51da0fb65d4623fe361a"), "id" : 20, "name" : "mongodb20" }
Type "it" for more
而同时在第一个副本集上查询,结果为空,表明它与第二副本集数据不是相通的。
两个副本集同时关闭,再重启,
./mongo 10.4.4.4:27017
查看rs
>rs.status()
{
"set" : "mongodb_set",
"date" : ISODate("2014-04-04T06:58:22Z"),
"myState" : 2,
"syncingTo" : "10.4.4.5:27017",
"members" : [
{
"_id" : 0,
"name" : "10.4.4.4:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 135,
"optime" : Timestamp(1396594084, 4268),
"optimeDate" : ISODate("2014-04-04T06:48:04Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.4.4.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 133,
"optime" : Timestamp(1396594084, 4268),
"optimeDate" : ISODate("2014-04-04T06:48:04Z"),
"lastHeartbeat" : ISODate("2014-04-04T06:58:21Z"),
"lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
"pingMs" : 0
}
],
"ok" : 1
}
执行
>db.getMongo().setSlaveOk();
>db.person.find()
{ "_id" : ObjectId("533e51da0fb65d4623fe3607"), "id" : 1, "name" : "mongodb1" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3608"), "id" : 2, "name" : "mongodb2" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3609"), "id" : 3, "name" : "mongodb3" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360a"), "id" : 4, "name" : "mongodb4" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360b"), "id" : 5, "name" : "mongodb5" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360c"), "id" : 6, "name" : "mongodb6" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360d"), "id" : 7, "name" : "mongodb7" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360e"), "id" : 8, "name" : "mongodb8" }
{ "_id" : ObjectId("533e51da0fb65d4623fe360f"), "id" : 9, "name" : "mongodb9" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3610"), "id" : 10, "name" : "mongodb10" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3611"), "id" : 11, "name" : "mongodb11" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3612"), "id" : 12, "name" : "mongodb12" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3613"), "id" : 13, "name" : "mongodb13" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3614"), "id" : 14, "name" : "mongodb14" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3615"), "id" : 15, "name" : "mongodb15" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3616"), "id" : 16, "name" : "mongodb16" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3617"), "id" : 17, "name" : "mongodb17" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3618"), "id" : 18, "name" : "mongodb18" }
{ "_id" : ObjectId("533e51da0fb65d4623fe3619"), "id" : 19, "name" : "mongodb19" }
{ "_id" : ObjectId("533e51da0fb65d4623fe361a"), "id" : 20, "name" : "mongodb20" }
Type "it" for more
这时候可以查找到数据。
./mongo 10.4.4.5:27017
查看rs
rs.status()
{
"set" : "mongodb_set",
"date" : ISODate("2014-04-04T06:56:30Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.4.4.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 22,
"optime" : Timestamp(1396594084, 4268),
"optimeDate" : ISODate("2014-04-04T06:48:04Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.4.4.6:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 20,
"optime" : Timestamp(1396594084, 4268),
"optimeDate" : ISODate("2014-04-04T06:48:04Z"),
"lastHeartbeat" : ISODate("2014-04-04T06:56:30Z"),
"lastHeartbeatRecv" : ISODate("2014-04-04T06:56:30Z"),
"pingMs" : 0,
"lastHeartbeatMessage" : "syncing to: 10.4.4.5:27017",
"syncingTo" : "10.4.4.5:27017"
}
],
"ok" : 1
}
副本集二没有变化。
现在两个副本集的数据相通。但是三个节点共有同一个副本集id,结果实际上分属于两个不同副本集。
现在从副本集一上的10.4.4.4:27017节点退出,改为10.4.4.5:27017登录,结果又如何?
./mongo 10.4.4.5:27017
查看rs
>rs.status()
{
"set" : "mongodb_set",
"date" : ISODate("2014-04-04T07:08:08Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "10.4.4.5:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 720,
"optime" : Timestamp(1396594084, 4268),
"optimeDate" : ISODate("2014-04-04T06:48:04Z"),
"self" : true
},
{
"_id" : 1,
"name" : "10.4.4.6:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 718,
"optime" : Timestamp(1396594084, 4268),
"optimeDate" : ISODate("2014-04-04T06:48:04Z"),
"lastHeartbeat" : ISODate("2014-04-04T07:08:06Z"),
"lastHeartbeatRecv" : ISODate("2014-04-04T07:08:06Z"),
"pingMs" : 0,
"syncingTo" : "10.4.4.5:27017"
}
],
"ok" : 1
}
可见当前连接的是副本集二的primary,此时副本集一的primary去哪里了呢?似乎只能通过10.4.4.4:27017来访问secondary了。
这个难道是mongodb的一个bug?