MongoDB replica set no primary, need to force new primary - mongodb

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.

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 replica set (Write on secondary)

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

Getting data from standalone into existing replica set without downtime

I have a standlone mongo instance running on AWS and now I created managed replica set using the mongo cloud manager (hosted on AWS as well). No I would like to get my data over from the standalone instance into the replica set if possible without downtime.
Shutting down all services generating a backup of the standalone instance and importing this into the replica set might be the possibility with a downtime.
If I add the standalone node as another replica set member to the new replica set what will happen then? will my standalone instance content be deleted? Or will this be synced to all other replica set hosts as well?
To increase security you need to have 3 systems in replica set (primary, secondary and secondary OR arbiter), having only pri/sec would cause more issues than expected.
if you server (primary) is not started with replica set parameter activated
mongod --replSet "rs0"
then, you need to add entry in config file or add startup parameter --replSet "myReplSet" - and this will cause a breakdown in your service.
then using manual from mongo you can go via steps and initiate single server replica set, then add secondary system.

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.