Mongodb replica set (Write on secondary) - mongodb

I have created 3 AWS instances for mongodb. One for primary, one for secondary and one arbiter. My application is pointing to the Primary node. So when the Primary goes down and becomes Secondary no data is posted on it. How can I enable write operations for a secondary node. Mongodb's write concern (w:"majority") didnt work for me.
Can anyone please give a work around ?

You cannot write on a secondary node.
If you primary goes down, it will become a secondary which will receive the writes through the replication mechanism from the new elected primary (only one machine of yours can be as the other member is an arbiter which doesn't hold data).
Write concern is not about writing to secondaries directly. It is about how many replica set member (primary, secondaries...) have to acknowledge the write for you application to receive the ok from the driver.

Seems like it will work if you don`t connect on the primary direction, but with the name of the replica set as a prefix. If the primary has been restarted and is a secondary now, it will redirect you to the new primary.
If your primary is mongo1 and your replica set is named rs you don`t use
mongo --host mongo1:27017
but
mongo --host rs/mongo1:27017

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.

Unable to elect primary after secondary taken offline Mongo

I have a replica setup with 1 Arbiter and 3 Mongo Databases.
2 of the databases (db1 and db2) I have given an equal priority of becoming primary, and the third one (db3) I have a priority of 0.
I am trying to take db3 offline to copy the data to another server, but every time I run db.shutdownServer() in db3, it causes db1 and db2 to become secondaries, and they remain stuck in this configuration.
It's my understanding that reelection only takes place when Primaries become unavailable.
Am I missing something??
So what was actually happening was I had added 3 other databases (shutdown) in hidden mode which were going to become my next replica set. Apparently Mongo has a setting where if the number of shutdown dbs > running dbs, the replica set goes into read-only mode, so obviously every time I would shutdown db3 this would trigger this to take place.

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

Does the mongoimport command for a replica set require all replica set members including the arbiter?

I am running MongoDB 2.6.2 and I have a replica set with a primary, secondary and an arbiter. Are there any adverse effects to running a mongoimport command with only one or two of the replica set members?
More specifically, does the arbiter need to be specified in the --host option of the mongoimport command? The documentation clearly describes the hostname format (here), but does not warn against how many of the members need to be specified.
mongoimport command should be run on primary host as this is an insert operation and all insert can be done on primary node only. Secondary nodes cannot entertain write operation directly. In a replicaset, primary node gets the data first and then secondary nodes reads the primary node's oplog and replicate the operations.
Simply put, if you are using a replicaset and you want import data using mongoimport then you must pass primary node as host to mongoimport command.
Also, mongoimport command does't care about arbiter. All it cares is, if it can write data to the node or not. In case of replicaset, you can insert data only on primary node.

MongoDB replica set no primary, need to force new primary

We have a mongoDB replica set which has 3 nodes;
Primary
Secondary
Arbiter
Somehow our replica set has ended up with 1 & 2 both being set as secondary members. I'm not sure how this has happened (we have had a server migration which one of the nodes runs on, but only one).
Anyway, I've been trying to re-elect a new primary for the replica set following the guide here
I'm unable to just use
rs.reconfig(cfg)
as it will only work if directed at the primary (which I don't have).
Using the force parameter
rs.reconfig(cfg, { force: true})
would appear to work but then when I requery the status of the replica set, both servers are still only showing as Secondary.
Why hasn't the force reconfig worked? Currently the database is locked out whatever I try.
1.Convert all nodes to standalone.
Stop mongod deamon and edit /etc/mongod.conf to comment replSet option.
Start mongod deamon.
2.Use mongodump to backup data for all nodes.
Reference from mongo docs:
https://docs.mongodb.com/manual/reference/program/mongodump/
3.Log into each node, and drop local database.
Doing this will delete replica set config on the node.
Or you can just delete a record in collection system.replset in local db, like it said here:
https://stackoverflow.com/a/31745150/4242454
4.Start all nodes with replSet option.
5.On the previous data node (not arbiter), initialize a new replica set.
6.Finally, reconfig replica set with rs.reconfig.