MongoDB secondary replica does not have collections as in primary - mongodb

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

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/

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).

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

Related

Error when try to connect to Replica Set in Mongo

When I try to connect to mongo replica set in AWS I get this error:
slavenode:27017: [Errno -2] Name or service not
known,ip-XXX-XX-XX-XX:27017: [Errno -2] Name or service not known
(where XXX-XX.. corresponds to my actual ip address)
The code to connect is shown below:
client = MongoClient("mongodb://Master-PublicIP:27017,Slave-PublicIP:27017/myFirstDB?replicaSet=rs0")
db = client.myFirstDB
try:
db.command("serverStatus")
except Exception as e:
print(e)
else:
print("You are connected!")
client.close()
(where in Master-PublicIP and Slave-PublicIP I have the actual IPv4 Public IP's from AWS console)
I have already a replica set and the configuration is:
rs0:PRIMARY> rs.conf()
{
"_id" : "rs0",
"version" : 2,
"members" : [
{
"_id" : 0,
"host" : "ip-XXX-XX-XX-XXX:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
},
{
"_id" : 1,
"host" : "SlaveNode:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : 0,
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatTimeoutSecs" : 10,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
}
}
}
I have create the /data/db in PRIMARY and the /data/db1 in SECONDARY and I have give the proper ownership with sudo chmod -R 755 /data/db
My MongoDB version is 3.0.15. Is anyone know what is going wrong?
Thanks in advance.
Have you tried removing the myFirstDB from within the MongoClient()
MongoClient("mongodb://Master-PublicIP:27017,Slave-PublicIP:27017/?replicaSet=rs0")
Because your next line then specifies which db you want to use
db = client.myFirstDB
Or I think you can specify the db by putting a dot after the closing brace on the MongoClient()
MongoClient("mongodb://Master-PublicIP:27017,Slave-PublicIP:27017/?replicaSet=rs0").myFirstDB
I manage to solve the problem. As #N3i1 suggests in commnets, I use the Public DNS (IPv4). There was an issue with the hosts that I had declare in /etc/hosts.
In this file I had define the ips of master/ slaves with some names. For some reason this didn't work. I delete them and then I reconfigure the replica set configuration.
In PRIMARY in mongo shell I did:
cfg = {"_id" : "rs0", "members" : [{"_id" : 0,"host" : "Public DNS (IPv4):27017"},{"_id" : 1,"host" : "Public DNS (IPv4):27017"}]}
rs.reconfig(cfg,{force: true});
Then I connect in the replica set with python with:
MongoClient("mongodb://Public DNS (IPv4):27017,Public DNS (IPv4):27017/?replicaSet=rs0")
Of course change the Public DNS (IPv4) adresses with yours.

Mongo sync: slow down or stop sync?

All,
I have a replicaset setup where I run two mongo processes, M_pri on port 28001 and M_sec on 28002 in the same machine with the following config:
"_id" : "myReplSet",
"version" : 1,
"protocolVersion" : NumberLong(1),
"members" : [
{
"_id" : 0,
"host" : "localhost:28001",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "localhost:28002",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : true,
"priority" : 0,
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : 2000,
"getLastErrorModes" : {
},
"replicaSetId" : ObjectId("593565b0ebd8ca36a07c6576")
}
The intention for this setup is to take a daily mongodump (gunzip) off M_sec. During the mongodump the whole of my system slow down as I have these processes that make writes and reads off the M_pri.
Is there a way by which I could stop the sync from Primary to Secondary mongo during the time when I am doing a mongodump off M_sec?
Thanks
You can set the M_sec to be a delayed member (https://docs.mongodb.com/manual/core/replica-set-delayed-member/#replica-set-delayed-members), so it will replicate from M_pri with a delay.
Things to keep in mind:
Requirements
Delayed members:
Must be priority 0 members. Set the priority to 0 to prevent a delayed member from becoming primary.
Should be hidden members. Always prevent applications from seeing and
querying delayed members.
do vote in elections for primary, if members[n].votes is set to 1.
Behavior
Delayed members copy and apply operations from the source oplog on a
delay. When choosing the amount of delay, consider that the amount of
delay:
must be equal to or greater than your expected maintenance window durations.
must be smaller than the capacity of the oplog. For more information on oplog size, see Oplog Size.
Configuration example:
{
"_id" : <num>,
"host" : <hostname:port>,
"priority" : 0,
"slaveDelay" : <seconds>,
"hidden" : true
}

MongoDb Replication Issue- when add one more node then primary stops responding

I am using mongodb replication
here is the output of rs.conf()
firstset:PRIMARY> rs.conf();
{
"_id" : "firstset",
"version" : 43,
"members" : [
{
"_id" : 7,
"host" : "primaryip:10002"
},
{
"_id" : 10,
"host" : "arbiterip:10009",
"votes" : 2,
"arbiterOnly" : true
},
{
"_id" : 12,
"host" : "secondaryip:10006"
}
]
}
Now I want to add another secondary instance. So i just started another mongod process on port 10004 and fired the command
rs.add("secondaryip:10004");
I got the output
{ "ok" : 1 }
and the state of newly attached instance was
"stateStr" : "STARTUP2",
but at the same time my application was not able to connect to primary instance. why ?
Please help me to solve this issue.
This was a bug of MongoDB. Bug resolved by MongoDB team from version 2.6.2

why is my mongo local db oplog gigantic

Appreciate any insights on this, I have 2 questions:
1) Figure out why my local db oplog is massive and growing
2) Safely delete (or reset) my local.oplog to free up the 18 gbs of wasted space
The scenario: I have been running mongod locally on a snapshot of production data like this:
mongod --dbpath /temp/MongoDumps/mongodata-2013-06-05_1205-snap/data
So I noticed the weird thing is my local db is huge
> show dbs
local 18.0693359375GB
prod-snapshot 7.9501953125GB
Which appears to be due to the gigantic local db oplog (even though its a capped collection)
db.oplog.rs.stats()
{
"ns" : "local.oplog.rs",
"count" : 25319382,
"size" : 10440151664,
"avgObjSize" : 412.33832895289464,
"storageSize" : 18634489728,
"numExtents" : 9,
"nindexes" : 0,
"lastExtentSize" : 1463074816,
"paddingFactor" : 1,
"systemFlags" : 0,
"userFlags" : 0,
"totalIndexSize" : 0,
"indexSizes" : {
},
"capped" : true,
"max" : NumberLong("9223372036854775807"),
"ok" : 1
}
And despite not having setup any replica sets on my local, my local db seems to have inherited my production replica set configurations (maybe it's inheriting through the snapshot???)
rs.config()
{
"_id" : "mongocluster1",
"version" : 38042,
"members" : [
{
"_id" : 4,
"host" : "mongolive-01D.mcluster-01:27017",
"tags" : {
"app" : "backend"
}
},
{
"_id" : 5,
"host" : "mongolive-01C.mcluster-01:27017"
},
{
"_id" : 11,
"host" : "mongoarbiter-01C.mcluster-01:27017",
"arbiterOnly" : true
},
{
"_id" : 7,
"host" : "mongoremote-01Z.mcluster-01:27017",
"priority" : 0,
"hidden" : true
},
{
"_id" : 21,
"host" : "mongodelayed-01D.mcluster-01:27017",
"priority" : 0,
"slaveDelay" : 3600,
"hidden" : true
}
]
}
Not sure if related but also seeing this:
> rs.status()
{ "ok" : 0, "errmsg" : "not running with --replSet" }
And when I start the server I get a replicaSet warning:
MongoDB shell version: 2.4.1
connecting to: test
Server has startup warnings:
** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
** Restart with --replSet unless you are doing maintenance and no other clients are connected.
** The TTL collection monitor will not start because of this.
You captured a snapshot of the data directory of a production node and therefore you got its EXACT database configuration.
This include its "local" database. The local database includes (among other things) the replica set configuration and the oplog.
Since you intend to run your mongod in stand-alone mode you can simply drop the local database with no ill effect. Use the dropDatabase() command. This will drop the database and the space will be reclaimed by the OS.

mongodb replicaset host name change error

I have a mongodb replicaset on ubuntu.. In replica set, hosts are defined as localhost. You can see ;
{
"_id" : "myrep",
"version" : 4,
"members" : [
{
"_id" : 0,
"host" : "localhost:27017"
},
{
"_id" : 2,
"host" : "localhost:27018"
},
{
"_id" : 1,
"host" : "localhost:27019",
"priority" : 0
}
]
}
I want to change host adresses with real ip of server. But when i run rs.reconfig, I get error :
{
"assertion" : "hosts cannot switch between localhost and hostname",
"assertionCode" : 13645,
"errmsg" : "db assertion failure",
"ok" : 0
}
How can i solve it ?
Thank you.
There is a cleaner way to do this:
use local
cfg = db.system.replset.findOne({_id:"replicaSetName"})
cfg.members[0].host="newHost:27017"
db.system.replset.update({_id:"replicaSetName"},cfg)
then restart mongo
The only way I found to change host names is recreating replica set.. To make it right db directories need to be cleaned.. Then starting all servers with replication mode after that creating new repset with new host names fixed it.