MongoDB 次要副本没有主副本中的集合

aaz*_*eem 0 mongodb replicaset

我在本地机器上设置了一个 mongodb 副本集,并在名为“adaptive-db”的数据库中创建了几个集合。在 mongo shell 中,当我连接到主服务器并运行 show dbs 时,我可以查看和查询我的数据库“adaptive-db”。

然后我切换到次要,并运行 rs.slaveOk() 期望看到在主要中创建的“自适应数据库”也出现在次要中,​​但我没有看到它。

这是我运行的命令:

shell:bin user1$ ./mongo localhost:27017
MongoDB shell version: 3.0.2
connecting to: localhost:27017/test
rs0:PRIMARY> show dbs
adaptive-db  0.125GB
local        0.281GB
rs0:PRIMARY> use adaptive-db
switched to db adaptive-db
rs0:PRIMARY> show collections
people
student
system.indexes
rs0:PRIMARY> db.people.find().count()
6003
rs0:PRIMARY> exit
bye


shell:bin user1$ ./mongo localhost:27018
MongoDB shell version: 3.0.2
connecting to: localhost:27018/test
rs0:SECONDARY> show dbs
2015-06-25T11:16:40.751-0400 E QUERY    Error: listDatabases failed:{ "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
    at Error (<anonymous>)
    at Mongo.getDBs (src/mongo/shell/mongo.js:47:15)
    at shellHelper.show (src/mongo/shell/utils.js:630:33)
    at shellHelper (src/mongo/shell/utils.js:524:36)
    at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47
rs0:SECONDARY> rs.slaveOk()
rs0:SECONDARY> show dbs
admin  0.031GB
local  0.281GB
rs0:SECONDARY> 
Run Code Online (Sandbox Code Playgroud)

有人可以解释我为什么吗?这是我的 rs.conf():

rs0:SECONDARY> rs.conf()
{
    "_id" : "rs0",
    "version" : 7,
    "members" : [
        {
            "_id" : 0,
            "host" : "localhost:27017",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 1,
            "host" : "localhost:27018",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        },
        {
            "_id" : 2,
            "host" : "localhost:27019",
            "arbiterOnly" : false,
            "buildIndexes" : true,
            "hidden" : false,
            "priority" : 1,
            "tags" : {

            },
            "slaveDelay" : 0,
            "votes" : 1
        }
    ],
    "settings" : {
        "chainingAllowed" : true,
        "heartbeatTimeoutSecs" : 10,
        "getLastErrorModes" : {

        },
        "getLastErrorDefaults" : {
            "w" : 1,
            "wtimeout" : 0
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢

小智 5

rs.slaveOk()在次要副本成员上运行命令;这允许当前连接允许读取操作在辅助成员上运行。

供参考:http : //docs.mongodb.org/manual/reference/method/rs.slaveOk/