Mongo :: OperationFailure - 使用from_uri时需要登录 [英] Mongo::OperationFailure - need to login when using from_uri

查看:103
本文介绍了Mongo :: OperationFailure - 使用from_uri时需要登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是与我的heroku / mongolab数据库连接,但我一直得到这个错误:

My goal is to connect with my heroku/mongolab database but I keep getting this error:

Mongo::OperationFailure at /mongotest/a/b
: need to login
file: networking.rb
location: send_message_with_gle
line: 89

我使用的代码是:

The code I'm using is:

client = Mongo::MongoClient.from_uri(ENV['MONGOLAB_URI'])
db   = client.db('test')
testcoll = db['testcoll']
testcoll.insert({:'_id' => "def", :'test' => "woop de doop"})
testcoll.find()

ENV ['MONGOLAB_URI'] = mongodb:// heroku_app ########:password@ds0xxxxx.mongolab.com:xxxxx / heroku_app ########

我知道uri是正确的并且包含用户名和密码,所以为什么会出现错误?此外,该错误发生在insert()行,而不是我认证的行。

I know that the uri is correct and contains the username and password, so why the error? Also, the error occurs on the insert() line, not the line where I authenticate.

推荐答案

Welp,结果是url将我连接到 heroku_app ######## 数据库,但是我试图访问名为 test 显然我没有通过身份验证。本来会很好的Mongo已经返回了一个错误,说明我已经登录了,但没有登录到正确的数据库。哦,很好。

Welp, turns out the url connects me to the heroku_app######## database, but I'm then trying to access the database called test so obviously I'm not authenticated. Would have been nice Mongo had returned an error specifying that I had logged in but not to the right database. Oh well.

我没有足够关注uri的格式,它是 $ b

I hadn't paid enough attention to the format of the uri, which is

mongodb://username:password@host:port/database

数据库部分是......非常重要,事实证明。

The database part is... pretty important, it turns out.

(我在写测试时实际上找到了答案,但如果这个答案存在,它可能会为我节省了大量的时间,所以我再次写下它并自己回答。)

(I actually found the answer to this while writing the test, but if this answer had existed it might have saved me an embarrassingly large amount of time, so I'm writing it again and answering it myself.)

这篇关于Mongo :: OperationFailure - 使用from_uri时需要登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆