我有一个空的环回应用程序,在那里我添加了一个模型和一个mongodb数据源。
连接时,我得到以下错误:
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer
Connection fails: { [MongoError: Authentication failed.]
name: 'MongoError',
message: 'Authentication failed.',
ok: 0,
code: 18,
errmsg: 'Authentication failed.' }
It will be retried for the next request.
/media/[...]/node_modules/mongodb/lib/mongo_client.js:454
throw err
^
MongoError: Authentication failed.
at Function.MongoError.create (/media/[...]/node_modules/mongodb-core/lib/error.js:31:11)
at /media/[...]/node_modules/mongodb-core/lib/topologies/server.js:778:66
at Callbacks.emit (/media/[...]/node_modules/mongodb-core/lib/topologies/server.js:95:3)
at null.messageHandler (/media/[...]/node_modules/mongodb-core/lib/topologies/server.js:249:23)
at Socket.<anonymous> (/media/[...]/node_modules/mongodb-core/lib/connection/connection.js:265:22)
at emitOne (events.js:77:13)
at Socket.emit (events.js:169:7)
at readableAddChunk (_stream_readable.js:153:18)
at Socket.Readable.push (_stream_readable.js:111:10)
at TCP.onread (net.js:531:20)
数据库在docker中运行。我可以通过"MongoClient"连接到它
datasources.js:
{
"mongodb_dev": {
"name": "mongodb_dev",
"connector": "mongodb",
"host": "127.0.0.1",
"database": "some-db",
"username": "mongouser",
"password": "pass",
"port": 27017
}
}
以下可能是导致此错误的原因。
- 您正在针对一个数据库进行身份验证,并将其他数据库用于您的应用程序。在您的mongoClient中,您可能会将auth-db配置为"admin",这样您就可以毫无错误地进行连接
如果是这样,你可以通过使用在数据库中创建用户来解决这个问题
use application dbname
db.createUser(
{
user: "username",
pwd: "password",
roles: [ "readWrite", "dbAdmin" ]
}
)
并使用此凭据连接数据库