简体   繁体   English

如何使用具有凭据的红宝石连接到mongo?

[英]How to connect to mongo, using ruby that have credentials?

I am trying to connect to a database that has credentials. 我正在尝试连接到具有凭据的数据库。 I cannot find any useful information online... 我在网上找不到任何有用的信息...

require: 'mongo'

begin
   db = Mongo::Connection.new(" IP ADDRESS " , PORT ).db("COLLECTION")
   db.authenticate("username","password")


rescue StandardError => err
    abort("error")
end

C:/Ruby193/lib/ruby/gems/1.9.1/gems/mongo-1.8.2/lib/mongo/networking.rb:306:in `rescue in receive_message_on_socket': Operation failed with the following exception: end of file reached (Mongo::ConnectionFailure) C:/Ruby193/lib/ruby/gems/1.9.1/gems/mongo-1.8.2/lib/mongo/networking.rb:306:在receive_message_on_socket中的'rescue'中:操作失败,但以下异常:文件结尾已达到(Mongo :: ConnectionFailure)

looks like there is an #add_auth method as well as auths can be passed to the constructor maybe try 貌似有一个#add_auth方法以及auths可以传递给构造也许尝试

auths = [{"db_name" => "COLLECTION", 
          "username" => YOUR_USERNAME, 
          "password" => YOUR_PASSWORD}]
Mongo::Connection.new(" IP ADDRESS " , PORT, auths: auths)

OR 要么

auth = {"db_name" => "COLLECTION", 
          "username" => YOUR_USERNAME, 
          "password" => YOUR_PASSWORD}
Mongo::Connection.new(" IP ADDRESS " , PORT).add_auth(auth)

and see if that works 看看是否可行

Reference Mongo::MongoClient::GENERIC_OPTS and Mongo::MongoClient#setup 参考Mongo :: MongoClient :: GENERIC_OPTSMongo :: MongoClient#setup

BTW that is a old version of the gem and ruby for that matter. 顺便说一句,这是宝石和红宝石的旧版本。 have you considered the possibly upgrading? 您是否考虑过可能的升级?

Newest version (as of now) of Mongo is 2.4.3 and the options are more transparent now eg Mongo的最新版本(到目前为止)是2.4.3,并且选项现在更加透明,例如

Mongo::Client.new("IP_ADDRESS:PORT", user: USERNAME, password: PASSWORD, auth_mech: AUTHENTICATION_MECHANISM)

Although based on your comments I am not sure authentication is your issue 尽管根据您的评论,我不确定身份验证是否是您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM