Mongo does not read from primary when secondary is down - mongodb

my mongo url config is url = 'mongodb://1.1.1.1:27017,2.2.2.2:27017/test';
where 1.1.1.1 is primary and 2.2.2.2 is secondary.
Initially it reads from the secondary but when I shut down secondary mongo does not read from primary and fails
I want to know how can I read from primary when secondary is down
I am using mongoose.

In order for a primary to exist, the majority of voting nodes in the deployment must be available and communicating. If your deployment has a total of 2 nodes, and any node is unavailable, there is no primary.
mongo does not read from primary
There is no primary in this case, therefore, it is impossible to "read from primary".

Related

Mongo Replica Endpoint

I have some questions about the mongo replica
mongo replica
If I make 1 primary and 2 secondary MongoDB for replication. So I have 3 endpoints to 3 different DB and my apps can only write on primary DB. what if suddenly my primary shutdown and secondary DB take over the primary. Then how to automatically change the endpoint in my apps? should I use mongos (mongo routes)? but it needs sharding if I remember correctly.
Thank you.
All nodes in a replica set work together to have identical data. Secondary nodes may lag behind the primary, but you don't get "3 different DB". There is only one database of which copies exist on each node.
All MongoDB drivers know to monitor replica set members and discover which is the primary one automatically. You need to configure some drivers to do so by providing the replica set name, others do it automatically by default when they connect to a replica set node. Look up "connecting to replica set" in your driver documentation.
In a proper connection string you will provide all three RS members, e.g.
mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/?replicaSet=myRepl
The client will detect the PRIMARY and will use it. I guess, most drivers will re-connect automatically if the PRIMARY node changes.
Most drivers will detect the PRIMARY automatically if you provide the ReplicaSet name, i.e.
mongodb://mongodb0.example.com:27017/?replicaSet=myRepl
would connect to the PRIMARY even if it is not mongodb0.example.com. However, if mongodb0.example.com does not run, then you don't connect at all. So, it is beneficial to provide all ReplicaSet members in the connection string.
See Connection String URI Format
mongos is needed only to connect to a Sharded Cluster.

Mongodb data sync between primary and secondary node even when port is closed

I have configured mongodb replicaset in aws using 3 ec2's with one primary and 2 secondary nodes. When I insert data on primary node it is getting synced to the secondary nodes which it should. But when I close the port via security group still the data is syncing to the secondary node whose port is closed even though the state in rs.status() in the primary node is stateStr" : "(not reachable/healthy)" -> for the secondary node(whose port is closed).
I tried this with my secondary node in different subnet public as well as private but its still getting synced.I am new to mongodb and might be doing something wrong. Could someone please suggest something.

MongoDB - Switch servers on replica set

I've set up 2 machines running MongoDB ( O.S. Windows ) and i've set the replication. I was wondering if it's possible to kill the process that is running as primary to check if the secondary will turn as primary.
Is it possible? How? I just killed the primary process and on my mongo client ( that is connected to primary ), it didn't turned to secondary server.
Thank in advance.
You will need to set up a third server as an arbitrer.
The arbitrer is the one that determinates which of the other servers will be primary.
Usually, it is recomended that each replica set have an odd number of servers(the miniumun would be 1 primary, 1 secondary, 1 arbitrer)
This might help you.
http://docs.mongodb.org/manual/tutorial/add-replica-set-arbiter/
The arbitrer can be in a very tiny server, since it does not stores data, its only function is to decide.
You should have at least a 3 members configuration.
Replica documentation
Set a correct replica, connect your mongoshell to the primary instance and shut it down (Shutdown), connect your mongoshell with a secondary and check replica set's status (RS Status).
That's all !
The easiest way is to shutdown your primary (assuming that primarydb is your primary and secondaryDB is your secondary. The process would be as follow
Connect to your primary and secondaries.
Stop the primary.
Look at your secondaries if a new primary is elected.
Connect to your primary in the shell
conn1 = new Mongo("localhost:31000")
primaryDB = conn1.getDB("test")
Connect to your secondaries
conn2 = new Mongo("localhost:31001")
secondaryDB = conn2.getDB("test")
...
Shutdown your primary
primaryDB.adminCommand({"shutdown" : 1})
Check if one of the secondaries is elected as the primary
secondaryDB.isMaster()
In the result of the last operation you will see which server is now the new primary (look out for the primary key).
As mentioned in the comments, you should have at least three servers (one primary, two secondaries) to get the election process working.

Do you lose records when you reconfigure mongodb replicaset?

I have 3 member replicaSet in MongoDB which fell apart when re-configuring the host names of the sever instances. I had to reconfigure the replicaSet, however I am curious how MongoDB handles records that are not synced across all the members.
Case 1) There is a new record on the MongoDB server that I access to reconfigure the set.
Case 2) There is a new record on another MongoDB server that is added later to the replica set.
Each replica-set has one primary node and one or more secondary nodes.
All writes happen on the primary. The primary then sends these changes to the secondaries (the list of changes is referred to as "the oplog"). That means the primary is always the member with the most up-to-date data.
When the primary is suddenly unreachable, the replica-set is put into read-only mode and an election takes place to find a new primary. Usually the secondary which is most up-to-date is selected (more details on replica-set election). Any writes which were not propagated to that secondary yet are lost.
When the old primary goes back online, it re-joins the replica-set as a secondary. Its data gets synchronized to the state of the new primary. Any writes which only happened on the old primary which weren't propagated to the new primary before the crash are rolled back.
The rolled-back writes are backed up as bson-files in the directory /rollback and can be re-added to the replica-set using bsondump and mongorestore. Details about this procedure can be found in the article Rollbacks During Replica Set Failover

why closing down the primary doesn't make replica to vote a new primary in mongodb replica set

I am experimenting the mongodb replica set, in which I started localhost:27017 as secondary and localhost:27018 as primary.
then I disconnect the localhost:27018, and I expected localhost:27017 to automatically become a primary, but it doesn't work as expected. In the shell, using command like rs.add() or rs.remove() gives out a error like ' "errmsg" : "replSetReconfig command must be sent to the current replica set primary.",'
I know this error is because the command is now running in the secondary, but since the primary is closed down already. what steps I should do now?
And also why closing down the primary doesn't allow the replica set to vote for a new primary ? what is the flexible way to make it automatic vote for a new primary
To elect a new primary in MongoDB replica set the majority of the members must vote for the new primary. That's majority of the original (or configured) members.
You configured two members - one is not a majority. You need to add a third member, either a regular node or an arbiter, if you want your failover to happen automatically.