连接MongoDB与mongo-express时遇认证错误及驱动警告问题求助
解决MongoDB与mongo-express Docker Compose连接认证问题
问题场景
使用以下Docker Compose配置启动MongoDB和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 ME_CONFIG_MONGODB_SERVER: mongo
启动后查看mongo-express日志出现以下错误:
(node:7) [MONGODB DRIVER] Warning: Current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor. (node:7) UnhandledPromiseRejectionWarning: MongoError: command listDatabases requires authentication at Connection. (/node_modules/mongodb/lib/core/connection/pool.js:453:61) at Connection.emit (events.js:314:20) at processMessage (/node_modules/mongodb/lib/core/connection/connection.js:456:10) at Socket. (/node_modules/mongodb/lib/core/connection/connection.js:625:15) at Socket.emit (events.js:314:20) at addChunk (_stream_readable.js:297:12) at readableAddChunk (_stream_readable.js:272:9) at Socket.Readable.push (_stream_readable.js:213:10) at TCP.onStreamRead (internal/stream_base_commons.js:188:23) (node:7) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:7) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案
1. 调整服务启动顺序与健康检查
mongo-express常因MongoDB未完成用户初始化就尝试连接导致认证失败,通过健康检查控制启动顺序:
version: '3.1' services: mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: example healthcheck: test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')", "-u", "root", "-p", "example"] interval: 5s timeout: 5s retries: 5 mongo-express: image: mongo-express restart: always ports: - 8081:8081 environment: ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: example ME_CONFIG_MONGODB_SERVER: mongo ME_CONFIG_MONGODB_OPTIONS: '{"useUnifiedTopology": true}' # 消除驱动弃用警告 depends_on: mongo: condition: service_healthy
2. 关键配置说明
- MongoDB健康检查:通过
mongosh执行ping命令并携带认证信息,确保用户初始化完成后才标记服务为健康状态。 - 依赖条件:mongo-express仅在MongoDB服务健康时启动,避免过早发起未认证的连接请求。
- ME_CONFIG_MONGODB_OPTIONS:添加
useUnifiedTopology: true参数,直接解决MongoDB驱动的弃用警告。
3. 验证修复
执行以下命令重启服务:
docker-compose down && docker-compose up -d
等待片刻后访问http://localhost:8081,若能正常进入mongo-express界面则说明问题解决。可通过以下命令确认日志无错误:
docker logs mongo-express
内容的提问来源于stack exchange,提问作者Mat Maan
相关产品推荐
相关产品推荐

