Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-22383

mongo shell should permit mongodb:// URI without database name

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.2.9, 3.3.10
    • Affects Version/s: 3.2.0
    • Component/s: Shell
    • Labels:
    • Fully Compatible
    • ALL
    • Platforms 17 (07/15/16)

      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
      

            Assignee:
            jonathan.reams@mongodb.com Jonathan Reams
            Reporter:
            kevin.pulo@mongodb.com Kevin Pulo
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated:
              Resolved: