MongoDB的数据复制分片(2)

[root@node2 ~]# mongo   
MongoDB shell version: 2.6.4   
connecting to: test   
> rs.status()   
{   
"startupStatus" : 3,   
"info" : "run rs.initiate(...) if not yet done for the set",   
"ok" : 0,   
"errmsg" : "can't get local.system.replset config from self or any seed (EMPTYCONFIG)" 
}

使用rs.status()查看状态,有3个节点,但是都没有初始化配置。需要运行rs.initiate()

1.3运行rs.initiate()

> rs.initiate()
{
"info2" : "no configuration explicitly specified -- making one",
"me" : "node2.linuxidc.com:27017",
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
testset:OTHER> rs.status()
{
"set" : "testset",
"date" : ISODate("2014-10-12T07:51:58Z"),
"myState" : 1,
"members" : [
  {
  "_id" : 0,
  "name" : "node2.linuxidc.com:27017",
  "health" : 1,
  "state" : 1,
  "stateStr" : "PRIMARY",
  "uptime" : 920,
  "optime" : Timestamp(1413100302, 1),
  "optimeDate" : ISODate("2014-10-12T07:51:42Z"),
  "electionTime" : Timestamp(1413100302, 2),
  "electionDate" : ISODate("2014-10-12T07:51:42Z"),
  "self" : true
  }
],
"ok" : 1
}
testset:PRIMARY>

可以看到添加了一个节点node2,也就是自己,且是primary节点。以及所有的状态

1.4添加其他两个节点

testset:PRIMARY> rs.add("node1.linuxidc.com")
{ "ok" : 1 }
testset:PRIMARY> rs.add("node3.linuxidc.com")
{ "ok" : 1 }
用rs.status()查看
testset:PRIMARY> rs.status()
{
"set" : "testset",
"date" : ISODate("2014-10-12T08:03:48Z"),
"myState" : 1,
"members" : [
  {
  "_id" : 0,
  "name" : "node2.linuxidc.com:27017",
  "health" : 1,
  "state" : 1,
  "stateStr" : "PRIMARY",
  "uptime" : 1630,
  "optime" : Timestamp(1413101019, 1),
  "optimeDate" : ISODate("2014-10-12T08:03:39Z"),
  "electionTime" : Timestamp(1413100302, 2),
  "electionDate" : ISODate("2014-10-12T07:51:42Z"),
  "self" : true
  },
  {
  "_id" : 1,
  "name" : "node1.linuxidc.com:27017",
  "health" : 1,
  "state" : 5,
  "stateStr" : "STARTUP2",
  "uptime" : 17,
  "optime" : Timestamp(0, 0),
  "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
  "lastHeartbeat" : ISODate("2014-10-12T08:03:46Z"),
  "lastHeartbeatRecv" : ISODate("2014-10-12T08:03:47Z"),
  "pingMs" : 224
  },
  {
  "_id" : 2,
  "name" : "node3.linuxidc.com:27017",
  "health" : 1,
  "state" : 6,
  "stateStr" : "UNKNOWN",
  "uptime" : 8,
  "optime" : Timestamp(0, 0),
  "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
  "lastHeartbeat" : ISODate("2014-10-12T08:03:47Z"),
  "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
  "pingMs" : 905,
  "lastHeartbeatMessage" : "still initializing"
  }
],
"ok" : 1
}

有没有发现新加的这两个节点状态不对?没有关系,可能是还没有同步过来,稍等在刷新试试

"name" : "node1.linuxidc.com:27017",

"stateStr" : "SECONDARY",

"name" : "node3.linuxidc.com:27017",

"stateStr" : "SECONDARY",

再刷新时添加的两个节点都成为了secondary。

1.5我们在主节点来创建一些数据

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

转载注明出处:https://www.heiqu.com/65f9bad25a5a9b6521a141109fddbd95.html