MongoDB 辅助副本没有主副本中的集合

MongoDB secondary replica does not have collections as in primary

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

然后我切换到辅助,运行 rs.slaveOk() 希望看到在主创建的 "adaptive-db" 也出现在辅助中,但我没有看到它。

这是我的命令 运行:

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> 

谁能解释一下为什么?这是我的 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
        }
    }
}

谢谢

我猜了一点,但根据您看到的其他集合判断,您在 local 数据库中。顾名思义,它不会被复制(它包含 oplog、startup_log 和其他类似的东西,它们是特定于实例的,如果被复制就会搞砸)。

使用不同的数据库。要么连接到 127.0.0.1/somedb(使用适当的 IP/hostname),要么执行 use somedb,以在控制台中切换数据库。然后创建集合,这些集合应该被复制(当然是复制到同名的数据库 - 在我的例子中 somedb)。

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

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

对于较新版本的 MongoDB 运行 命令 rs.secondaryOk() 在辅助数据库上。