简体   繁体   中英

Failed to authenticate credentials using Mongolite R

I use R and trying to use the very recent "Mongolite". However I cannot connect to MongoDB server. The manual clearly states the following:

mongo(collection = "test", db = "test", url = "mongodb://localhost")

This is what I have tried without success, where I have a log token and the port of course.

mongodb://heroku:TOKEN@lennon.mongohq.com:PORT 

and keep getting the following error:

Error in mongo_collection_new(url, db, collection) : 
  Failed to authenticate credentials.

It is tried and true that mongolite (v0.7 as of today) supports authenticated connection to a remote 3.2.x MongoDB (as opposed to localhost ).

The example below jeroenooms provides himself worked:

library(mongolite)
mongo(collection = "mtcars", url = "mongodb://readwrite:test@ds043942.mongolab.com:43942/jeroen_test")

To explain:

  • mtcars is the name of the MongoDB "collection" (or "table" if you insist). It could be a name that does not exist yet.
  • readwrite is the user name of your mongodb
  • test is the password of the user "readwrite"
  • ds043942.mongolab.com is the remote host, which could be replaced by ip address, ie 23.20.234.21
  • 43942 is the port number. By default in MongoDB, it is 27017
  • jeroen_test is the name of the database instance in use, which has to exist already

Running the commands above in R will print:

Mongo Message: SCRAM: authenticating "readwrite" (step 1)

Mongo Message: SCRAM: authenticating "readwrite" (step 2)

Mongo Message: SCRAM: authenticating "readwrite" (step 3)

Mongo Message: SCRAM: "readwrite" authenticated

...


I tried to replace it with my own remote host info. It didn't work in the beginning, which turned to be the user role issue rather than mongolite issue. After ensuring a user with correct readWrite roles, I connected to my remote 3.2.x MongoDB via mongolite package.

The following 2 sources were of great help to me in setting up users in MongoDB:

If you look into the source code of jeroenooms/mongolite you can see that it doesn't support authentication yet:

https://github.com/jeroenooms/mongolite/blob/master/R/mongo.R

mongo_collection_new <- function(uri = "mongodb://localhost", db = "test", collection = "test"){
 stopifnot(is.character(uri))
 stopifnot(is.character(db))
 stopifnot(is.character(collection))
 .Call(R_mongo_collection_new, uri, db, collection)
}

just change the order :

mongo( url = "mongodb://localhost",db = "test",collection = "test")

I found that the following command allowed me to connect to mongodb using mongolite and the legacy SCRAM-SHA-1 authentication mode.

library(mongolite)
mongoUrl <- "mongodb://a_username:a_password@localhost:27017/admin" #<-admin here is the mongodb database that stores the authentication info

# specify your collection
colname <- "a_collection"

# specify your database
dbname <- "a_database"

# create connection (con)
con <- mongo(collection = colname, url = mongoUrl, db=dbname)

# count how many records (fyi this is just a test)
con$count('{}')

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