如何在不使用主节点的情况下仅连接到ruby中的次要mongo节点。我必须进行大量读取才能检索一些数据。但是我不想在小学做,因为会有很多读物。
这就是我尝试过的:
require 'rubygems' # not necessary for Ruby 1.9
require 'mongo'
def dbm()
client = Mongo::Client.new(['mongodb://xx.xx.x.xxx:27017'], :database => 'mydb', read: { mode: :secondary})
collection = client[:info]
doc = collection.find({identityId: "e5c3f71c-21e0-415a-8a97-b6d8929c2af6"}).first
puts doc
end
dbm
所以当我这样运行时:
ruby script.rb
我收到此错误:
D,[2019-05-16T11:46:02.902111#58849]调试-:MONGODB |在mongodb:0上运行ismaster时出错:getaddrinfo:提供了节点名或服务名,或者未知
但是当我试图像这样创建mongo客户端时:
Mongo::Client.new('mongodb://xx.xx.x.xxx:27017/mydb')
我得到了这个日志,但是我可以获取数据了:
D, [2019-05-16T12:28:57.374829 #60137] DEBUG -- : MONGODB | EVENT: #<TopologyOpening topology=Unknown[]>
D, [2019-05-16T12:28:57.375289 #60137] DEBUG -- : MONGODB | Topology type 'unknown' initializing.
D, [2019-05-16T12:28:57.377073 #60137] DEBUG -- : MONGODB | EVENT: #<TopologyChanged prev=Unknown[] new=Unknown[xx.xx.x.xxx:27017]>
D, [2019-05-16T12:28:57.377503 #60137] DEBUG -- : MONGODB | Topology type 'Unknown' changed to type 'Unknown'.
D, [2019-05-16T12:28:57.377880 #60137] DEBUG -- : MONGODB | EVENT: #<ServerOpening address=xx.xx.x.xxx:27017 topology=Unknown[xx.xx.x.xxx:27017]>
D, [2019-05-16T12:28:57.378311 #60137] DEBUG -- : MONGODB | Server xx.xx.x.xxx:27017 initializing.
D, [2019-05-16T12:28:58.073676 #60137] DEBUG -- : MONGODB | EVENT: #<ServerDescriptionChanged address=xx.xx.x.xxx:27017 topology=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,name=some_name] prev=#<Mongo::Server:Description:0x70261608870540 ....
D, [2019-05-16T12:28:58.075678 #60137] DEBUG -- : MONGODB | Server description for xx.xx.x.xxx:27017 changed from 'unknown' to 'secondary'.
D, [2019-05-16T12:28:58.082393 #60137] DEBUG -- : MONGODB | EVENT: #<TopologyChanged prev=Unknown[xx.xx.x.xxx:27017] new=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name]>
D, [2019-05-16T12:28:58.082723 #60137] DEBUG -- : MONGODB | Topology type 'Unknown' changed to type 'ReplicaSetNoPrimary'.
D, [2019-05-16T12:28:58.083183 #60137] DEBUG -- : MONGODB | EVENT: #<ServerOpening address=pmongo1.myorg.io:27017 topology=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name]>
D, [2019-05-16T12:28:58.083491 #60137] DEBUG -- : MONGODB | Server pmongo1.myorg.io:27017 initializing.
D, [2019-05-16T12:28:58.084471 #60137] DEBUG -- : MONGODB | EVENT: #<ServerOpening address=pmongo2.myorg.io:27017 topology=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name]>
D, [2019-05-16T12:28:58.084824 #60137] DEBUG -- : MONGODB | Server pmongo2.myorg.io:27017 initializing.
D, [2019-05-16T12:28:58.085464 #60137] DEBUG -- : MONGODB | EVENT: #<ServerOpening address=parbiter1.myorg.io:27017 topology=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name]>
D, [2019-05-16T12:28:58.086113 #60137] DEBUG -- : MONGODB | Server parbiter1.myorg.io:27017 initializing.
W, [2019-05-16T12:28:58.086649 #60137] WARN -- : MONGODB | Removing server xx.xx.x.xxx:27017 because it reported itself as pmongo2.myorg.io:27017
D, [2019-05-16T12:28:58.807027 #60137] DEBUG -- : MONGODB | EVENT: #<ServerDescriptionChanged address=pmongo2.myorg.io:27017 topology=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name] ...>>
D, [2019-05-16T12:28:58.808115 #60137] DEBUG -- : MONGODB | Server description for pmongo2.myorg.io:27017 changed from 'unknown' to 'secondary'.
D, [2019-05-16T12:28:58.810403 #60137] DEBUG -- : MONGODB | EVENT: #<TopologyChanged prev=ReplicaSetNoPrimary[xx.xx.x.xxx:27017,pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name] new=ReplicaSetNoPrimary[pmongo1.myorg.io:27017,pmongo2.myorg.io:27017,parbiter1.myorg.io:27017,name=some_name]>
D, [2019-05-16T12:28:58.810826 #60137] DEBUG -- : MONGODB | Topology type 'ReplicaSetNoPrimary' changed to type 'ReplicaSetNoPrimary'.
在第二种方法中,我不确定是否执行正确或以某种方式弄乱了其配置。
有人可以帮我解决这个问题吗?