我正在try 使用身份验证运行mongo docker映像.按照documentation中最简单的示例,我使用docker-compose up命令运行了mongo和mongo express图像.现阶段我的建议是:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

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

运行时,两个容器都可以启动,我可以从mongo express网站浏览mongo的内容.但是,每当我更改docker-compose.yml文件中的用户名或密码时,例如:

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example123

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

mongo express会抛出未经分析的错误消息:

mongo-express_1  | Admin Database connected
mongo-express_1  | { MongoError: Authentication failed.
mongo-express_1  |     at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1  |     at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1  |     at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1  |     at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1  |     at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1  |     at emitOne (events.js:116:13)
mongo-express_1  |     at Socket.emit (events.js:211:7)
mongo-express_1  |     at addChunk (_stream_readable.js:263:12)
mongo-express_1  |     at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1  |     at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1  |   name: 'MongoError',
mongo-express_1  |   message: 'Authentication failed.',
mongo-express_1  |   ok: 0,
mongo-express_1  |   errmsg: 'Authentication failed.',
mongo-express_1  |   code: 18,
mongo-express_1  |   codeName: 'AuthenticationFailed' }
mongo-express_1  | unable to list databases
mongo-express_1  | { MongoError: command listDatabases requires authentication
mongo-express_1  |     at Function.MongoError.create (/node_modules/mongodb-core/lib/error.js:31:11)
mongo-express_1  |     at /node_modules/mongodb-core/lib/connection/pool.js:483:72
mongo-express_1  |     at authenticateStragglers (/node_modules/mongodb-core/lib/connection/pool.js:429:16)
mongo-express_1  |     at Connection.messageHandler (/node_modules/mongodb-core/lib/connection/pool.js:463:5)
mongo-express_1  |     at Socket.<anonymous> (/node_modules/mongodb-core/lib/connection/connection.js:319:22)
mongo-express_1  |     at emitOne (events.js:116:13)
mongo-express_1  |     at Socket.emit (events.js:211:7)
mongo-express_1  |     at addChunk (_stream_readable.js:263:12)
mongo-express_1  |     at readableAddChunk (_stream_readable.js:250:11)
mongo-express_1  |     at Socket.Readable.push (_stream_readable.js:208:10)
mongo-express_1  |   name: 'MongoError',
mongo-express_1  |   message: 'command listDatabases requires authentication',
mongo-express_1  |   ok: 0,
mongo-express_1  |   errmsg: 'command listDatabases requires authentication',
mongo-express_1  |   code: 13,
mongo-express_1  |   codeName: 'Unauthorized' }

无论我在docker-compose.yml中输入什么用户名或密码,我都无法让mongo express连接到mongo,除非我使用原来的rootexample对.

请注意,我没有将用户名和密码作为环境变量,但它们直接输入到docker-compose.yml文件中,如您所见.

还要注意的是,当我将MONGO_INITDB_ROOT_USERNAMEMONGO_INITDB_ROOT_PASSWORD(mongo’s)变量更改为任何值时,它们似乎都没有效果,我仍然可以使用原始根和示例凭据连接mongo express.

是什么导致了这种行为?我怎样才能做到这一点?

推荐答案

docker compose命令:

docker-compose up --build --force-recreate    

Mongo image uses anonymous volumes,所以您还需要--renew-anon-volumes(doc):

docker-compose up --build --force-recreate --renew-anon-volumes

Otherwise previous volume with already initialized DB is used => INITDB env variables won't be used.

Mongodb相关问答推荐

映射数组值并查找每个匹配的集合

查找/查找单个深度嵌套文档的多个集合

mongo如何通过聚合加载嵌套文档

Kotlin 使用初级构造实例化公开类

MongoDB聚合:如何将查找结果放入嵌套数组中?

用Golang减go mongodb中的两个字段

Pymongo API TypeError: Unhashable dict

Stripe:必须提供来源或客户

通过 Spring Boot 应用程序访问 mongodb 时的身份验证错误

如何更新 mongodb 文档以向数组添加新元素?

MongoDB:单个数据库处理程序的 >5 个打开连接

嵌套数组内的Mongodb增量值

将 MongoDB 数据库复制到本地计算机

MongoDB 中的所有列

mongodb nodejs - 转换圆形 struct

我如何使用 Twitter 的流 api 中的推文并将它们存储在 mongodb 中

查询 Mongoid/rails 3 中的嵌入对象(Lower than、Min 运算符和排序)

mongodb类型更改为数组

如何判断 MongoDB 中是否存在字段?

使用 $in 进行不区分大小写的搜索