我已经设置了 3 节点副本集。
写入主节点有效,但是当我使用 readPreference=secondary; 读取时,我得到的值为空。
当我使用 robomongo 打开本地节点时,我发现该集合不存在。所以它还没有被复制?
我不确定它什么时候被复制或者我遗漏了什么?
我使用了以下 URI
'mongodb://DNS1,DNS2,DNS3/?readPreference=secondary&w=0'
更新
以下是我的 rs.status 的样子
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2014-03-10T10:57:13Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "NWI47:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 16593,
"optime" : Timestamp(1394432703, 1),
"optimeDate" : ISODate("2014-03-10T06:25:03Z"),
"self" : true
},
{
"_id" : 1,
"name" : "t-plat-mongodb1.paand.local:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1656,
"optime" : Timestamp(1394432703, 1),
"optimeDate" : ISODate("2014-03-10T06:25:03Z"),
"lastHeartbeat" : ISODate("2014-03-10T10:57:13Z"),
"lastHeartbeatRecv" : ISODate("2014-03-10T10:57:11Z"),
"pingMs" : 187,
"syncingTo" : "NWI47:27017"
},
{
"_id" : 2,
"name" : "NWI27.np.i:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1664,
"optime" : Timestamp(1394432703, 1),
"optimeDate" : ISODate("2014-03-10T06:25:03Z"),
"lastHeartbeat" : ISODate("2014-03-10T10:57:11Z"),
"lastHeartbeatRecv" : ISODate("2014-03-10T10:57:12Z"),
"pingMs" : 102,
"syncingTo" : "NWI47:27017"
}
],
"ok" : 1
}
答案1
Mongo 立即同步,因此您的副本集有问题。
MongoDB 副本集是您在首次设置时需要正确设置的东西。如果设置不正确,则很难修复。
副本集的配置(通常)应仅从主服务器进行。如果您的副本集尚未启用,最佳选择可能是重新创建它。
另外,不确定 robomongo 是什么,但您最好使用本机 mongo 客户端来了解发生了什么。
rs.status() 命令应该会给出如下输出
rs0:SECONDARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2014-03-10T10:42:27Z"),
"myState" : 2,
"syncingTo" : "mongo-master:27017",
"members" : [
{
"_id" : 0,
"name" : "mongo-master:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 3008469,
"optime" : Timestamp(1394448146, 1),
"optimeDate" : ISODate("2014-03-10T10:42:26Z"),
"lastHeartbeat" : ISODate("2014-03-10T10:42:26Z"),
"lastHeartbeatRecv" : ISODate("2014-03-10T10:42:26Z"),
"pingMs" : 1
},
{
"_id" : 3,
"name" : "mongo-slave3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 3012206,
"optime" : Timestamp(1394448146, 1),
"optimeDate" : ISODate("2014-03-10T10:42:26Z"),
"self" : true
},
{
"_id" : 4,
"name" : "mongo-slave4:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 890533,
"optime" : Timestamp(1394448146, 1),
"optimeDate" : ISODate("2014-03-10T10:42:26Z"),
"lastHeartbeat" : ISODate("2014-03-10T10:42:26Z"),
"lastHeartbeatRecv" : ISODate("2014-03-10T10:42:26Z"),
"pingMs" : 0,
"syncingTo" : "mongo-master:27017"
}
],
"ok" : 1
}