我創建了一個mongo副本集,我正嘗試使用它來連接elixir。我在副本集中創建了3個mongo實例,並在/etc/hosts
中給出了主機名,所以事情是在mongo終端中一切工作正常,並且能夠正確連接mongo副本集。 我在NODE.JS
中編寫代碼以使用mongodb
庫從副本集中獲取數據,並且它也正常工作(這意味着在mongo服務器中的配置以及我的本地服務器的狀態正如我所知的那樣),但是當我嘗試連接它時當我連接到該副本集時它會拋出錯誤。如何連接到elixir中的mongo副本集羣
我正在使用下面的庫來連接它。
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)
,當我使用下面的工作人員配置
{: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
所以我被困在這裏,我不知道我在想什麼。請有人指導我如何解決這個問題,我錯了。
在此先感謝
我想使用種子是推薦的方法,但它不是唯一的方法。也許你應該嘗試連接到該副本集,就像連接到單個節點一樣? (只是爲了確保它能正常工作) – Haito
您確定每個節點都已啓動嗎? https://github.com/ankhers/mongodb/issues/144你認爲這個問題可能有關係嗎? – Haito
一切都很好,我能夠在node.js中完美地完成所有任務,但我是新的靈丹妙藥,所以我被困在這裏。 –