无法使用 Mongolite R 验证凭据

Failed to authenticate credentials using Mongolite R

我使用 R 并尝试使用最近的 "Mongolite"。但是我无法连接到 MongoDB 服务器。 手册明确指出以下内容:

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

这是我尝试过但没有成功的方法,当然我有一个日志令牌和端口。

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

并不断收到以下错误:

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

如果您查看 jeroenooms/mongolite 的源代码,您会发现它还不支持身份验证:

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)
}

mongolite(截至今天的 v0.7)支持与 远程 3.2.x MongoDB(相对于 localhost)。

jeroenooms 提供的以下示例已成功运行:

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

说明:

  • mtcars 是 MongoDB“集合”的名称(如果您坚持的话,也可以是“table”)。它可能是一个尚不存在的名称。
  • readwrite是你的mongodb
  • 的用户名
  • test是用户“readwrite”的密码
  • ds043942.mongolab.com是远程主机,可以用ip地址代替,即23.20.234.21
  • 43942 是端口号。在MongoDB中默认为27017
  • jeroen_test是正在使用的数据库实例的名称,必须已经存在

运行 以上 R 中的命令将打印:

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

...


我尝试用我自己的远程主机信息替换它。一开始不行,后来变成了用户角色的问题,而不是mongolite的问题。在确保用户具有正确的读写角色后,我通过 mongolite 包连接到我的远程 3.2.x MongoDB。

以下 2 个资源对我在 MongoDB 中设置用户有很大帮助:

只需更改顺序即可:

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

我发现以下命令允许我使用 mongolite 和旧版 SCRAM-SHA-1 身份验证模式连接到 mongodb。

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('{}')