How to check the state of rs.initiate - mongodb

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.

Related

Mongodb replication automatically

Is there any ways or methods to start mongodb replication directly when mongod service start? I don't want to enter to shell and ON the replication?
Thanks!
You can create a mongod service which starts automatically when server starts.
First you need to create a configuration file(mongodb.conf) which will include configuration settings such as replicaSet name etc. Then create a service and install it using following command
mongod -f c:\mongod.conf --install
Then start the service using
net start mongodb
Read about configuration file here and
How to install mongo as service here
When you create a valid replica set in mongodb, your data will be asynchronously from the primary member to the secondary members in replica set
Having said that, you're not required to do extra efforts manually to get data replication done
When you do rs.slaveOk() on secondary, that allows you to query data from secondary members in the replica set.
It's a provision. It allows you to read from secondary provided that you're can tolerate the possible eventual consistent data. The replication does not happen when you do rs.slaveOk() on secondary
I'm not sure to understand. Your question was about service start. On my part, I install mongo on ubuntu and the service is not started with replicatet mode.
Finally, I disabled the first one and I created another service with the option --replSet myReplicat .
When you have only 2 servers, there is a problem with majority votes. On my part, I had 2 secondary after I stopped the primary and it was difficult to comeback with 1 primary and 1 secondary.
Effectively, the replication is always active. By default, all connections should go to the Primary. If you want to readonly from a secondary, you first enter the commande rs.slaveOk(). This command is active at session level. If you reconnect, you have to pass it again. It is not possible to put it at server side.

Is there a way to revert or undo replica set configuration

I have two standalone nodes, which have mongodb running on them. Both of them have a replica set configuration rs0 and start with rs.initiate().
But in some scenarios, I want them to run as individual nodes, but in some cases, I want one to become primary and one to become secondary. But since I have done rs.initiate() on both, I won't be able to add any node as secondary.
So is there a way to undo the replica set configuration so that I can enable adding the secondary node.
Follow these steps
Switch to local database
Execute the command to make the cllection such as system.replset empty
db.system.replset.remove({});
Make sure that system.replset in local database is empty by executing the following command:
db.system.replset.find();
Refernce from : https://vitalflux.com/mongodb-how-to-reset-mongo-replica-set/
you can simply prepare a new config document and reconfigure the replication.
rs.reconfig(new_config_file)

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 Replica Sets Not Syncing?

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.

MongoDB add replica sets on running isntances

Does anybody knows of a way to add a replica set to a mongo instance which hasn't been started with -replSet and without restarting it ? In other words is it possible to create a replica set and add already running instances of mongodb ?
You need to start mongod with a --replSet parameter (or replSet config file option) in order to use replication. There are replication background tasks and other server internals that are not enabled in standalone mode.
There is no method (as at MongoDB 2.2.0) to change the role of a running mongod instance from standalone to replica set mode (or vice-versa).
In other words is it possible to create a replica set and add already running instances of mongodb ?
As noted, you would need to restart those instances with the replSet parameter.
You can, however, add additional members to a running replica set without downtime.
For more information see the MongoDB manual: Add Members to a Replica Set.