No matter what I do I cannot seem to get Ruby's Mongo driver to connect to a Replica Set. I've dug through all the tickets and SO questions on the subject and tried every combination of syntaxes. Ultimately, though, I've ended up just trying to use the driver API in the Rails console like so:
Mongo::ReplSetConnection.new(['db1.yada.to:27017', 'db2.yada.to:27017'], :read => :primary).db("yada_production")
But no matter what it always spits back, instantly:
Mongo::ConnectionFailure: Failed to connect to primary node.
When I log into the db servers and access mongo from there I see db1 is PRIMARY> while db2 is SECONDARY>
Additionally I can telnet over to the db servers from the application server on port 27017 just fine. Switching the connection back to a single server with Connection.new works without issue.
I am going insane. I originally was using Mongo ruby driver 1.4 + Rails 3.1 + Mongoid 2.3 + MongoDB 2.0.6 but am now upgraded to 1.6.2 + Rails 3.2 + 2.4.12 + 2.0.6 with the same issue.
I was originally using it with auth but I have turned auth off for now to simplify things.
Is this just totally broken? It seems like other people have had similar issues but none of the 'solutions' work for me. Anyone else out there have ideas?
I figured out my problem. My mongo replica set looks like this:
PRIMARY> rs.status()
{
"set" : "yada",
"date" : ISODate("2012-07-30T06:30:23Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "YADAdb1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"optime" : {
"t" : 1343627448000,
"i" : 2
},
"optimeDate" : ISODate("2012-07-30T05:50:48Z"),
"self" : true
},
{
"_id" : 1,
"name" : "db2.yada.to:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 16022,
"optime" : {
"t" : 1343627448000,
"i" : 2
},
"optimeDate" : ISODate("2012-07-30T05:50:48Z"),
"lastHeartbeat" : ISODate("2012-07-30T06:30:22Z"),
"pingMs" : 0
}
],
"ok" : 1
}
Notice that even though the FQDN of db1 is db1.yada.com, the node is initialized with its local hostname. It appears that you must use the exact name provided by rs.status() or connection will fail. This means if it isn't resolvable with DNS, you need to add it to your /etc/hosts on your connecting server. Once I did that the connection worked.
There was a response to this effect in the mongoid bugs, which I read, but I didn't fully understand what they were implying in the dialog. I assumed they only needed to modify /etc/hosts because there was no public DNS entry for the server. Now it makes sense that the exact name in Mongo is mandatory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With