[英]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
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.