【问题标题】:Authentication Error with mongolitemongolite 的身份验证错误
【发布时间】:2021-01-16 02:00:35
【问题描述】:

为了将数据保存到 mongodb(使用 CRAN 的 mongolite 包),我定义了以下函数:

  saveData <- function(data, collection) {
    # Connect to the database
    db <- mongo(collection = collection,
                url = paste0(
                  "mongodb://",
                  options()$mongodb$username, ":",
                  options()$mongodb$password, "@",
                  options()$mongodb$host, "/",
                  databaseName))
    # Insert the data into the mongo collection as a data.frame
    data <- as.data.frame(t(data))
    db$insert(data)
  }

我的数据存储在一个如下所示的矩阵中:

> mat.answers
  var1 var2 var3 var4 var5
1    90       60          70      60      50
2    85       65          75      55      50
3    80       70          75      70      25
4    75       70          80      80      55
5    80       66          80      75      55

但是,当我使用命令保存一些数据时,我收到以下错误:

saveData(df.answers,"SST")
Mongo Message: SCRAM: authenticating "dynamo" (step 1)
Error: Authentication failed.
Called from: mongo_collection_command_simple(col, "{\"ping\":1}")
Browse[1]> 

我在 mongodb 配置文件中生成了一个用户并启用了身份验证。从mongo shell,我可以使用这些凭据正常登录。它既不适用于本地数据库,也不能连接到 mongolab 提供的远程数据库。

【问题讨论】:

  • paste0( "mongodb://", options()$mongodb$username, ":", options()$mongodb$password, "@", options()$mongodb$host, "/", databaseName) 是否为您提供了有效的 URL?
  • 你有没有想过这个问题?我遇到问题 b/c 我的身份验证数据库 admin 不是我的数据所在的数据库。但根据文档,我必须将 auth 数据库放在 uri 的末尾

标签: r mongodb


【解决方案1】:

我发现以下命令允许我使用 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('{}')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2019-01-03
    • 2012-05-16
    • 2011-02-16
    • 2013-11-28
    • 2017-11-14
    • 2017-06-18
    相关资源
    最近更新 更多