简体   繁体   English

错误:MongoError:身份验证失败。 我正在使用 docker 和 mongoose

[英]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.我尝试通过 docker-compose 连接到 mongoDB 但是我一遍又一遍地遇到相同的错误,尽管我尝试了来自网络的所有解决方案。 Thank you.谢谢你。

I have my docker-compose.yml as我有我的 docker-compose.yml 作为

# 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我有这样一个 shell 脚本

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:在 Linux 我的朋友可以连接相同的代码片段,但我收到此错误:

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.我通过将端口从 27017 更改为 27018 解决了这个问题。不幸的是,我的另一个应用程序正在使用该端口。

There is my solved used:有我解决的使用:

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

stack.yml: version: "3.5" stack.yml:版本:“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服务:mongo:图像:mongo:最新 container_name:mongo 环境:MONGO_INITDB_ROOT_USERNAME:admin MONGO_INITDB_ROOT_PASSWORD:管理端口:-“0.0.0.0:27017:27017”网络:- MONGO 卷:- 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 环境: ME_CONFIG_MONGODB_ADMINUSERNAME: admin ME_CONFIG_MONGODB_ADMINPASSWORD: admin ME_CONFIG_MONGODB_SERVER: mongo ME_CONFIG_MONGODB_PORT: "27017"端口:-“0.0.0.0:8088:8081”网络:- MONGO 依赖:- mongo

networks: MONGO: name: MONGO网络: MONGO: 名称: MONGO

volumes: MONGO_DATA: name: MONGO_DATA卷: MONGO_DATA:名称: MONGO_DATA

MONGO_CONFIG: name: MONGO_CONFIG MONGO_CONFIG:名称:MONGO_CONFIG

Replace path with your compute absolute path.将路径替换为您的计算绝对路径。

And DO NOT use 'root' to authentication.并且不要使用“root”进行身份验证。

use mongosh使用mongosh

shell script can use: shell 脚本可以使用:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MongoError:身份验证失败。 -无法连接到MongoLab - MongoError: Authentication failed. - trouble connecting to MongoLab 我正在尝试创建反应应用程序但失败了。 它显示错误 - I am trying to create react app but failed. It show error 使用XMLHttpRequest来获取Ecobee API信息。 得到500(错误1:“身份验证失败。需要令牌。”) - Using XMLHttpRequest to GET Ecobee API info. Getting a 500 (Error 1: “Authentication failed. Token is required.”) 当我运行 nodemon 服务器时,我收到错误消息“错误的身份验证失败。[nodemon] 应用程序崩溃 - 在开始之前等待文件更改......” - When I run nodemon server I get the error "bad auth Authentication failed. [nodemon] app crashed - waiting for file changes before starting..." 我正在尝试使用mongoose设置我的mongoDB数据库,但是出现错误“ mongoose默认的Promise库已弃用” - I am trying to setup my mongoDB database using mongoose, but i getting an error “mongoose default promise library is deprecated” 猫鼬查询返回错误,但错误是我要寻找的结果 - Mongoose query returns error, but error is the result I am looking for 使用 Next js 时出现以下错误。 我正在使用 axios 作为 http 客户端构建身份验证管道 - I am getting the following error using Next js. I am building an authentication pipeline using axios as the http client 当我尝试将包含与 EJS 文件一起使用时,编译失败。 (错误:子编译失败。) - Fails to compile, when I try to use include with EJS files. (Error: Child compilation failed.) MongoServerError:使用带有 docker 容器的 mongo 和 mongo express 时身份验证失败 - MongoServerError: Authentication failed when using mongo and mongo express with docker containers 猫鼬交易-MongoError:已中止 - Mongoose Transactions - MongoError: Commited to Aborted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM