[SERVER-22382] mongo shell should accept mongodb:// URI for --host Created: 30/Jan/16  Updated: 23/Feb/18  Resolved: 18/Aug/16

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.2.0
Fix Version/s: 3.3.12

Type: Improvement Priority: Major - P3
Reporter: Kevin Pulo Assignee: Matt Cotter
Resolution: Done Votes: 1
Labels: bkp
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
is depended on by SERVER-30850 startParallelShell cannot be used in ... Closed
Documented
is documented by DOCS-8934 mongo shell should accept mongodb:// ... Closed
is documented by DOCS-9808 mongo --host doesn't mention new supp... Closed
Related
related to SERVER-6233 Support MongoDB URIs as mongo shell a... Closed
is related to SERVER-33227 Using the method `connect` in a scrip... Closed
is related to SERVER-27289 Shell: mongo --host replSet/Host:Port... Closed
Backwards Compatibility: Fully Compatible
Backport Requested:
v3.2
Sprint: Platforms 17 (07/15/16), Platforms 18 (08/05/16), Platforms 2016-08-26
Participants:

 Description   

The mongo shell accepts mongodb:// URI-style connection strings as a plain argument:

$ /m/3.2.1/bin/mongo mongodb://genique:11111/test?replicaSet=replset
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111/test?replicaSet=replset
2016-01-30T00:55:35.554+1100 I NETWORK  [thread1] Starting new replica set monitor for replset/genique:11111
2016-01-30T00:55:35.554+1100 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-01-30T00:55:35.555+1100 I NETWORK  [thread1] changing hosts to replset/genique:11111,genique:11112 from replset/genique:11111
>
bye

However, this format currently does not work if passed as the argument to the --host option:

$ /m/3.2.1/bin/mongo --host mongodb://genique:11111/test?replicaSet=replset
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111/test?replicaSet=replset/test
2016-01-30T00:55:40.327+1100 I NETWORK  [thread1] Starting new replica set monitor for replset/test/genique:11111
2016-01-30T00:55:40.327+1100 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-01-30T00:55:40.328+1100 W NETWORK  [thread1] node: genique:11111 isn't a part of set: replset/test ismaster: { hosts: [ "genique:11111", "genique:11112" ], arbiters: [ "genique:11113" ], setName: "replset", setVersion: 1, ismaster: true, secondary: false, primary: "genique:11111", me: "genique:11111", electionId: ObjectId('56ab6ab10000000000000005'), maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 1000, localTime: new Date(1454075740328), maxWireVersion: 4, minWireVersion: 0, ok: 1.0 }
2016-01-30T00:55:40.328+1100 W NETWORK  [thread1] No primary detected for set replset/test
2016-01-30T00:55:40.328+1100 I NETWORK  [thread1] All nodes for set replset/test are down. This has happened for 1 checks in a row. Polling will stop after 29 more failed checks
2016-01-30T00:55:40.829+1100 W NETWORK  [thread1] node: genique:11111 isn't a part of set: replset/test ismaster: { hosts: [ "genique:11111", "genique:11112" ], arbiters: [ "genique:11113" ], setName: "replset", setVersion: 1, ismaster: true, secondary: false, primary: "genique:11111", me: "genique:11111", electionId: ObjectId('56ab6ab10000000000000005'), maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 1000, localTime: new Date(1454075740829), maxWireVersion: 4, minWireVersion: 0, ok: 1.0 }
2016-01-30T00:55:40.829+1100 W NETWORK  [thread1] No primary detected for set replset/test
2016-01-30T00:55:40.829+1100 I NETWORK  [thread1] All nodes for set replset/test are down. This has happened for 2 checks in a row. Polling will stop after 28 more failed checks
2016-01-30T00:55:41.329+1100 W NETWORK  [thread1] node: genique:11111 isn't a part of set: replset/test ismaster: { hosts: [ "genique:11111", "genique:11112" ], arbiters: [ "genique:11113" ], setName: "replset", setVersion: 1, ismaster: true, secondary: false, primary: "genique:11111", me: "genique:11111", electionId: ObjectId('56ab6ab10000000000000005'), maxBsonObjectSize: 16777216, maxMessageSizeBytes: 48000000, maxWriteBatchSize: 1000, localTime: new Date(1454075741329), maxWireVersion: 4, minWireVersion: 0, ok: 1.0 }
2016-01-30T00:55:41.329+1100 W NETWORK  [thread1] No primary detected for set replset/test
2016-01-30T00:55:41.329+1100 I NETWORK  [thread1] All nodes for set replset/test are down. This has happened for 3 checks in a row. Polling will stop after 27 more failed checks
^C

But --host accepts all other valid connection specifications, ie:

  • mongo --host hostname
  • mongo --host hostname:port
  • mongo --host replsetname/seedhostname1:port,seedhostname2:port,...
  • mongo --host /tmp/mongodb-XXXXX.sock

Therefore, it's reasonable to expect that

  • mongo --host mongodb://...

should also work, with the same behaviour as when --host is not used.

The problem appears to be that the dbname specified by a plain argument (which defaults to "test") gets appended to the --host argument (as well as a slash). For the traditional forms of --host (listed above), this makes sense because they can't specify the dbname. So appending the dbname to --host gives the corresponding plain argument, eg. "mongo --host hostname:port dbname" is equivalent to "mongo hostname:port/dbname". But this doesn't make sense for the URI format, because it has its own way of specifying the dbname. So a simple fix should be to avoid appending "/dbname" if the argument to --host starts with "mongodb://".



 Comments   
Comment by Matt Cotter [ 18/Aug/16 ]

--host flag now accepts full mongo URI's

Comment by Githook User [ 18/Aug/16 ]

Author:

{u'username': u'Machyne', u'name': u'Matt Cotter', u'email': u'matt.cotter@mongodb.com'}

Message: SERVER-22382 allow mongo --host to take uri
Branch: master
https://github.com/mongodb/mongo/commit/88b540f0c14c7ab8af708aecc4cd6df83081b32e

Generated at Thu Feb 08 04:00:14 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.