zookzook / elixir-mongodb-driver

MongoDB driver for Elixir
Apache License 2.0
245 stars 63 forks source link

Domain name is ignored when port is 27017 #75

Closed floriandotorg closed 3 years ago

floriandotorg commented 4 years ago

I noticed today, that the mongo driver ignores the domain name when the port is 27017, example code:

defmodule App.Application do
  use Application

  def start(_type, _args) do
    database_config = Application.get_env(:app, :db_config)

    # List all child processes to be supervised
    children = [
      # Start config agent
      App.Settings,
      # Start the Mongo repository
      %{
        id: Mongo,
        start: {Mongo, :start_link, [database_config]}
      },
      # Start the endpoint when the application starts
      AppWeb.Endpoint
    ]

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: App.Supervisor]
    Supervisor.start_link(children, opts)
  end

This config works:

config :app, :db_config,
  name: :mongo,
  url: "mongodb://mongo:27018/meteor"

This config doesn't:

config :app, :db_config,
  name: :mongo,
  url: "mongodb://mongo:27017/meteor"

It tries to connect to localhost instead of mongo only when the port 27017: [error] Mongo.MongoDBConnection (#PID<0.1025.0>) failed to connect: ** (Mongo.Error) localhost:27017 tcp connect: connection refused - :econnrefused

zookzook commented 4 years ago

What is the IP address for "mongo"? Maybe it is a configuration problem?

floriandotorg commented 4 years ago

Hey Zook, mongo is the DNS name in a Docker configuration, that's completely fine. It's really a problem with the mongo driver itself, it always tries to connect to localhost if the port is 27017. I think the error lies in the UrlParser.

zookzook commented 3 years ago

So, after a long while back to this issue. I build a demo app, using you configuration and I get this log output:

[error] Mongo.MongoDBConnection (#PID<0.473.0>) failed to connect: ** (Mongo.Error) mongo:27017 tcp connect: connection refused - :econnrefused
[error] Mongo.MongoDBConnection (#PID<0.473.0>) failed to connect: ** (Mongo.Error) mongo:27017 tcp connect: connection refused - :econnrefused

So, the URL-Parser does not change the hostname mongo to localhost.

    database_config = Application.get_env(:check_domain, :db_config)

    children = [
      %{
        id: Mongo,
        start: {Mongo, :start_link, [database_config]}
      },
      CheckDomainWeb.Endpoint
    ]

And the config looks like that:

config :check_domain, :db_config,
       name: :mongo,
       url: "mongodb://mongo:27017/meteor"

Unfortunately I'm not able to reproduce your issue. Maybe it is already resolved. Can I close this issue?

floriandotorg commented 3 years ago

Thanks for your effort. I will update the package and check on our side.