83

If I'm writing an application which connects to mongodb then I can provide a seed list for a replicaset, and the driver will direct me to the master node, where I can run write commands.

How do I specify the seed list for a commandline mongo shell in order to conenct to a replicaset.

Richard Warburton
  • 1,462
  • 1
  • 9
  • 13

10 Answers10

120

To connect to a replica set Primary use the mongo shell --host option:

mongo --host replicaSetName/host1[:porthost1],host2[:porthost1],host3[:porthost3],etc

For example:

$ mongo --host rs1/john.local:27019,john.local:27018
MongoDB shell version: v3.4.9
connecting to: mongodb://john.local:27019,john.local:27018/?replicaSet=rs1
2017-10-12T14:13:03.094+0000 I NETWORK  [thread1] Starting new replica set monitor for rs1/john.local:27019,john.local:27018
2017-10-12T14:13:03.096+0000 I NETWORK  [thread1] Successfully connected to john.local:27019 (1 connections now open to john.local:27019 with a 5 second timeout)
2017-10-12T14:13:03.096+0000 I NETWORK  [thread1] Successfully connected to john.local:27018 (1 connections now open to john.local:27018 with a 5 second timeout)
rs1:PRIMARY> db
test
rs1:PRIMARY>

Note: From versions 3.4.2 to 3.4.10 there was a bug (SERVER-28072) which prevented specifying the db after when using --host or --port.

M. Justin
  • 14,487
  • 7
  • 91
  • 130
Gianfranco P.
  • 10,049
  • 6
  • 51
  • 68
  • 3
    Can you please clearify: Does "host1", "host2" include masters, slaves and arbiters? Or only hosts which we write to (masters) ? – Daniel W. Apr 11 '16 at 13:38
  • I'm getting an error that says all nodes for set are down, but rs.status() shows primary and secondary are up and healthy. – SSH This Jul 12 '16 at 18:46
  • @SSHThis are you sure you don't have a typo, using the wrong hostname or the mongod processes are binded to the wrong IP/interface? Double check the first 3/4 lines of the mongo shell output – Gianfranco P. Jul 13 '16 at 14:39
  • @GianPaJ Thank you sorry, you were right, I wasn't using the correct name for my replicaset, – SSH This Jul 13 '16 at 15:59
  • This works fine for me: `mongo --host repl-set-name/localhost` — this won't work, though, if the repl set member on localhost is offline, and in my case, that's fine. Localhost happens to be a secondary, in my case. – KajMagnus Sep 29 '16 at 10:20
  • 3
    Note: Due to a bug this doesn't work in MongoDB 3.4.0 and 3.4.1 (https://jira.mongodb.org/browse/SERVER-27289) – qff Feb 28 '17 at 14:21
34

The answers above are for Mongo 3.2.

According to Mongo 3.4 documentation, the shell was changed a bit:

In 3.2:
mongo --host host1,host2,host3/myRS myDB
or,
mongo --host host1:27017,host2:27017,host3:27017/myRS myDB

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

Moshe
  • 9,283
  • 4
  • 29
  • 38
Shemeshey
  • 551
  • 4
  • 7
20

All you have to do is to use --host and give it one of your hosts in the replicaset but with the name of the replicaset as a prefix.

For example:

mongo --host my_mongo_server1

will connect to my_mongo_server1, it may just be yet another SECONDARY node.

But:

mongo --host my_repl_set_name/my_mongo_server1

will always connect to the PRIMARY node in the replica set, even if it's not my_mongo_server1.

Why? The answer is "replica set monitor". In the example above, mongo shell would connect to the specified node, start a new replica set monitor for the replica set and will use the specified node just to seed it. From there, the monitor will figure out all nodes in the replica set and will switch the connection to the PRIMARY node.

Hope that helped.

Doron Levari
  • 566
  • 4
  • 4
16

You can use the "name/seed1,seed2,..." format:

> conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")
> db = conn.getDB("test")

This should give you a connection to whichever node is currently primary and handle failover okay. You can specify one or more seeds and it'll find the rest.

Note that (AFAIK) the shell does not allow you to route reads to secondaries with a replica set connection.

kris
  • 23,024
  • 10
  • 70
  • 79
11

To the best of my knowledge, the mongo command line client will not accept seeds to forward you to the master node, because you may often want to actually operate on the secondary node rather than being forwarded.

However, once connected to any node in the RS, you can discover the RS topology via rs.config() or db.isMaster(). You could then use this information to reconnect to the primary node. Depending on your shell, you might be able to use mongo --eval "db.isMaster()['primary']" to automatically connect to the master.

Chris Heald
  • 61,439
  • 10
  • 123
  • 137
  • 2
    Nice tip, ``mongo --host `mongo --quiet --eval "db.isMaster()['primary']"` `` – KCD Jul 15 '14 at 20:04
  • 8
    Actually, it will. mongo --host 1.1.1.1:27017 will connect you directly to a node without forwarding you to the primary. mongo --host rsName/1.1.1.1:20717 will discover all nodes and forward you to the primary. – Joshua Lawrence Austill Mar 08 '16 at 23:11
7

In the shell, you can first use:

mongo --nodb

to open a mongo session without connecting to mongo replicaset

Then, like kristina said, then you should be able to use

conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")

to connect to a replicaset.

Or eventually put

conn = new Mongo("myReplicaSet/A:27017,B:27017,C:27017")

in your js file and

mongo --nodb yourcode.js
geekysteven
  • 113
  • 1
  • 6
5

You can use the --host param to specify the replSet name and seed list, then mongo will automatically connect to the current primary host.

example:
mongo --host rs0/1.example.com:27017,2.example.com:27017,3.example.com:27017 [dbname]

Sam Hiatt
  • 385
  • 4
  • 8
1

Building on the answer by Chris Heald these two bash aliases let me connect to the master with one command (where db1.test.test is one member of the replica set, acme is the database name, mreppy is my account etc) It will fail of course if db1 is down, but it's still handy.

alias whichprimary='mongo db1.test.test/acme --username mreppy --password testtest --quiet --eval "db.isMaster()['"'primary'"']"' 
alias connectprimary='mongo -u mreppy -p testtest `whichprimary`/acme'

The quoting in the eval alias is hard, I used How to escape single-quotes within single-quoted strings? for help :-)

Community
  • 1
  • 1
mreppy
  • 667
  • 5
  • 5
  • In my case I was needing this in a bash script, so I added a function like this one: `function replica_set_master_host { mongo --quiet -u user -p password --eval "db.isMaster()['primary']" }` – Tonatiuh Jun 09 '14 at 20:45
1

I am using v3.4. Also new to mongodb stuff... Although the help info from "man mongo" suggests to use "--host replicaSet/host:port,host:port" url, it does not work for me. However, I can connect to my replicaSet according to official document, as below:

$ mongo "mongodb://c1m,c2m,c3m/?replicaSet=rs0"
MongoDB shell version v3.4.1
connecting to: mongodb://c1m,c2m,c3m/?replicaSet=rs0
2017-02-08T14:46:43.818+0800 I NETWORK  [main] Starting new replica set monitor for rs0/c1m:27017,c2m:27017,c3m:27017
MongoDB server version: 3.4.1
Server has startup warnings:
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten]
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2017-02-08T13:31:14.672+0800 I CONTROL  [initandlisten]
rs0:PRIMARY>

So I guess the man page of my mongo is outdated (I am using CentOS 7.3).

bruin
  • 979
  • 1
  • 10
  • 30
1
mongodb://< dbuser >:< dbpassword >@example.com:< port >,example2.com:< port >/< dbname >?replicaSet=setname
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
IamDsp
  • 9
  • 3