37

为了设置副本集,我在 3 个单独的终端选项卡中运行:

 $ sudo mongod --replSet rs0 --dbpath /data/mining --port 27017
 $ sudo mongod --replSet rs0 --dbpath /data/mining2 --port 27018
 $ sudo mongod --replSet rs0 --dbpath /data/mining3 --port 27019

然后,我在 Mongo shell 中配置了复制并验证它是否有效:

 > var rsconf = {
     _id: "rs0",
     members: [
       {
         _id: 0,
         host: 'localhost:27017'
       },
       {
         _id: 1,
         host: 'localhost:27018'
       },
       {
         _id: 2,
         host: 'localhost:27019'
       }
     ]
   };
 > rs.initiate(rsconf);
{
  "info": "Config now saved locally.  Should come online in about a minute.",
  "ok": 1
}
// Some time later...
 > rs.status()
{
  "set": "rs0",
  "date": ISODate("2013-06-17T13:23:45-0400"),
  "myState": 2,
  "syncingTo": "localhost:27017",
  "members": [
    {
      "_id": 0,
      "name": "localhost:27017",
      "health": 1,
      "state": 1,
      "stateStr": "PRIMARY",
      "uptime": 4582,
      "optime": {
        "t": 1371489546,
        "i": 1
      },
      "optimeDate": ISODate("2013-06-17T13:19:06-0400"),
      "lastHeartbeat": ISODate("2013-06-17T13:23:44-0400"),
      "lastHeartbeatRecv": ISODate("2013-06-17T13:23:44-0400"),
      "pingMs": 0
    },
    {
      "_id": 1,
      "name": "localhost:27018",
      "health": 1,
      "state": 2,
      "stateStr": "SECONDARY",
      "uptime": 5034,
      "optime": {
        "t": 1371489546,
        "i": 1
      },
      "optimeDate": ISODate("2013-06-17T13:19:06-0400"),
      "self": true
    },
    {
      "_id": 2,
      "name": "localhost:27019",
      "health": 1,
      "state": 2,
      "stateStr": "SECONDARY",
      "uptime": 4582,
      "optime": {
        "t": 1371489546,
        "i": 1
      },
      "optimeDate": ISODate("2013-06-17T13:19:06-0400"),
      "lastHeartbeat": ISODate("2013-06-17T13:23:44-0400"),
      "lastHeartbeatRecv": ISODate("2013-06-17T13:23:45-0400"),
      "pingMs": 0,
      "syncingTo": "localhost:27017"
    }
  ],
  "ok": 1
}

我的脚本对主脚本运行良好:

 $ ./runScripts.sh -h localhost -p 27017
MongoDB shell version: 2.4.3
connecting to: localhost:27017/test
Successful completion

但是,针对任一次要:

 $ ./runScripts.sh -h localhost -p 27018
MongoDB shell version: 2.4.3
connecting to: localhost:27017/test
Mon Jun 17 13:30:22.989 JavaScript execution failed: count failed: 
{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" } 
at src/mongo/shell/query.js:L180
failed to load: /.../.../myAggregateScript.js

我已经在多个地方阅读过使用rs.slaveOk()or db.getMongo().setSlaveOk(),但无论是从 shell 输入还是在我的脚本中调用,这些都没有任何效果。这些语句在调用时没有抛出错误,但它们也没有解决问题。

有谁知道为什么我不能将我的 replset 配置为允许查询辅助节点?

4

3 回答 3

83

rs.slaveOk()在 mongo shell 中运行将允许您从辅助文件中读取。这是在 MongoDB 2.4.3 下使用 mongo shell 的演示:

$ mongo --port 27017
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1:27017/test
replset:PRIMARY> db.foo.save({})
replset:PRIMARY> db.foo.find()
{ "_id" : ObjectId("51bf5dbd473d5e80fc095b17") }
replset:PRIMARY> exit

$ mongo --port 27018
MongoDB shell version: 2.4.3
connecting to: 127.0.0.1:27018/test
replset:SECONDARY> db.foo.find()
error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
replset:SECONDARY> rs.slaveOk()
replset:SECONDARY> db.foo.find()
{ "_id" : ObjectId("51bf5dbd473d5e80fc095b17") }
replset:SECONDARY> db.foo.count()
1
于 2013-06-17T19:13:40.013 回答
21

您必须rs.slaveOk()在辅助服务器的 shell 中运行该命令。

于 2013-09-13T03:18:51.690 回答
0

使用以下命令在 MongoDB 辅助节点上运行查询:

db.getMongo().setReadPref('secondary') 
于 2021-07-15T13:28:06.060 回答