无法连接到mongolab,获取MongoError:auth失败

问题描述 投票:27回答:9

我最近在mongoLab中创建了一个帐户。当我尝试使用以下语句连接到数据库时。

var mongoose = require('mongoose');
mongoose.connect('mongodb://mk:[email protected]:47742/mkdb');

我总是收到以下错误

MongoError: auth failed
at Function.MongoError.create (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/error.js:31:11)
at /Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:793:66
at Callbacks.emit (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:94:3)
at null.messageHandler (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/topologies/server.js:235:23)
at Socket.<anonymous> (/Users/a042292/Desktop/start/node_modules/mongoose/node_modules/mongodb/node_modules/mongodb-core/lib/connection/connection.js:259:22)
at Socket.emit (events.js:107:17)
at readableAddChunk (_stream_readable.js:163:16)
at Socket.Readable.push (_stream_readable.js:126:10)
at TCP.onread (net.js:538:20)
node.js mongodb mlab
9个回答
41
投票

确保您使用的数据库usernamepassword不是来自Mlab的帐户usernamepassword

在MLab,以前的MongoLab,做以下事情

  1. 导航到Users
  2. Add Database User
  3. 选择您的用户名和密码

现在,您可以使用mongo ds061374.mlab.com:61374/yourdb -u <dbuser> -p <dbpassword>在shell上测试它


24
投票

Mongolab将其2.6.x数据库升级到3.0.x.不幸的是,mongo3具有不同的身份验证机制,因此旧客户端不兼容。

Mongoose正在使用本机mongo驱动程序,因此您必须升级它。这通常通过升级本地mongo安装来完成。

对于使用mongojs的用户,升级到最新版本并在连接时在options对象中添加authMechanism:'ScramSHA1'参数:

db = mongojs('mongodb://username:[email protected]:32132/mydb', ["mycollection"], {authMechanism: 'ScramSHA1'});

9
投票

对我来说,解决方案是:

$ npm install --save --save-exact [email protected]

根据:Heroku app crashes after MongoDB updated to 3.0


5
投票

在这里我们必须知道mLab用户名和密码也不是我们数据库的用户名和密码...我们必须检查我们是否使用了正确的用户名和密码来连接String

我们可以在这里创建数据库用户帐户---- >>

我的连接const如下

const db ="mongodb://<My database username>:<my database password>.mlab.com:39648/videoplayer"

1
投票

1-确保数据库已启动并正在运行。 2-不要忘记创建db用户以获取访问凭据。

希望能帮到你!


1
投票

只需将?authSource = yourDB&w = 1添加到db url的末尾

mongoose.connect('mongodb:// user:password @ host / yourDB?authSource = yourDB&w = 1')这对我有用。 &w = 1很重要

e.g

?MONGO_URI =“的mongodb://卡恩:[email protected]:13402 /电子商务AuthSource =电子商务&W = 1“;

https://github.com/Automattic/mongoose/issues/4587

这救了我的命


0
投票

我在使用较旧版本的mongoose(版本3.8.10)时收到此错误。升级到最新版本(版本5.0.10)后,错误消失,并建立了连接。

只需运行npm install [email protected] --save ....但是用最新版本替换版本,


0
投票

确保使用正确的db用户名和密码。

如果您尝试通过代码连接到db,并且您的用户名和密码包含任何特殊字符,如“@”,“$”等,请确保使用encodeURIComponent()函数对URI进行编码

例如:“localhost:// pooja:”+ encodeURIComponent('pooja @ 123')+“/ trymynewdb”,然后使用enocded uri连接到db。


0
投票

如果您的密码有特殊字符,最好检查此处出现的特殊字符的网址编码值:url encoding list

但我强烈建议您在尝试连接之前先验证是否先发送了数据。验证它的一种方法是console.log发送的数据。例:

console.log(process.env.MONGO_ATLAS_PW);
© www.soinside.com 2019 - 2024. All rights reserved.