Mongo Ruby Driver cannot connect to Replica Set Primary - ruby-on-rails

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.

Related

write_points() Python not writing data for InfluxDB

I'm doing the basic setup in python to pass data to an InfluxDB server I have running on a RaspberryPi. My issue is the write_points() function does not write ANY data to InfluxDB even though I am using the simplest possible measurement and field-set entry as a test:
from influxdb import InfluxDBClient
from influxdb_config import HOST, PORT, USERNAME, PASSWORD, DATABASE
from data_poll import quotes_response
import pprint
influxdbClient = InfluxDBClient(
host = HOST,
port = PORT,
username = USERNAME,
password = PASSWORD,
database = 'example'
)
data = [
{
"measurement": "stock price",
"fields": {
"price": 0.64,
"volume": 120
}
}
]
pprint.pprint(influxdbClient.ping())
pprint.pprint(influxdbClient.get_list_database())
influxdbClient.switch_database('example')
pprint.pprint(influxdbClient.write_points(data))
pprint.pprint(influxdbClient.query('SELECT * FROM example'))
I am able to communicate with the server via Python and, if I create values manually on the server, retrieve them in the same script. Below is a snippet of the terminal output that matches some of the requests in the above code snippet.
'1.8.4'
[{'name': '_internal'}, {'name': 'jsonAAPLDataTest'}, {'name': 'example'}]
True
ResultSet({})
Update 2021/03/14 - I'm currently using Python 3.9.2, but had the exact same issue utilizing 3.7.3 (tested by the API developers). My next attempt is to downgrade my InfluxDB instance from v1.8.4 to v1.7.4 to see if this, by chance, resolves the issue.
I was able to now right data to my InfluxDB v1.8.4 database using the proper API github.com/influxdata/influxdb-client-python. Prior to this I was utilizing the prior release API which apparentely must have a difference in the underlying functionality of writing to the database. FIgured I would at least follow up and share the information so others would know in case they encoutner this issue.

Requested database is not available. Requested database name: 'graph.db'

am getting this error message
{
"code": "Neo.TransientError.Database.DatabaseUnavailable",
"message": "Requested database is not available. Requested database name: 'graph.db'."
}
while sending a request through rest API, with this statement
{
"statements" : [ {
"statement" : "CREATE (n) RETURN id(n)"
} ]
}
I'm guessing you probably don't have a database called graph.db. That is the name of the file system directory, not the database. Unless you've set up a database yourself, use neo4j, which is the default.
As Nigel said, it'll be because you don't have a graph by that name.
I ran into this issue recently, after upgrading from 3.5.3 to 4.1, and having to figure out some new behaviours (why yes, I have been living under a rock).
Read through the server's logs - it reports the database name while it's starting up. If you're using a Docker instance, as I am, docker logs <instance-id> is your friend.

Get Client Machine Name in Ruby on Rails

How do I get the client (not the server) machine name in Rails from a controller? I've tried Socket.gethostnamebut that just get's the name of the server the Rails app is running on. Resolv.getname(request.remote_ip) just gives back an error saying there is no name for the client's IP address. I'm on Rails v4 if that matters.
I had the same problem using Resolv, I ended up doing this, as it is not always possible to retrieve the name of a machine from an ip address:
dns_lookup = ""
begin
dns_lookup = Resolv.getname(request.remote_ip)
rescue Resolv::ResolvError => e
end

Unable to authenticate mongodb remotely

I am unable to authenticate mongodb remotely. I'm running MongoDB 3.0.0 provided by the DigitalOcean One-Click installer, and I can't seem to set up authentication properly.
I have 1 database called wbio_production. I followed mongodb docs tutorials as best as I could. When I execute:
> db.auth("siteUserAdmin","MYPASSWORD")
1
> db.getUsers()
The output of that is:
{
"_id" : "admin.siteUserAdmin",
"user" : "siteUserAdmin",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
And when I switch to the wbio_production database and perform the same getUsers():
> use wbio_production
switched to db wbio_production
> db.getUsers()
[
{
"_id" : "wbio_production.herokuUser",
"user" : "herokuUser",
"db" : "wbio_production",
"roles" : [
{
"role" : "userAdmin",
"db" : "wbio_production"
},
{
"role" : "readWrite",
"db" : "wbio_production"
}
]
}
]
When I fill out the connection form in Robomongo however, it cannot successfully connect to the database. Authorization always fails, however it does successfully connect to the database.
I have tried the following combinations of using the users above:
DB: Admin, User: siteUserAdmin
DB: Admin, User: herokuUser
DB: wbio_production, User: siteUserAdmin
DB: wbio_production, User: herokuUser
All of them wind up with some output coming back in the heroku logs similar:
failed with error 13: "not authorized for query on
wbio_production.mongoid_forums_forums"
I have tried reinstalling mongodb, recreating users based off different tutorials, and just trying different peoples explanations. I've spent about 5 hours now looking for a solution to my issue, and now I must turn to SO for your advice. I'm sure this is a simple error on my part that I am not understanding from the docs or something I am continuously missing in the set up.
What must I do to get a secured and functional MongoDB server? Thanks!
You need to configure mongod with its binding so clients can connect remotely.
bind_ip = 0.0.0.0
Robomongo authentication does not work with MongoDB 3+. I recommend switching over to MongoChef. http://3t.io/mongochef/. I highly recommend it, as it contains MANY more capabilities than Robomongo does.
Another thing to keep in the back of your mind...
If you are also using Mongoid, it is currently undergoing a large rewrite. Mongoid 5 will drop Moped https://www.mongodb.com/blog/post/announcing-ruby-driver-20-rewrite.
Take a look at my other answer on what to do if you are using Mongoid 4 with MongoDB 3+. Rails Mongoid fails to authenticate - failed with error 13: "not authorized for query on my_db.my_collection"

error 16550 : "not authorized for query on " mongolab

I have a Rails application running on heroku and i am connecting at two dbs hosted in mongolab (X and Y).
I have configured two heroku env variables containing the connection strings.
When i query on Y all works fine but when i query on X db it gives me the error 16550 : "not authorized for query on X.table".
I have setted up correctly both env variables for these connections and also have a valid user to access X db.
If i connect with the shell all works fine.
How can i solve this?
Here is the error message in rails:
{"status":"500",
"error":"The operation: #<Moped::Protocol::Query\n #length=88\n #request_id=4\n #response_to=0\n
#op_code=2004\n #flags=[:slave_ok]\n
#full_collection_name=\"X.table\"\n
#skip=0\n #limit=0\n
#selector={\"_id\"=>\"5252c92521e4af681a000002\"}\n
#fields=nil>\n
failed with error 16550: \"not authorized for query on X.table\"\n\n
See https://github.com/mongodb/mongo/blob/master/docs/errors.md\nfor details about this error."}
I solved this, if someone comes here with the same problem : look at your table model, if as in my case it is "stored_in" another database you must specify there the session of the the uri = evn variables on datbase.yml

Resources