Home > Net >  How I can switch to secondary node from primary
How I can switch to secondary node from primary

Time:05-25

I'm using a replicaset with 3 nodes. Node primary with name n1 and port 27018, secondaries n2 port 27019 and n3 por 27020.

I would like to switch to any secondady node from primary. I've tried with mongo -host n2 -port 27019 but not work for me returns: "uncaught exception: SyntaxError: unexpected token: identifier : @(shell):1:12"

There is another way to do it? I looked in the documentation but I didn't found nothing

Thanks

CodePudding user response:

  • In command terminal you can try.

mongo "mongodb://host1:27017,host2:27017,host3:27017/myDB?replicaSet=myRS"

or

  • In the shell, you can first open a mongo session without connecting to mongo replicaset

mongo --nodb

  • in mongo shell you can connect to a replicaset.

conn = new Mongo("myRS/host1:27017,host2:27017,host3:27017")

db = conn.getDB("myDB")

CodePudding user response:

Please run the same command from the CLI instead of the Mongo Shell. Also consider to use the double dash when specify the full name options, like above:

mongo --host n2 --port 27019

In this way you will be able to accesso in read-only mode the data. In order to navigate dbs you will need to run the following command first:

rs.secondaryOk()

Instead, if your needings are to switch the primary node to the "n2" node, you will need to run the following queries in your primary:

cfg = rs.config()
cfg.members[1].priority = 10 \\ "set a value higher than the primary node"
rs.reconfig(cfg)
rs.stepDown()

In this way you will set the election priority and running the step down command will force the n2 node to become primary

  • Related