How do you connect to replication from the MongoDB shell?

If I write an application that connects to mongodb, then I can provide a list of seeds for replication, and the driver will direct me to the node master, where I can run write commands.

How to specify seed list for mongo command line to go to replication.

+64
mongodb mongo-shell
Dec 17 '12 at 10:55
source share
10 answers

To connect to the primary replica set, use the --host shell 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. In versions 3.4.2 to 3.4.10 there was an error ( SERVER-28072 ), due to which it was not possible to specify db after using --host or --port.

+93
Feb 23 '15 at 2:04
source share

The answers above are for Mongo 3.2 .

According to the Mongo 3.4 documentation , the shell has been slightly modified:

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"

+22
Mar 08 '17 at 12:00
source share

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

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

This should give you a connection to what node is currently the main one and handle rollback. You can specify one or more seeds, and he will find the rest.

Note that the (AFAIK) shell does not allow you to route reads to secondary with a replica-based installation.

+13
Dec 17 '12 at 18:57
source share

All you have to do is use -host and provide it with one of your hosts in replication, but with the replication name as the prefix.

For example:

 mongo --host my_mongo_server1 

will connect to my_mongo_server1, it could just be 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 is not my_mongo_server1.

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

Hope this helped.

+10
Sep 15 '15 at 1:31
source share

As far as I know, the mongo command-line client will not accept seeds to redirect you to the node master, because you can often actually use a secondary node rather than redirecting you.

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

+8
Dec 17 '12 at 11:21
source share

In the shell, you can first use:

 mongo --nodb 

to open a mongo session without connecting to mongo replicaset

Then, as Christina said, you should be able to use

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

to connect to the replicator.

Or, in the end, put

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

in your js file and

 mongo --nodb yourcode.js 
+7
May 23 '14 at 11:09
source share

You can use the --host option to specify a name and a sampling list, and 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]

+3
Mar 17 '15 at 20:51
source share

Based on Chris Hade's answer, these two bash aliases allow me to connect to the wizard with a single command (where db1.test.test is one of the members of the replica set, acme is the name of the database, mreppy is my account, etc. .). Of course, if db1 does not work, but it is still convenient.

 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 quote in the alias eval is complex, I used How to avoid single quotes in single quotes? for reference: -)

+1
Oct 30 '13 at 14:14
source share

I am using v3.4. Also new to mongodb ... Although the reference from "man mongo" suggests using the "-host replicaSet / host: port, host: port" url, this does not work for me. However, I can connect to my replica according to the white paper as shown 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 think the man page of my mongo is out of date (I am using CentOS 7.3).

0
Feb 08 '17 at 6:55
source share
 mongodb://< dbuser >:< dbpassword >@example.com:< port >,example2.com:< port >/< dbname >?replicaSet=setname 
0
May 13 '19 at 11:35
source share



All Articles