简体   繁体   中英

error: MongoError: Authentication failed. I am using docker and mongoose

I try to connect to mongoDB through docker-compose but i get the same error over and over again although i tried all the solutions from the net. Thank you.

I have my docker-compose.yml as

# Use root/example as user/password credentials
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    container_name: mongo
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
      MONGO_INITDB_DATABASE: test
      MONGO_USERNAME: admin
      MONGO_PASSWORD: example
      
    volumes:
      - ./data:/data/db
      - ./mongo-entrypoint.sh:/docker-entrypoint-initdb.d/mongo-init.sh:ro
    command: mongod


  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

And I have such a shell script

mongo -- "$MONGO_INITDB_DATABASE" <<EOF
  db.createUser({
    user: "$MONGO_USERNAME",
    pwd: "$MONGO_PASSWORD",
    roles: [
      { role: 'readWrite', db: "$MONGO_INITDB_DATABASE" }
    ]
  })
EOF

And I try to connect to database by:

mongoose
  .connect("mongodb://admin:example@localhost:27017/test")
  .then(() => console.log("connected"))
  .catch((e) => console.log("error:", e));

In Linux my friend can connect with the same code pieces but I am getting this error:

running on 3000
error: MongoError: Authentication failed.
    at MessageStream.messageHandler (C:\Users\kamad\Desktop\3-2\cs308\proje\backend\node_modules\mongodb\lib\cmap\connection.js:268:20)
    at MessageStream.emit (events.js:315:20)
    at processIncomingData (C:\Users\kamad\Desktop\3-2\cs308\proje\backend\node_modules\mongodb\lib\cmap\message_stream.js:144:12)
    at MessageStream._write (C:\Users\kamad\Desktop\3-2\cs308\proje\backend\node_modules\mongodb\lib\cmap\message_stream.js:42:5)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at Socket.ondata (internal/streams/readable.js:719:22)
    at Socket.emit (events.js:315:20)
    at addChunk (internal/streams/readable.js:309:12)
    at readableAddChunk (internal/streams/readable.js:284:9)
    at Socket.Readable.push (internal/streams/readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  ok: 0,
  code: 18,
  codeName: 'AuthenticationFailed'
}

I solved this just by changing the port from 27017 to 27018. Unfortunately, another app of mine was using that port.

There is my solved used:

docker-compose -f stack.yml up --build --force-recreate --renew-anon-volumes -d

stack.yml: version: "3.5"

services: mongo: image: mongo:latest container_name: mongo environment: MONGO_INITDB_ROOT_USERNAME: admin MONGO_INITDB_ROOT_PASSWORD: admin ports: - "0.0.0.0:27017:27017" networks: - MONGO volumes: - MONGO_DATA:/Users/lirui/docker/data/mongo/db - MONGO_CONFIG:/Users/lirui/docker/data/mongo/configdb mongo-express: image: mongo-express:latest container_name: mongo-express environment: ME_CONFIG_MONGODB_ADMINUSERNAME: admin ME_CONFIG_MONGODB_ADMINPASSWORD: admin ME_CONFIG_MONGODB_SERVER: mongo ME_CONFIG_MONGODB_PORT: "27017" ports: - "0.0.0.0:8088:8081" networks: - MONGO depends_on: - mongo

networks: MONGO: name: MONGO

volumes: MONGO_DATA: name: MONGO_DATA

MONGO_CONFIG: name: MONGO_CONFIG

Replace path with your compute absolute path.

And DO NOT use 'root' to authentication.

use mongosh

shell script can use:

mongosh --port 27017 --authenticationDatabase -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD"  <<EOF
  use admin
  db.createUser({
     user: process.env.MONGO_USERNAME ,
     pwd: process.env.MONGO_PASSWORD ,
     roles: [{ 
        role: 'readWrite',
        db: process.env.MONGO_INITDB_DATABASE }]
   })
EOF

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