Mongoerror: Authentication failed. Mongoose

Asked

Viewed 78 times

-1

I am running an application Node and mongodb Docker on my machine, but when I try to connect to the bank using moogose returns:

MongoError: Authentication failed.
    at MessageStream.messageHandler (D:\Node\08-multi-dbb-mongodb\node_modules\mongodb\lib\cmap\connection.js:272:20)
    at MessageStream.emit (events.js:223:5)
    at processIncomingData (D:\Node\08-multi-dbb-mongodb\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
    at MessageStream._write (D:\Node\08-multi-dbb-mongodb\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
    at doWrite (_stream_writable.js:435:12)
    at writeOrBuffer (_stream_writable.js:419:5)
    at MessageStream.Writable.write (_stream_writable.js:309:11)
    at Socket.ondata (_stream_readable.js:728:22)
    at Socket.emit (events.js:223:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at Socket.Readable.push (_stream_readable.js:224:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:181:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed',
  name: 'MongoError'
}

Follow how I’m making the connection:
file name: mongodb.js

const mongoose = require('mongoose')
mongoose.connect('mongodb://user:password@localhost:27017/dbname', 
    {useNewUrlParser: true, useUnifiedTopology: true}, function (error){
        if(!error) return;
        console.log('Falha na conexao!', error)
    }
)

const connection = mongoose.connection
connection.once('open', () => console.log('database rodando!!'))

I can access the bank through the client (visual interface) and I can connect to the bank also through the terminal, giving a

docker exec -it 347b053d4320 mongo -u user -p password --authenticationDatabase myDb

But when I try the code running the mongodb.js file, it returns the error.

If you can help me please.

Thank you!

1 answer

-3

If you know English, check out what I found: Connection to Docker container using Mongoose

From what I understand the authentication using Docker works slightly differently than conventional. Good, but not only that. I am studying Mongo and it seems necessary to add permissions to the database in question. Try the following command:

db.getUsers()

The first bank to be created by Mongo is admin. If you run the getUsers() will have a way out as:

    > db.getUsers()
[
        {
                "_id" : "admin.henrique",
                "userId" : UUID("fa52408f-d9ed-43a2-b524-7b64233a2752"),
                "user" : "henrique",
                "db" : "admin",
                "roles" : [
                        {
                                "role" : "userAdminAnyDatabase",
                                "db" : "admin"
                        },
                        {
                                "role" : "dbAdminAnyDatabase",
                                "db" : "admin"
                        },
                        {
                                "role" : "readWriteAnyDatabase",
                                "db" : "admin"
                        }
                ],
                "mechanisms" : [
                        "SCRAM-SHA-1",
                        "SCRAM-SHA-256"
                ]
        },
        {
                "_id" : "admin.root",
                "userId" : UUID("2d668d76-4aa3-4ed2-b4a9-5836fdda1cc2"),
                "user" : "root",
                "db" : "admin",
                "roles" : [
                        {
                                "role" : "root",
                                "db" : "admin"
                        }
                ],
                "mechanisms" : [
                        "SCRAM-SHA-1",
                        "SCRAM-SHA-256"
                ]
        }
]

If you run in the bank you created, you probably have something like:

[ ]

That is, no user has permissions to manipulate.

Probably our problem is in this section of the documentation: Add user

Big brother, here’s a way to solve your pain.

Create a new user and give permission to edit the database in question. It worked for me.

    db.createUser(
  {
    user: "myUser",
    pwd: passwordPrompt(),  
    roles: [
       { role: "readWrite", db: "meudb" }
    ]
  }
)

Like everything in Mongo is document, Mongo creates a document to control access as well. Extremely important detail. Switch to the db in question.

use meubd;

Roles, are functions. So in this creation BSON you put which banks this user has permission and which are these permissions.

My API is now working, I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.