我最近从 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/