【问题标题】:How to connect to mongo replica cluster in elixir如何在 elixir 中连接到 mongo 副本集群
【发布时间】:2017-07-26 07:11:41
【问题描述】:

我创建了一个 mongo 副本集,我试图从它与 elixir 连接。我在副本集中创建了 3 个 mongo 实例,并在 /etc/hosts 中提供了主机名,所以事情是在 mongo 终端中一切正常,我能够与 mongo 副本正确连接。 我在NODE.JS 中编写了代码以使用mongodb 库从副本集中获取数据,它也工作正常(这意味着我知道mongo 服务器和我的本地服务器中的配置很好),但是当我试图通过 elixir 连接它,当我连接到该副本集时它会引发错误。

我正在使用以下库来连接它。

https://github.com/ankhers/mongodb

图书馆作者建议使用以下配置

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

这是抛出语法错误。

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

这很明显,因为它是错误的。

当我使用这个配置时

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]])

注意:我已将主机地址更改为 xxx,请不要混淆。

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)

当我在 worker 中使用以下配置时

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

这个错误来了

    [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

当我直接连接到一个独立的 mongodb 服务器时,它不是 作为副本集的一部分,它可以正常工作。

# 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

所以我被困在这里,我不知道我错过了什么。请有人指导我如何解决这个问题,我错了。

提前致谢

【问题讨论】:

  • 我想使用种子是推荐的方式,但这不是唯一的方式。也许您应该尝试连接到该副本集,就好像您连接到单个节点一样? (只是为了确保它正常工作)
  • 你确定每个节点都启动了吗? github.com/ankhers/mongodb/issues/144你认为这个问题可能相关吗?
  • 一切都很好,我可以在 node.js 中完美地完成所有事情,但我是 elixir 的新手,所以我被困在这里。
  • 这个驱动程序可能存在副本集问题。也许你应该问作者本人?在 github 上发布问题是个好主意。
  • 肯定会的 :) 非常感谢您的帮助

标签: mongodb erlang elixir replicaset


【解决方案1】:

经过这么多的打击和考验 而不是服务器IP

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

我们必须提供主机名称

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

我们也可以使用 poolboy。

【讨论】:

  • 这听起来不对。您应该能够使用 ip:port 对的种子列表连接到您的集群。我只能在我自己的集群上做到这一点。 免责声明:我是当前的mongodb 包维护者。
猜你喜欢
  • 2020-10-15
  • 2015-09-26
  • 2013-03-30
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2016-12-09
  • 2020-07-14
  • 1970-01-01
相关资源
最近更新 更多