[SERVER-57929] Inconsistent handling of mongodb:// URIs Created: 17/Jun/21  Updated: 27/Oct/23  Resolved: 28/Jun/21

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: 4.0.25
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Tais P. Hansen Assignee: Edwin Zhou
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

Description

Attempting to use the mongodb URI "mongodb://0.0.0.0:27017/db" works with mongodump/mongorestore but fails with mongo shell.

When running mongo inside a docker-compose environment, I would typically run "docker-compose port mongodb 27017" to get the address and port which docker mapped the docker container port to on the local host. Ie. it could respond with "0.0.0.0:49156".

 

Example:

MONGODB_URI="mongodb://$(docker-compose port mongodb 27017)/db"

mongorestore --uri "$MONGODB_URI" --dir dump --drop

mongo "$MONGODB_URI" database/transformMessages.js

 

The last step fails however, which the error message below:

MongoDB shell version v4.0.25
connecting to: mongodb://0.0.0.0:49156/?gssapiServiceName=mongodb
2021-06-17T11:55:16.488+0200 E QUERY [js] Error: couldn't connect to server 0.0.0.0:49156, address resolved to 0.0.0.0 :
connect@src/mongo/shell/mongo.js:356:17
@(connect):2:6
exception: connect failed

 

And just to confirm a connection is possible:

curl -sv http://0.0.0.0:49156/

  • Trying 0.0.0.0:49156...
  • TCP_NODELAY set
  • Connected to 0.0.0.0 (127.0.0.1) port 49156 (#0)
    > GET / HTTP/1.1
    > Host: 0.0.0.0:49156
    > User-Agent: curl/7.68.0
    > Accept: /
    >
  • Mark bundle as not supporting multiuse
  • HTTP 1.0, assume close after body
    < HTTP/1.0 200 OK
    < Connection: close
    < Content-Type: text/plain
    < Content-Length: 85
    <
    It looks like you are trying to access MongoDB over HTTP on the native driver port.
  • Closing connection 0

 

Steps to Reproduce

1. docker run --rm -it -p 27017 --name mongo -d mongo

2. MONGODB_URI="mongodb://$(docker port mongo 27017 | head -n1)/test"

3. mongo "$MONGODB_URI"

 

 



 Comments   
Comment by Tais P. Hansen [ 29/Jun/21 ]

Hi Edwin,

 

Mongo shell is the only tool I've encountered that cannot use 0.0.0.0. Both mysql, psql and cockroach are all able to connect to a server using 0.0.0.0 as the host name.

 

RFC 5735 states: 
Address 0.0.0.0/32 may be used as a source address for this
host on this network
Listening on 0.0.0.0:8080 is usually how one would bind to port 8080 on all interfaces. Logically (although not explicitly stated) a client could be assumed to be able to connect to a local server using the same parameters.

 

I find it odd that you would accept inconsistent behaviour between mongo's own tools. This should be changed from a bug to a feature request then.

 

 

Comment by Edwin Zhou [ 28/Jun/21 ]

Hi tais.hansen@gmail.com,

The IP address 0.0.0.0 is typically reserved for something unspecified. In the mongo shell, we throw an error when the host address is "0.0.0.0".
https://github.com/mongodb/mongo/blob/master/src/mongo/client/dbclient_connection.cpp#L416

As you can see from the output from the curl command, the address 0.0.0.0 is resolving to 127.0.0.1, which is the localhost address. You may find success replacing "0.0.0.0" with "127.0.0.1".

Best,
Edwin

Comment by Tim Fogarty [ 22/Jun/21 ]

Hi tais.hansen@gmail.com, sorry to hear about this. It sounds like you're specifically having trouble with the behavior of the mongo shell. So I'm going to move this ticket to the Server Jira project (which is where we track issues with the mono shell) so somebody can take a closer look.

Generated at Thu Feb 08 05:43:08 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.