我试图通过docker-compose连接到mongoDB,但我一次又一次地收到相同的错误,尽管我尝试了网络上的所有解决方案。谢谢。
我有我的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
我有这样一个shell脚本
mongo -- "$MONGO_INITDB_DATABASE" <<EOF
db.createUser({
user: "$MONGO_USERNAME",
pwd: "$MONGO_PASSWORD",
roles: [
{ role: 'readWrite', db: "$MONGO_INITDB_DATABASE" }
]
})
EOF
我尝试通过以下方式连接到数据库:
mongoose
.connect("mongodb://admin:example@localhost:27017/test")
.then(() => console.log("connected"))
.catch((e) => console.log("error:", e));
在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'
}
发布于 2021-03-23 04:05:19
我仅仅通过将端口从27017改为27018就解决了这个问题。不幸的是,我的另一个应用程序正在使用这个端口。
发布于 2021-06-28 18:32:01
这里有我的已解决的使用:
docker-compose -f stack.yml up --build --force-recreate --renew-anon-volumes -d
stack.yml :版本:"3.5“
服务: mongo:图像: MONGO :最新数据: mongo环境: MONGO_INITDB_ROOT_USERNAME:管理数据:管理端口:- "0.0.0.0:27017:27017“网络:-mongo卷:-mongo_ MONGO_INITDB_ROOT_PASSWORD: /container_name/lirui/docker/data/mongo/db- MONGO_CONFIG:/Users/lirui/docker/data/mongo/configdb mongo-express:图像: mongo-express :最新container_name: mongo-express环境: ME_CONFIG_MONGODB_管理员名称:管理员ME_CONFIG_MONGODB_ADMINPASSWORD:管理员ME_CONFIG_MONGODB_SERVER: mongo ME_CONFIG_MONGODB_PORT:"27017“端口:- "0.0.0.0:8088:8081”网络:- MONGO depends_on:- mongo
网络: MONGO:名称: MONGO
卷: MONGO_DATA:名称: MONGO_DATA
MONGO_CONFIG:名称: MONGO_CONFIG
将path替换为您的计算绝对路径。
并且不要使用“root”进行身份验证。
https://stackoverflow.com/questions/66738344
复制相似问题