0

I have set up a mongodb replica set on my local machine, and created couple of collections in a database named "adaptive-db". From the mongo shell, when i connect to the primary and run show dbs, i can see and query my database "adaptive-db".

I then switched to secondary, and ran rs.slaveOk() expecting to see the "adaptive-db" created in primary to be present in secondary as well, but i don't see it.

Here are the commands i ran:

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> 

Can someone plz explain me why? Here is my 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
        }
    }
}

Thanks

aazeem
  • 844
  • 1
  • 12
  • 23
  • please, when posting command output, use code formatting. it makes things way more readable. – Fox Jun 24 '15 at 23:04

3 Answers3

5

Run the command rs.slaveOk() on the secondary replica member; this allows the current connection to allow read operations to run on the secondary member.

For reference: http://docs.mongodb.org/manual/reference/method/rs.slaveOk/

Sculper
  • 756
  • 2
  • 12
  • 24
ejm
  • 51
  • 1
  • 2
1

I'm guessing a little, but judging by other collections you see, you are in the local database. Which, as the name suggests is not replicated (it contains oplog, startup_log and other things like that, which are specific to instance and would get messed up if replicated).

Use a different database. Either connect to 127.0.0.1/somedb (use appropriate IP/hostname), or do use somedb, to switch databases while in console. Then create collections and those should get replicated (to the database of same name, of course - somedb in my example).

Fox
  • 2,348
  • 19
  • 19
  • Thanks for correcting, i moved my collections to a new database, but it still doesn't work. I've updated my question with the latest sequence of commands i ran with the new database – aazeem Jun 25 '15 at 15:25
  • @user1437795 is your slave up to date? what does `rs.status()` say? or `rs.conf()` for that matter ... – Fox Jun 25 '15 at 15:29
  • UPDATE: I initially had the collections in "local" db, and then moved it to custom database using: db.runCommand({renameCollection:"local.student",to:"adaptive-db.student"}). But, when i tried to create a brand new database, and new collections within it, the replication worked fine. So, something to do with "renameCollection" i guess. I've udpated the question with my rs.conf() – aazeem Jun 25 '15 at 15:35
  • @user1437795 that kind of makes sense. If you create a collection which does not get replicated and then rename the database, replicating the rename command would lead to an invalid operation on the slave (as there is nothing to rename). Maybe there will even be error/warning in the logs. If not, you probably should file that as a MongoDB bug (https://jira.mongodb.org). I'd even say letting you rename from/to local database on a replicaSet is a bug ... but I'm not that deep into mongo design to know if there is not a reason. – Fox Jun 25 '15 at 15:46
0

For newer version of MongoDB run the command rs.secondaryOk() on the secondary database.

Subhojit Ghosh
  • 257
  • 3
  • 10