[SERVER-22383] mongo shell should permit mongodb:// URI without database name Created: 30/Jan/16  Updated: 22/Nov/16  Resolved: 12/Jul/16

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: 3.2.0
Fix Version/s: 3.2.9, 3.3.10

Type: Bug Priority: Major - P3
Reporter: Kevin Pulo Assignee: Jonathan Reams
Resolution: Done Votes: 0
Labels: code-only
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by CXX-846 Connection string URI parsing is no c... Closed
Related
related to SERVER-6233 Support MongoDB URIs as mongo shell a... Closed
is related to SERVER-22707 MongoDB shell's parsing of connection... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Completed:
Sprint: Platforms 17 (07/15/16)
Participants:

 Description   

The mongodb:// URI-style connection string format permits the database name to be absent. However, the shell complains if this dbname (or the preceding backslash) isn't specified:

$ /m/3.2.1/bin/mongo mongodb://genique:11111
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111
2016-01-30T22:24:17.085+1100 E QUERY    [thread1] Error: Missing database name in connection string "mongodb://genique:11111" :
connect@src/mongo/shell/mongo.js:220:1
@(connect):1:6
 
exception: connect failed
$ /m/3.2.1/bin/mongo mongodb://genique:11111/
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111/
2016-01-30T22:24:18.815+1100 E QUERY    [thread1] Error: Missing database name in connection string "mongodb://genique:11111/" :
connect@src/mongo/shell/mongo.js:220:1
@(connect):1:6
 
exception: connect failed
$ /m/3.2.1/bin/mongo mongodb://genique:11111/db
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111/db
>
bye
$ /m/3.2.1/bin/mongo mongodb://genique:11111?replicaSet=replset
FailedToParse std::exception
try '/m/3.2.1/bin/mongo --help' for more information
$ /m/3.2.1/bin/mongo mongodb://genique:11111/?replicaSet=replset
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111/?replicaSet=replset
2016-01-30T22:26:02.226+1100 I NETWORK  [thread1] Starting new replica set monitor for replset/genique:11111
2016-01-30T22:26:02.226+1100 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-01-30T22:26:02.227+1100 I NETWORK  [thread1] changing hosts to replset/genique:11111,genique:11112 from replset/genique:11111
2016-01-30T22:26:02.230+1100 E QUERY    [thread1] Error: Missing database name in connection string "mongodb://genique:11111/?replicaSet=replset" :
connect@src/mongo/shell/mongo.js:220:1
@(connect):1:6
 
exception: connect failed
$ /m/3.2.1/bin/mongo mongodb://genique:11111/db?replicaSet=replset
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111/db?replicaSet=replset
2016-01-30T22:26:12.385+1100 I NETWORK  [thread1] Starting new replica set monitor for replset/genique:11111
2016-01-30T22:26:12.385+1100 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-01-30T22:26:12.386+1100 I NETWORK  [thread1] changing hosts to replset/genique:11111,genique:11112 from replset/genique:11111
>
bye

Note that the URI spec specifies that if the database name is absent and authentication credentials are also specified, then the default db is admin (unless the authSource option (or, for the shell, --authenticationDatabase) is also specified). Care will need to be taken to override the shell's default database of test in this case. The spec doesn't specify a default database otherwise; presumably the shell's normal behaviour of defaulting to the test database is the most appropriate behaviour.

It is interesting how late the missing database name is found, it is well after parsing has finished, and after at least 1 initial connection.

This means that if an expected connection can't be created, the missing dbname is not noticed:

$ /m/3.2.1/bin/mongo mongodb://genique:33333
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:33333
2016-01-30T22:42:59.384+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:42:59.384+1100 E QUERY    [thread1] Error: couldn't connect to server genique:33333, connection attempt failed :
connect@src/mongo/shell/mongo.js:218:14
@(connect):1:6
 
exception: connect failed
$ /m/3.2.1/bin/mongo mongodb://genique:33333/db?replicaSet=replset
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:33333/db?replicaSet=replset
2016-01-30T22:43:44.723+1100 I NETWORK  [thread1] Starting new replica set monitor for replset/genique:33333
2016-01-30T22:43:44.723+1100 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-01-30T22:43:44.723+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:44.723+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:44.723+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 1 checks in a row. Polling will stop after 29 more failed checks
2016-01-30T22:43:45.224+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:45.224+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:45.224+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 2 checks in a row. Polling will stop after 28 more failed checks
2016-01-30T22:43:45.725+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:45.725+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:45.725+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 3 checks in a row. Polling will stop after 27 more failed checks
2016-01-30T22:43:46.225+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:46.225+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:46.225+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 4 checks in a row. Polling will stop after 26 more failed checks
^C
$ /m/3.2.1/bin/mongo mongodb://genique:33333/?replicaSet=replset
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:33333/?replicaSet=replset
2016-01-30T22:43:56.926+1100 I NETWORK  [thread1] Starting new replica set monitor for replset/genique:33333
2016-01-30T22:43:56.927+1100 I NETWORK  [ReplicaSetMonitorWatcher] starting
2016-01-30T22:43:56.927+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:56.927+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:56.927+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 1 checks in a row. Polling will stop after 29 more failed checks
2016-01-30T22:43:57.428+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:57.428+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:57.428+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 2 checks in a row. Polling will stop after 28 more failed checks
2016-01-30T22:43:57.929+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:57.929+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:57.929+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 3 checks in a row. Polling will stop after 27 more failed checks
2016-01-30T22:43:58.430+1100 W NETWORK  [thread1] Failed to connect to 127.0.1.1:33333, reason: errno:111 Connection refused
2016-01-30T22:43:58.430+1100 W NETWORK  [thread1] No primary detected for set replset
2016-01-30T22:43:58.430+1100 I NETWORK  [thread1] All nodes for set replset are down. This has happened for 4 checks in a row. Polling will stop after 26 more failed checks
^C

By contrast, a missing trailing slash when options are used (which is still a valid URI) is noticed early at parse time:

$ /m/3.2.1/bin/mongo mongodb://genique:11111
MongoDB shell version: 3.2.1
connecting to: mongodb://genique:11111
2016-01-30T22:24:17.085+1100 E QUERY    [thread1] Error: Missing database name in connection string "mongodb://genique:11111" :
connect@src/mongo/shell/mongo.js:220:1
@(connect):1:6
 
exception: connect failed
$ /m/3.2.1/bin/mongo mongodb://genique:11111?replicaSet=replset
FailedToParse std::exception
try '/m/3.2.1/bin/mongo --help' for more information
$ /m/3.2.1/bin/mongo mongodb://genique:33333?replicaSet=replset
FailedToParse std::exception
try '/m/3.2.1/bin/mongo --help' for more information



 Comments   
Comment by Githook User [ 13/Jul/16 ]

Author:

{u'username': u'jbreams', u'name': u'Jonathan Reams', u'email': u'jbreams@mongodb.com'}

Message: SERVER-22383 Do not throw in shell if no database is specified in MongoURI

(cherry picked from commit dd0165b595c8470d7252218a490c2deb9ad2e387)
Branch: v3.2
https://github.com/mongodb/mongo/commit/9fa58371ef69ffa1c310e67e524949e8f51d0827

Comment by Githook User [ 12/Jul/16 ]

Author:

{u'username': u'jbreams', u'name': u'Jonathan Reams', u'email': u'jbreams@mongodb.com'}

Message: SERVER-22383 Do not throw in shell if no database is specified in MongoURI
Branch: master
https://github.com/mongodb/mongo/commit/dd0165b595c8470d7252218a490c2deb9ad2e387

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