简体   繁体   中英

MongoError: Authentication failed. Mongoose

I'm running a node and mongodb docker application on my machine, but when I try to connect to the database using moogose it 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'
}

Here's 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 Database through the client (visual interface) and I can also connect to the Database through the terminal, for example

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

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

If you can help me out of kindness.

Thanks!

It all depends where you are making the connection from and how your Docker configuration looks like.

Separate Docker Containers

Suppose you have Mongo in a Docker Container and NodeJS in another Docker Container.

If you do the connection from NodeJS then Mongo is not running on localhost.

NodeJS and Mongo in seaparate Containers but in the same docker-compose file

Again looking from the NodeJS-standpoint, Mongo is not running on localhost but on the service in docker-compose

Example

version: '3.7'

services:

  myapp:
    image: img_myapp:latest
    build:
      context: ./build/
    container_name: myapp
    depends_on:
      - database
    ports:
      - "8091:80"
    networks:
      - myapp-network

  database:
    image: 'mongo'
    container_name: 'mongo'
    environment:
      MONGO_INITDB_DATABASE: mydb
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    ports: 
      - "27999:27017"
    logging:
      driver: "json-file"
      options:
        max-size: "200k"
        max-file: "10"
    volumes:
      - ./database:/data/db
    networks:
      - myapp-network


networks:
  myapp-network:

In the above config you connect to mongodb://root:root@database:27999/mydb

Running Mongo in a container and NodeJS on the host

In that case you can connect to localhost if mongo-container published its port. Which it did in the above yml: port 27999

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM