【问题标题】:MongoExpress shows "Cannot read property 'listDatabases' of undefined" errorMongoExpress 显示“无法读取未定义的属性 'listDatabases'”错误
【发布时间】:2020-02-06 03:20:30
【问题描述】:

我尝试使用 docker-compose.yml 创建 Mongo 和 Mongo-Express 服务器。这是我的设置

version: "3"
services:
  mongo:
    image: "mongo:3-stretch"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=devroot
      - MONGO_INITDB_ROOT_PASSWORD=devroot
      - MONGO_INITDB_DATABASE=DataBank
    ports:
       - "27017:27017"
  mongo-express:
    image: "mongo-express:latest"
    environment:
      - ME_CONFIG_MONGODB_SERVER=mongo
      - ME_CONFIG_MONGODB_PORT=27017
      - ME_CONFIG_MONGODB_ENABLE_ADMIN=false
      - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
      - ME_CONFIG_MONGODB_AUTH_USERNAME=devroot
      - ME_CONFIG_MONGODB_AUTH_PASSWORD=devroot
      - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
      - ME_CONFIG_BASICAUTH_USERNAME=dev
      - ME_CONFIG_BASICAUTH_PASSWORD=dev
    depends_on:
      - mongo
    ports:
      - "8081:8081"

结果(这里有错误):

mongo-express_1  | Thu Feb  6 03:11:36 UTC 2020 retrying to connect to mongo:27017 (3/5)
mongo-express_1  | /docker-entrypoint.sh: connect: Connection refused
mongo-express_1  | /docker-entrypoint.sh: line 14: /dev/tcp/mongo/27017: Connection refused


尽管出现第一个错误,我仍然可以访问http://localhost:8081/ 但是登录成功后,它显示(另一个错误):

mongo-express_1  | Database connected
mongo-express_1  | Connecting to admin...
mongo_1          | 2020-02-06T03:11:39.826+0000 I ACCESS   [conn2] Successfully authenticated as principal devroot on admin
mongo-express_1  | Database admin connected
mongo-express_1  | Admin database is not accessible
mongo-express_1  | TypeError: Cannot read property 'listDatabases' of undefined
mongo-express_1  |     at Object.connectionData.updateDatabases (/node_modules/mongo-express/lib/db.js:41:11)
mongo-express_1  |     at /node_modules/mongo-express/lib/router.js:94:11
mongo-express_1  |     at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
mongo-express_1  |     at next (/node_modules/express/lib/router/route.js:137:13)
mongo-express_1  |     at Route.dispatch (/node_modules/express/lib/router/route.js:112:3)
mongo-express_1  |     at Layer.handle [as handle_request] (/node_modules/express/lib/router/layer.js:95:5)
mongo-express_1  |     at /node_modules/express/lib/router/index.js:281:22
mongo-express_1  |     at param (/node_modules/express/lib/router/index.js:354:14)
mongo-express_1  |     at param (/node_modules/express/lib/router/index.js:365:14)
mongo-express_1  |     at Function.process_params (/node_modules/express/lib/router/index.js:410:3)
mongo-express_1  | GET / 500 12.737 ms - 1049


我应该注意什么?

【问题讨论】:

    标签: mongodb docker docker-compose


    【解决方案1】:

    这里的问题是 mongo-express 无法访问 admin 数据库。将这些环境变量添加到 mongo express 应该可以解决您的问题

    - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
    - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
    - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
    

    我最终的 docker-compose 文件看起来像这样,它适用于我

    version: '3.3' # specify docker-compose version
    
    services:
      mongo:
        image: mongo
        environment:
          - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER}
          - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD}
          - MONGO_INITDB_DATABASE=project
    
      mongo-express:
        image: mongo-express
        environment:
          - ME_CONFIG_MONGODB_SERVER=mongo
          - ME_CONFIG_MONGODB_PORT=27017
          - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
          - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_ROOT_USER}
          - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_ROOT_PASSWORD}
          - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
          - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER}
          - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD}
          - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN}
          - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD}
        depends_on:
          - mongo
    
    

    注意:请不要在生产中使用它。

    参考: https://hub.docker.com/_/mongo-express

    【讨论】:

    • 默认的 mongo root 用户/密码是什么?
    • 我收到以下错误:无法使用 connectionString 连接到数据库:mongodb://root:pass@mongo:27017/admin"
    猜你喜欢
    • 2019-03-10
    • 1970-01-01
    • 2019-03-09
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2021-12-31
    相关资源
    最近更新 更多