mongoose - 使用 SCRAM-SHA-1 在 Mongoose 中进行身份验证

标签 mongoose

我最近从 MongoDB 2.6 升级到 3.0.4,并将 Mongoose 版本升级到 4.0.0。

现在当我尝试使用 mongoose 连接到 mongo 时:

mongoose.connect('mongodb://user:password@host:port/dbname')

在 mongo 日志上我收到此消息

SCRAM-SHA-1 authentication failed for user on dbname from client xxx.xxx.xxx.xxx

我检查了mongodb,该用户存在于管理员中。我使用的命令是 db.system.users.findOne({user:'user'})

该语句返回的信息包含SCRAM-SHA-1信息。

我的问题是如何在建立连接时在mongoose中指定SCRAM相关信息。我读了很多文章,但没能理解它是如何完成的

最佳答案

找到了解决方案,我没有传递authDatabase名称,这就是连接失败的原因。早些时候我用过这个

mongoose.connect('mongodb://user:password@host:port/dbname')

现在我用这个

mongoose.connect('mongodb://user:password@host:port/dbname?authSource=dbWithUserCredentials')

Discussion thread上找到了这个解决方案Mongoose 本身

编辑:

不要忘记将 dbWithUserCredentials 替换为您自己的。在大多数情况下 dbWithUserCredentials 将是管理员。所有登录凭据(例如用户名、密码)已在传递给 mongoose.connect() 的参数中指定。

关于mongoose - 使用 SCRAM-SHA-1 在 Mongoose 中进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31398367/

相关文章:

node.js - 从 docker 容器连接到本地 mongodb

node.js - 如何在具有 mongoose 数据库的nodejs中存储最新图像

node.js:如何在我的用户模型上编写 "find or create"静态方法?

javascript - 从 Mongoose 的多个集合中获取数据?

mongodb - 运行 app.js 时 Nodejs-mongoose-0.0.5 错误

node.js - 在 mongoose 中查找相对于我的坐标在 10 英里半径内的附近用户

javascript - 如何制作像 "router.get('/region/:id')"but with a limited range for the id? 这样的获取路线

node.js - Mongoose 的全文搜索不起作用

node.js - Mongoose 查询 where

node.js - Mongoose 只返回文档的 id