最近系统需要用到mongodb,通过docker进行部署,正常是很简单的通过docker-compose定义及发布,不过最终还是翻车了,在一个docker下一直是Authentication failed。
版本:mongodb,mongo-express都是latest,
docker-compose配置:
xp-mongo:
container_name: xp-mongo
build:
context: ./mongo
dockerfile: dockerfile
ports:
- "27017:27017"
restart: always
user: root
logging:
driver: json-file
options:
max-size: 1g
volumes:
- ./mongo/database:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=admin
# 因为转移的问题,root密码为xp$27011,可以从docker启动的参数中获取到
- MONGO_INITDB_ROOT_PASSWORD=123456
xp-mongo-express:
container_name: xp-mongo-express
build:
context: ./mongo-express
dockerfile: dockerfile
depends_on:
- xp-mongo
links:
- xp-mongo
restart: always
user: root
logging:
driver: json-file
options:
max-size: 1g
ports:
- "8282:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: admin
ME_CONFIG_MONGODB_ADMINPASSWORD: 123456
ME_CONFIG_MONGODB_ENABLE_ADMIN: "true"
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: 123456
ME_CONFIG_MONGODB_SERVER: xp-mongo
一切正常的话,通过http://localhost:8282应该是能管理mongo的,可是,居然报错Authentication failed。
xp-mongo-express | Could not connect to database using connectionString: mongodb://admin:****@xp-mongo:27017/admin"
xp-mongo-express | /app/node_modules/mongodb/lib/cmap/connection.js:227
xp-mongo-express | callback(new error_1.MongoServerError(document));
xp-mongo-express | ^
xp-mongo-express |
xp-mongo-express | MongoServerError: Authentication failed.
xp-mongo-express | at Connection.onMessage (/app/node_modules/mongodb/lib/cmap/connection.js:227:30)
xp-mongo-express | at MessageStream.<anonymous> (/app/node_modules/mongodb/lib/cmap/connection.js:60:60)
xp-mongo-express | at MessageStream.emit (node:events:517:28)
xp-mongo-express | at processIncomingData (/app/node_modules/mongodb/lib/cmap/message_stream.js:125:16)
xp-mongo-express | at MessageStream._write (/app/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
xp-mongo-express | at writeOrBuffer (node:internal/streams/writable:392:12)
xp-mongo-express | at _write (node:internal/streams/writable:333:10)
xp-mongo-express | at Writable.write (node:internal/streams/writable:337:10)
xp-mongo-express | at Socket.ondata (node:internal/streams/readable:809:22)
xp-mongo-express | at Socket.emit (node:events:517:28) {
xp-mongo-express | ok: 0,
xp-mongo-express | code: 18,
xp-mongo-express | codeName: 'AuthenticationFailed',
xp-mongo-express | connectionGeneration: 0,
xp-mongo-express | [Symbol(errorLabels)]: Set(2) { 'HandshakeError', 'ResetPool' }
xp-mongo-express | }
通过第三方mongodb GUI管理工具Studio 3T连接居然是正常的,那就一定是配置有问题,经过多方检查最终定位在mongo-express连接串的问题上,修正的连接如下:
xp-mongo:
container_name: xp-mongo
build:
context: ./mongo
dockerfile: dockerfile
ports:
- "27017:27017"
restart: always
user: root
privileged: true
logging:
driver: json-file
options:
max-size: 1g
volumes:
- ./mongo/database:/data/db
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: 123456
TZ: Asia/Shanghai
xp-mongo-express:
container_name: xp-mongo-express
build:
context: ./mongo-express
dockerfile: dockerfile
depends_on:
- xp-mongo
restart: always
user: root
privileged: true
logging:
driver: json-file
options:
max-size: 1g
ports:
- "8282:8081"
environment:
ME_CONFIG_BASICAUTH_USERNAME: admin
ME_CONFIG_BASICAUTH_PASSWORD: 123456
ME_CONFIG_MONGODB_ENABLE_ADMIN: "true"
ME_CONFIG_MONGODB_URL: mongodb://admin:123456@xp-mongo:27017/local?retryWrites=true&loadBalanced=false&serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256
TZ: Asia/Shanghai
关键信息应该是authMechanism连接串mongodb://admin:123456@xp-mongo:27017/local?retryWrites=true&loadBalanced=false&serverSelectionTimeoutMS=5000&connectTimeoutMS=10000&authSource=admin&authMechanism=SCRAM-SHA-256