Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
blackham opened this issue Jun 24, 2024 · 0 comments

Comments

@blackham
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant