MongoDB Replica Sets Not Syncing? - mongodb

I've been following the steps on the tutorial for deploying a replica set to a development/test environment: http://docs.mongodb.org/manual/tutorial/deploy-replica-set/#deploy-a-development-or-test-replica-set
I have three instances of mongod.exe running, and all three are able to connect properly.
I wanted to verify that the Secondaries are properly syncing up to the Primary when an Insert happens, but when I do an Insert, there doesn't seem to be any indication that the Secondaries are following through.
For instance, I have a MongoShell connected to the Primary and I run the commands:
db.createCollection("testCol")
db.testCol.insert( { item: "card", qty: 15 } )
This creates the 'testCol' collection and inserts a value into it.
When I do:
show collections
The 'testCol' collection shows up fine, and the item I inserted is present.
My question is: Shouldn't the Secondaries sync up with the Primary and copy the Collection and Item over?
Starting up another MongoShell and connecting to one of the Secondaries, I see that the collection I created on the Primary is not showing up.
Am I just misunderstanding how Replica Sets work, or is there something else that I need to set in order for the Secondaries to copy Insert actions?
I followed the instructions in the link I gave in the beginning, so any configurations that I have set up are exactly the same.

Well that's embarrassing.
Turns out that I was looking at the incorrect database on my side. The way that I'd configured, I needed to be using a database named test but I had been doing inserts on local.
Looking back at the setup instructions for Replica Sets, I'm not quite sure where I configured MongoShell to connect to test, but connecting to that database and making a change on the Primary seemed to fix everything.
Here's how my MongoShell connected to the Primary looked like:
MongoDB shell version: 2.4.5
connecting to: 127.0.0.1.27017/test
Maybe the connection to test was configured when I first set up MongoDB? Not too sure right now.
Doing a little more looking around, it seems like when you connect with MongoShell, it's automatically connecting to the test database. Not sure how to change parameters in the shortcut I'm using in order to have it point to a different database.
http://docs.mongodb.org/manual/reference/mongo-shell/#basic-shell-javascript-operations
In the mongo shell, db is the variable that references the current
database. The variable is automatically set to the default database
test or is set when you use the use to switch current database.
I guess this means there's no parameter you can pass in, and you have to do a use <db> manually?
Found it!
If you set up a shortcut to mongo.exe, set this as your Target:
C:\mongodb\bin\mongo.exe <db name> --host <host> --port <port>
Previously I only had the --port parameter, but adding in the rest allowed me to start directly in a specific database.

Related

How to Make Mongodb replicaset as master

Recently because of an unknown issue our MongoDB hosted on a GCP compute VM has stopped, we were unable to restart it because it's throwing MongoDB.service is not found. so we reinstalled the MongoDB after taking a backup of all .wt files in the DB path. once we've reinstalled the MongoDB we copied the files back and we can't see the data in the DB. we tried the --repair flag but still no use.
is there a way we can get this working?
The other thing is we've taken the VM snapshot from the day before the crash. there we can see the data in MongoDB only if we run the method rs.slaveOk(). I think we can't use that DB as the primary db. is there a way that we can use this as a primary db.
I'm relatively new to the concept of replica set, Master/slave any suggestions and questions are welcome
Thanks
If you see the data from the snapshot with rs.slaveOk() it is easy to recover , you can reconfigure the member as standalone PRIMARY with this steps:
Get the current config:
cfg = rs.conf()
printjson(cfg)
Set in the temp variable cfg only the available member(in the example it is the first in the cfg with id:0 ):
cfg.members = [cfg.members[0]]
Reconfigure the replicaSet only with the available member:
rs.reconfig(cfg, {force : true})
(Don't forget to add the {force:true} option since it is the only way to reconfigure from SECONDARY)
If all is good with this member and it successfully elect as PRIMARY , you can add other new members with rs.add() ...

Connect directly to the "local" database of primary MongoDB node in replica set

I need access to the OpLog of the primary of my replica set in MongoDB. I am developing an OpLog driven ETL job as it is far more efficient than simply querying and transferring data from the entire table.
I can access the OpLog easily with the following steps in the terminal:
mongo mymongoserver.com:10733/my-db -u oplogUser -pxxxxxxxx
Then I run:
use local
and then I can query the OpLog using:
db.oplog.rs.find()
My question is: Are there any settings I can pass to the mongo connect command to get me straight into the local database for the primary node?
I am using Talend Open Studio for my ETL needs. Am I approaching this the wrong way?
I have come across this from the guys at Stripe, which means that this is definitely possible! https://www.mongodb.com/presentations/building-real-time-systems-mongodb-using-oplog-stripe

How to check the state of rs.initiate

I have an existing system using replica set of 3 members already running, I am trying to add another member, but I saw in the Doc that I need to run this command rs.initiate() fist before I can add another member rs.add(“hostname03:27017”)
Do I need to run rs.initiate() ? I am assuming that I don't need to since I already have a replica set up & running. How do I check the status of rs.initiate if it has already been initiated.. Thanks.
You don't need to. The rs.initiate() command is not much more than telling the instance you are connected to that it should set up everything for a replica set. In your case, this has already be done. So simply connect to the primary of your replica set and add the member.
Disclaimer: The following comes of the top of my head and I don't have the means of checking its correctness on my tablet.
Iirc, rs.initiate() is a shell helper. So you might be able to see what it is actually doing by issuing
rs.initiate
(Note the missing parenthesises) in the mongo shell.

MongoDB how to become master

I am creating a MongoDB database through a linux terminal and I am trying to create a collection for the database.
But when I run the command: db.createCollection("mainCollection") I get the following error message: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
I'm not exactly sure what this means. How can I make the database master?
Thanks
You have started the mongod with the --replSet option (or equivalent configuratin file option). That puts the mongod into a mode where it will not allow any writes until it receives a replica set configuration.
For an existing replica set this is accomplished by doing a rs.add("<host>:<port>") on the existing primary for the replica set.
Based on the conversation around this question I think you have a single MongoDB instance and do not plan to have a true multi-member replica set. If that is the case you have two options:
Stop trying to run a replica set
Stop the mongod.
Wipe the data directory for the mongod process.
Restart the mongod process without the --replSet option on the command line/config.
Initialize the mongod as a single node replica set.
Run rs.initiate() from the shell (no config is required). You will get disconnected but the shell will automatically reconnect and then you can create collections and do other writes.
Thats expected behaviour "Viratan" if you are querying on the replica set and the shell you are connected with is not the Primary.
You can do either of these two things.
Disconnect from the current shell and connect with the Primary. In mongo shell you would see it written "PRIMARY" or "SECONDARY".
In case you want the same member as the primary then you can increase the priority of that particular member and/or force the formar primary to stepDown. once the primary is step down, the election would occur and because the desired member has higher priority that would become primary.
you can follow the below link to change the priority of a member http://docs.mongodb.org/manual/tutorial/force-member-to-be-primary
Once you are connected with Primary in either ways, you can query the DB and do your stuff.
Happy Mongoing.. :-)
-$
you are trying to create a new collection on secondary.thats why it is giving as error please use primary to create new

MongoDB: Stop from storing replica set settings

I have a local mongo database that's being populated by a script which will run for the next few days.
When it'll be done, I'm expecting its size to be quite substantial, so I want mongo to stop storing replica sets (or whatever is it doing in that database called "local"), because this is just a pet project, the database is local. I won't have any clients so I don't need replication and the database will only be used for reads after the initial data is inserted. It has the same size as my database so it's a waste of space.
I found this other thread where they just basically say to drop the local database and I did that but, every time I'm starting mongod, it's there again (and I'm not starting mongod with --replSet. Mongod doesn't even recognize that flag, btw). Could you please tell me how do I stop mongod from generating it?
Running mongod as a standalone (no --replSet option) means you won't store oplog entries or replica set related information.
The local database will be created. The size of the local database should remain static in this case. The contents should be 2 collections:
startup_log which holds static information about your mongod instance, including version, start time and startup options.
system.indexes which contains index definitions for the collections in this database. In this case it would have a single entry defining the _id index for startup_log.
To confirm yourself that these are the only 2 collections run the following from the mongo shell:
use local;
show collections;