6

I have an empty loopback app, where I added a model and a mongodb datasource.

When connecting, I get the following error:

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)

The db runs in a docker. I can connect to it via "MongoClient"

datasources.js:

{
    "mongodb_dev": {
        "name": "mongodb_dev",
        "connector": "mongodb",
        "host": "127.0.0.1",
        "database": "some-db",
        "username": "mongouser",
        "password": "pass",
        "port": 27017
    }
}
Alexander
  • 310
  • 2
  • 13
  • Are you sure of those credentials ? – Overdrivr May 20 '16 at 12:11
  • Also, does the issue happens immediately at server boot, or after when you use the REST API ? – Overdrivr May 20 '16 at 12:12
  • On server boot. Yes I am sure - that's why I wrote that I can connect via MongoClient, so it's also not a port issue. – Alexander May 22 '16 at 07:02
  • Do you have a boot script that performs an `autoupdate` or `automigrate` of the database ? If yes, could you post it ? – Overdrivr May 24 '16 at 07:23
  • In order to remove the error , goto datasources.json and update the username & password as empty string and then try and connect. I have the same issue, trying to find out the root cause. – Viswas Menon Jul 18 '16 at 12:01
  • @Alexander how did u fix this issue currently i am facing the same can you post your answer – YLS Dec 11 '16 at 15:54
  • @YLS as far as I remember, the problem was not recoverable. I had to switch to a different DB (I used postgresql in docker) and it worked. I think it was some loopback component that was not ready for it and didn't seem to be in the foreseeable future (not sure about that though). – Alexander Dec 13 '16 at 13:44
  • thanks to @ViswasMenon and Alexander for posting this question i spent more than 2hrs on this. after setting the username and password as empty string it worked. If i want username and password to be set then how to do. – YLS Dec 14 '16 at 06:01
  • thank you, it worked for empty strings ^_^ @ViswasMenon – Zainab Hammami Aug 15 '17 at 11:05

1 Answers1

7

The following might be the reason for this error.

  1. You are authenticating against one database and using some other database for your application. In your mongoClient you might be configured auth db as "admin" so you can connect without error.

If so you can solve this by creating users in your database by using

use application dbname
db.createUser(
   {
     user: "username",
     pwd: "password",
     roles: [ "readWrite", "dbAdmin" ]
   }
)

And use this credential for connecting your database

nijin aniyath
  • 101
  • 1
  • 5