简体   繁体   中英

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)

looks like there is an #add_auth method as well as auths can be passed to the constructor maybe try

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

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::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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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