Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Unable to connect remotely (non-localhost) : aka library ignores URI hostname #382

@blackham

Description

@blackham

I am having difficultly connecting to MongoDB remotely. It appears it is trying to connect to localhost regardless of what the hostname or URI/URL is set to.

I am able to connect to MongoDB from Compass (GUI) using the URL mongodb://10.192.10.70:27017/?directConnection=true
I am able to connect to MongoDB using elixir-mongo/mongodb if I run elixir on the the same server as mongo (localhost) using: {:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/parties"}

However, from a remote computer the following command produces an error.

{:ok, conn} = Mongo.start_link(url: "mongodb://10.192.10.70:27017/parties?directConnection=true")
{:ok, #PID<0.574.0>}
[error] Mongo.Protocol (#PID<0.588.0>) failed to connect: ** (Mongo.Error) localhost:27017 tcp connect: connection refused - :econnrefused

I tried the following, all with the same error about (Mongo.Error) localhost:27017 tcp connect: connection refused

{:ok, conn} = Mongo.start_link(url: "mongodb://10.192.10.70:27017/?replicaSet=rs0&tls=false&directConnection=true")
{:ok, pid} = Mongo.start_link(database: "parties", seeds: ["10.192.10.70:27017"])
{:ok, conn} = Mongo.start_link(url: "mongodb://10.192.10.70:27017/parties?directConnection=true", hostname: "10.192.10.70")
{:ok, conn} = Mongo.start_link(url: "mongodb://10.192.10.70:27017/parties", hostname: "10.192.10.70")
{:ok, conn} = Mongo.start_link(url: "mongodb://10.192.10.70:27017/parties")

I did toss some debug code into Mongo.Protocol.Utils and Mongo.UrlParser which shows it has the correct URL

[
...
seeds: ["10.192.10.70:27017"],
hostname: "10.192.10.70",
port: 27017
]
[
pool_index: 1,
topology_pid: #PID<0.574.0>,
connection_type: :client,
database: "parties",
seeds: ["10.192.10.70:27017"],
hostname: "10.192.10.70",
port: 27017
]

But the inspect output of opts from Mongo.Protocol shows tcp_connect has the following opts:

[
pool_index: 1,
pool_size: 1,
topology_pid: #PID<0.5876.0>,
connection_type: :monitor,
backoff_type: :rand,
backoff_max: 10000,
backoff_min: 10000,
after_connect: {Mongo.Monitor, :connected, [#PID<0.5887.0>, #PID<0.5876.0>]},
skip_auth: true,
database: "admin",
pool: DBConnection.ConnectionPool,
seeds: ["10.192.10.70:27017"],
hostname: "localhost",
port: 27017
]

Note hostname is now "localhost" oops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions