Connect to a particular Mongo server in rails app

696 views
Skip to first unread message

Gaurav Sahu

unread,
May 25, 2017, 7:59:36 PM5/25/17
to mongodb-user
Hello All,

I am trying to build a monitoring system for my rails app that uses MongoDB for storage. I have three machines for mongo, out of which two are secondary (being the replica set) and one is the primary database. My issue is that I am not able to get data from all of my machines.

Initially I tried connecting to each of my servers using Mongo::Client.new([server_i_address], { database: DB_NAME }) where server_i_address is address of my ith server, but figured out that it is selecting my primary database every time.

Now I am using this command Mongo::Client.new([SERVER_ADDRESSES], { database: DB_NAME, connect: :replica_set}).with(read: { mode: :secondary }). This fetched me data from one of my secondary servers but the other one is left and I am not able to get data from all of the servers.

So Is there a way to make Mongo connection, which doesn't involve any auto-detection internally and that connects to the server it is specified to connect to regardless of whether it is secondary or primary?

Thank you for consideration and I'd appreciate any help from the community.

With Regards,
Gaurav Sahu.

Gaurav Sahu

unread,
May 26, 2017, 3:27:34 AM5/26/17
to mongodb-user
Update

I tried Mongo::Client.new([SERVER_ADDRESS], { database: DB_NAME, connection: :direct })

The client generated by this command is able to fetch data when I place my secondary servers' address at start but fails when I put my primary server's address at the beginning.

Kevin Adistambha

unread,
Jun 1, 2017, 1:14:05 AM6/1/17
to mongodb-user

Hi Gaurav

I tried Mongo::Client.new([SERVER_ADDRESS], { database: DB_NAME, connection: :direct })

I was able to connect to a primary directly using:

client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test', :connect => :direct)

In the console, this shows:

D, [2017-06-01T15:04:05.972907 #63409] DEBUG -- : MONGODB | Topology type 'single' initializing.
D, [2017-06-01T15:04:05.973043 #63409] DEBUG -- : MONGODB | Server 127.0.0.1:27017 initializing.
D, [2017-06-01T15:04:05.975230 #63409] DEBUG -- : MONGODB | Server description for 127.0.0.1:27017 changed from 'unknown' to 'primary'.

Note the line Topology type 'single'. Without the direct connection option, e.g.:

client = Mongo::Client.new(['127.0.0.1:27017'], :database => 'test')

this shows:

D, [2017-06-01T15:04:37.617541 #63417] DEBUG -- : MONGODB | Topology type 'unknown' initializing.
D, [2017-06-01T15:04:37.617698 #63417] DEBUG -- : MONGODB | Server 127.0.0.1:27017 initializing.
D, [2017-06-01T15:04:37.620555 #63417] DEBUG -- : MONGODB | Server description for 127.0.0.1:27017 changed from 'unknown' to 'primary'.
...
D, [2017-06-01T15:04:37.812831 #63417] DEBUG -- : MONGODB | Topology type 'unknown' changed to type 'replica set'.

Note the line Topology type 'unknown' changed to type 'replica set'. above, which indicated that the driver discovered a replica set and acted accordingly. I tested the above code using the Ruby MongoDB driver version 2.4.2 and MongoDB 3.4.4. The connection code is the one available in the Using Mongo::Client section in the Ruby driver manual

If you’re still having issues, could you post:

  • Your MongoDB version and your Ruby driver version
  • Code snippet that you tried
  • Any errors and relevant information in both Ruby logs and the mongod logs

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages