MongoDB + Mongo-Express docker-compose 导致 MongoError:身份验证失败



我已经在这个问题上呆了几个小时了,我真的不知道还能做什么(不幸的是,我所做的所有研究都没有提供任何解决方案(。所以我问是否有人能想到为什么这不起作用的答案:

我运行一个带有 mongo 和 mongo-express 图像的 docker-compose 文件。使用此处提供的默认代码不起作用(mongo-express 停止使用退出代码 1(,所以我尝试了大量配置,给我留下了下面的 docker-compose 文件(实际上可以达到 mongo-express 启动并显示的程度

数据库: 管理员

服务器状态: 在配置中打开admin.js查看服务器统计信息!

显示在图形用户界面中(。但从那里没有任何效果。控制台显示问题:

mongoviewer_1  | Database connected
database_1     | 2018-08-22T09:15:27.743+0000 I ACCESS   [conn2] SASL SCRAM-SHA-1 authentication failed for admin on admin from client; UserNotFound: Could not find user admin@admin
mongoviewer_1  | unable to list databases
mongoviewer_1  | { MongoError: command listDatabases requires authentication
mongoviewer_1  |     at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongoviewer_1  |     at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongoviewer_1  |     at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongoviewer_1  |     at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongoviewer_1  |     at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongoviewer_1  |     at emitOne (events.js:116:13)
mongoviewer_1  |     at Socket.emit (events.js:211:7)
mongoviewer_1  |     at addChunk (_stream_readable.js:263:12)
mongoviewer_1  |     at readableAddChunk (_stream_readable.js:250:11)
mongoviewer_1  |     at Socket.Readable.push (_stream_readable.js:208:10)
mongoviewer_1  |   name: 'MongoError',
mongoviewer_1  |   message: 'command listDatabases requires authentication',
mongoviewer_1  |   ok: 0,
mongoviewer_1  |   errmsg: 'command listDatabases requires authentication',
mongoviewer_1  |   code: 13,
mongoviewer_1  |   codeName: 'Unauthorized' }

docker 组合如下所示:

database:
image: mongo:latest
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: password
MONGO_INITDB_DATABASE: dockerdb
ports:
- "27017:27017"
mongoviewer:
image: mongo-express
environment:
ME_CONFIG_MONGODB_SERVER: database
ME_CONFIG_BASICAUTH_USERNAME: ""
ME_CONFIG_MONGODB_AUTH_DATABASE: admin
ME_CONFIG_MONGODB_AUTH_USERNAME: admin
ME_CONFIG_MONGODB_AUTH_PASSWORD: password
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: password
ports:
- "8081:8081"
links:
- database
depends_on:
- database

这是我正在尝试的当前版本,我尝试了许多其他配置,但没有解决身份验证问题。

如果有人有答案或至少对该怎么做有一个想法,我将不胜感激!

本质上,当您配置docker-compose.yaml时有两种状态。我不是专家,但这个解决方案为我解决了这个问题。

  1. 无需身份验证

默认情况下,当您运行实例时mongo它需要身份验证才能访问数据库,因为您同时指定了MONGO_INITDB_ROOT_USERNAMEMONGO_INITDB_ROOT_PASSWORD

您可以通过删除这两个密钥来访问mongo中的数据库。

  1. 需要身份验证

根据 mongo-express 文档,默认情况下,ME_CONFIG_MONGODB_ENABLE_ADMIN密钥是false使您的 mongo-express 应用程序不会尝试任何身份验证并直接连接到您的mongo实例(从而产生Authentication failed错误(。

您需要通过将ME_CONFIG_MONGODB_ENABLE_ADMIN密钥更改为 true 来启用它。记得在你的docker-compose.yaml中串起"真实"。

确保ME_CONFIG_MONGODB_ADMINUSERNAMEME_CONFIG_MONGODB_ADMINPASSWORD密钥分别与MONGO_INITDB_ROOT_USERNAMEMONGO_INITDB_ROOT_PASSWORD密钥匹配。

根据 docker 文件上的信息,如果 MongoDB 在名为 "database" 的容器上运行,则可以使用以下方法从 mongo-express 访问它:

docker run -it --rm 
--network web_default 
--name mongo-express 
-p 8081:8081 
-e ME_CONFIG_MONGODB_SERVER=database 
-e ME_CONFIG_MONGODB_ADMINUSERNAME= admin 
-e ME_CONFIG_MONGODB_ADMINPASSWORD=password 
mongo-express

有关参考,请查看 dockerhub 上的 mongo-express 文档

注意:如果要使用用户凭据访问特定数据库:

docker run -it --rm 
--network web_default 
--name mongo-express 
-p 8081:8081 
-e ME_CONFIG_MONGODB_SERVER= database 
-e ME_CONFIG_MONGODB_AUTH_DATABASE= dockerdb 
-e ME_CONFIG_MONGODB_ENABLE_ADMIN=false 
-e ME_CONFIG_MONGODB_AUTH_USERNAME=admin 
-e ME_CONFIG_MONGODB_AUTH_PASSWORD=password 
mongo-express

相关内容

最新更新