【问题标题】:Mongoose connection error when using db name in connection stringMongoose connection error when using db name in connection string
【发布时间】:2022-10-05 15:59:52
【问题描述】:

当我尝试使用此连接字符串连接到 Mongo 实例时

mongodb://root:password@localhost:27017/

但是,当我尝试在连接字符串中指定数据库名称时,一切正常

IE mongodb://root:password@localhost:27017/storefont

我收到以下错误MongoDB 连接错误:MongoNetworkError:首次连接时无法连接到服务器 [localhost:27017] [MongoError:身份验证失败。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    像这样指定身份验证数据库:

    mongodb://root:password@localhost:27017/storefont?authSource=admin
    

    如果您指定一个数据库,那么默认情况下也会使用该数据库进行身份验证。 MongoDB 文档在该主题中并非 100% 清楚。

    另见Authentication failure while trying to save to mongodb

    【讨论】:

    • 我错过了“?authSource = admin”,谷歌搜索了几个小时。 :D 发送了向上箭头。
    【解决方案2】:

    Wernfield 提供的答案是正确的。我提供了一个小调整以及一个连接到副本集的示例:

    使用 authSource 查询参数时,请确保连接字符串用单引号括起来,或者只使用 --authenticationDatabase 参数,

    mongo mongodb://ip1:27017,ip2:27017,ip3:27017/my-db --authenticationDatabase admin -u myUsername -p myPassword
    

    【讨论】:

      猜你喜欢
      • 2018-12-11
      • 2013-05-29
      • 2020-12-11
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2019-06-11
      相关资源
      最近更新 更多