我有以下 docker compose
文件,用于连接到 mongoDB 容器
的 Spring boot 容器
:
#
# APIs
#----------------------------------------------
pokerstats:
image: pokerstats
container_name: pokerstats
ports:
- 8080:8080
depends_on:
- db
#
# Utilities
#----------------------------------------------
db:
image: mongo
container_name: mongo
volumes:
- ./database:/data
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerStats
在我的 spring boot application.properties
我有:
server.port=8080
spring.main.allow-bean-definition-overriding=true
spring.data.mongodb.uri=mongodb://admin:admin@mongo:27017/pokerStats
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.database=pokerStats
当我尝试使用 Postman 访问我的端点时,出现以下错误:
{
"timestamp": "2020-04-07T17:39:19.129+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='pokerStats', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='admin', source='pokerStats', password=<hidden>, mechanismProperties=<hidden>}",
"path": "/rounds"
}
请注意,在我将以下内容添加到我的 docker compose 文件之前,它工作正常:
environment:
MONGO_INITDB_ROOT_USERNAME: admin
MONGO_INITDB_ROOT_PASSWORD: admin
MONGO_INITDB_DATABASE: pokerStats
最佳答案
我能够通过添加来解决问题:
应用程序.properties:
spring.data.mongodb.uri=mongodb://admin:admin@mongo:27017/pokerStats?authSource=admin
spring.data.mongodb.authentication-database = admin
spring.data.mongodb.database=pokerStats
我相信:?authSource=admin
需要添加到 mongo uri 的末尾。
关于mongodb - 尝试从 spring boot docker 容器连接到 mongo docker 容器时验证 MongoCredential 时出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61109707/