rs1:SECONDARY> rs.isMaster()
{
"setName" : "rs1",
"setVersion" : 1,
"ismaster" : false,
"secondary" : true,
"hosts" : [
"172.17.0.1:27017",
"172.17.0.2:27017",
"172.17.0.3:27017"
],
"primary" : "172.17.0.1:27017",
"me" : "172.17.0.2:27017",
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2015-07-15T02:59:27.121Z"),
"maxWireVersion" : 3,
"minWireVersion" : 0,
"ok" : 1
}
可以看到主节点的信息 和上个容器的信息一致
rs1:SECONDARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2015-07-15T03:14:10.512Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "172.17.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 981,
"optime" : Timestamp(1436929097, 1),
"optimeDate" : ISODate("2015-07-15T02:58:17Z"),
"lastHeartbeat" : ISODate("2015-07-15T03:14:10.478Z"),
"lastHeartbeatRecv" : ISODate("2015-07-15T03:14:10.478Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1436929104, 1),
"electionDate" : ISODate("2015-07-15T02:58:24Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "172.17.0.2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1452,
"optime" : Timestamp(1436929097, 1),
"optimeDate" : ISODate("2015-07-15T02:58:17Z"),
"infoMessage" : "could not find member to sync from",
"configVersion" : 1,
"self" : true
},
{
"_id" : 2,
"name" : "172.17.0.3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 981,
"optime" : Timestamp(1436929097, 1),
"optimeDate" : ISODate("2015-07-15T02:58:17Z"),
"lastHeartbeat" : ISODate("2015-07-15T03:14:09.523Z"),
"lastHeartbeatRecv" : ISODate("2015-07-15T03:14:09.523Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
linuxidc@linuxidc:~$ mongo --port 23117
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:23117/test
rs1:SECONDARY> rs.status()
{
"set" : "rs1",
"date" : ISODate("2015-07-15T03:13:25.167Z"),
"myState" : 2,
"members" : [
{
"_id" : 0,
"name" : "172.17.0.1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 936,
"optime" : Timestamp(1436929097, 1),
"optimeDate" : ISODate("2015-07-15T02:58:17Z"),
"lastHeartbeat" : ISODate("2015-07-15T03:13:24.479Z"),
"lastHeartbeatRecv" : ISODate("2015-07-15T03:13:23.531Z"),
"pingMs" : 0,
"electionTime" : Timestamp(1436929104, 1),
"electionDate" : ISODate("2015-07-15T02:58:24Z"),
"configVersion" : 1
},
{
"_id" : 1,
"name" : "172.17.0.2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 936,
"optime" : Timestamp(1436929097, 1),
"optimeDate" : ISODate("2015-07-15T02:58:17Z"),
"lastHeartbeat" : ISODate("2015-07-15T03:13:23.507Z"),
"lastHeartbeatRecv" : ISODate("2015-07-15T03:13:23.506Z"),
"pingMs" : 0,
"configVersion" : 1
},
{
"_id" : 2,
"name" : "172.17.0.3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1392,
"optime" : Timestamp(1436929097, 1),
"optimeDate" : ISODate("2015-07-15T02:58:17Z"),
"configVersion" : 1,
"self" : true
}
],
"ok" : 1
}
如果在从节点此时查看数据库会出错 ,信息显示不是主节点
rs1:SECONDARY> show dbs;
××××××××××××××××××××××××××××××××××××
设置从节点可以读
rs1:SECONDARY> db.getMongo().setSlaveOk();
rs1:SECONDARY> show dbs;
local 1.031GB
打开先前与主节点的终端 插入点数据
rs1:PRIMARY> show dbs;
local 1.031GB
rs1:PRIMARY> use tt;
switched to db tt
rs1:PRIMARY> db.user.insert({"name":"mongodb is greate!!!!"})
WriteResult({ "nInserted" : 1 })