0

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

谢谢

4

3 回答 3

5

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

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

于 2015-08-31T20:56:47.703 回答
1

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

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

于 2015-06-24T23:03:27.447 回答
0

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

于 2021-04-07T05:38:38.860 回答