Search code examples
mongodberlangelixirreplicaset

How to connect to mongo replica cluster in elixir


I created a mongo replica set from which I am trying to connect with elixir. I created 3 mongo instance in replica set and gave hostnames in /etc/hosts so the thing is that everything is working fine in mongo terminal and I am able to connect properly with mongo replica set. I wrote code in NODE.JS to fetch data from replica set using mongodb library and it is also working fine(It means configuration in mongo server and my local server is fine as much as I know ), but when I am trying to connect it through elixir it is throwing error when I am connecting to that replica set.

I am using following library to connect with it.

https://github.com/ankhers/mongodb

as in library author has suggested to use following configuration

{:ok, pid} = Mongo.start_link(database: "test", seeds: "hostname1.net:27017", "hostname2.net:27017")

which is throwing syntax error.

** (SyntaxError) iex:6: syntax error before: "hostname2.net:27017"

which is obvious as it is wrong.

When I am using this configuration

worker(Mongo, [[name: :mongo,database: "yatender", topology: "replica_set_no_primary",seeds: ["xxx.xxx.xxx.xxx:27017","xxx.xxx.xxx.xxx:27017","xxx.xxx.xxx.xxx:27017"] ,pool: DBConnection.Poolboy]])

Note:I have changed host address as xxx so don't get confused with it.

GenServer #PID<0.1436.0> terminating
** (stop) exited in: GenServer.call(#PID<0.1410.0>, 
{:server_description, %{address: "xxx.xxx.xxx.xxx:27017", arbiters: [], 
election_id: nil, error: nil, hosts: ["mongo.host1:27017", 
"mongo.host2:27017", "mongo.host3:27017"], 
last_update_time: -576460750248, last_write_date: %DateTime{calendar: 
Calendar.ISO, day: 26, hour: 6, microsecond: {0, 3}, minute: 47, month: 
7, second: 23, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, 
year: 2017, zone_abbr: "UTC"}, max_wire_version: 5, me: 
"mongo.host3:27027", min_wire_version: 0, op_time: %{"t" => 
2, "ts" => #BSON.Timestamp<6446967716292067329>}, passives: [], 
primary: "mongo.host2:27017", round_trip_time: 240, set_name: 
"rs0", set_version: 4, tag_set: %{}, type: :rs_secondary}}, 30000)

when I use following configuration in worker

{:ok, pid} = Mongo.start_link(database: "yatender", seeds: ["xxx.xxx.xxx.xxx:27017","xxx.xxx.xxx.xxx:27017","xxx.xxx.xxx.xxx:27017"])

this error comes

    [warn]  Logger dropped 999 OTP/SASL messages as it exceeded the amount of 500 messages/second                                                                                    
** (EXIT from #PID<0.1410.0>) exited in: GenServer.call(#PID<0.1415.0>, 
{:server_description, %{address: "xxx.xxx.xxx.xxx:27017", arbiters: [], 
election_id: nil, error: nil, hosts: ["mongo.host1:27017", 
"mongo.host2:27017", "mongo.host3:27027"], 
last_update_time: -576460688733, last_write_date: %DateTime{calendar: 
Calendar.ISO, day: 26, hour: 6, microsecond: {0, 3}, minute: 58, month: 
7, second: 13, std_offset: 0, time_zone: "Etc/UTC", utc_offset: 0, 
year: 2017, zone_abbr: "UTC"}, max_wire_version: 5, me: 
"mongo.host1:27017", min_wire_version: 0, op_time: %{"t" => 2, 
"ts" => #BSON.Timestamp<6446970508020809729>}, passives: [], primary: 
"mongo.host2:27017", round_trip_time: 42, set_name: "rs0", 
set_version: 4, tag_set: %{}, type: :rs_secondary}}, 30000)
** (EXIT) time out

When I just directly connect to a standalone mongodb server which is not a part of replica set it works fine as this.

# Starts an unpooled connection
{:ok, conn} = Mongo.start_link(database: "test",hostname: Application.get_env(:api, :api_env)[:mongo_host])

# Gets an enumerable cursor for the results
cursor = Mongo.find(conn, "test-collection", %{})

cursor
|> Enum.to_list()
|> IO.inspect

So I am stuck here and I don't know what I am missing. Please someone guide me how I can approach this problem, where I am wrong.

Thanks in advance


Solution

  • after so many hit & trials instead of server IP

    {:ok, pid} = Mongo.start_link(database: "yatender", seeds: ["xxx.xxx.xxx.xxx:27017","xxx.xxx.xxx.xxx:27017","xxx.xxx.xxx.xxx:27017"])
    

    we have to give name of hosts

    {:ok, pid} = Mongo.start_link(database: "yatender", seeds: ["mongo.host1:27017","mongo.host2:27017","mongo.host3:27017"])
    

    we can also use poolboy with this.