我无法从我的spring引导应用程序连接到本地坞容器中运行的mongodb实例。当应用程序试图连接到数据库时,会引发以下错误:
2022-04-03 22:45:57.243 ERROR 14559 --- [nio-5000-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='root', source='admin', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-256, userName='root', source='admin', password=<hidden>, mechanismProperties=<hidden>}] with root cause
com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 'Authentication failed.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Authentication failed.", "code": 18, "codeName": "AuthenticationFailed"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:198) ~[mongodb-driver-core-4.4.0.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:418) ~[mongodb-driver-core-4.4.0.jar:na]
这是docker组成的mongodb服务定义:
mongodb:
image: mongo
container_name: mongodb
restart: always
ports:
- "27017:27017"
volumes:
- "mongo_data:/tmp/techbank/mongo"
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=Pa$$w0rd
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always # fixes MongoNetworkError when mongodb is not ready when mongo-express starts
ports:
- "8081:8081"
environment:
- ME_CONFIG_MONGODB_ADMINUSERNAME=root
- ME_CONFIG_MONGODB_ADMINPASSWORD=Pa$$w0rd
- ME_CONFIG_MONGODB_SERVER=mongodb
当所有服务启动并运行时,我可以从客户端访问数据库.
这些是spring数据mongodb属性:
spring:
data:
mongodb:
host: localhost
port: 27017
database: bankAccount
username: root
password: Pa$$w0rd
authentication-database: admin
我也试过:
spring:
data:
mongodb:
uri: "mongodb://root:Pa$$w0rd@localhost:27017/bankAccount=true&authSource=admin&authMechanism=SCRAM-SHA-1"
我在堆栈溢出问题上发现了类似的问题,但建议的解决方案在我的情况下行不通。
您可以在这里找到在本地运行应用程序的说明,技术银行-构建-github。
如果您需要更多的细节,请在下面留下您的评论。
发布于 2022-04-05 20:12:35
由于@Mark的回答,我意识到我无法使用root:Pa$$w0rd
凭据对mongodb容器进行身份验证。
我创建了一个名为init-mongo.js
的文件
db.createUser(
{
user: "storeAdmin",
pwd: "storeAdmin2022",
roles: [
{
role: "readWrite",
db: "bankAccount"
}
]
}
)
在./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo-js:ro
中services:mongodb:volumes
下添加了the docker-compose.yml
,以便将init-mongo.js
作为只读文件复制到/docker-entrypoint-initdb.d/
。
/docker-entrypoint-initdb.d
是一个已经在mongodb容器中创建的文件夹,用于启动数据库。
然后相应地更新spring数据mongodb属性:
spring:
data:
mongodb:
host: localhost
port: 27017
database: bankAccount
username: storeAdmin
password: storeAdmin2022
现在它就像一种魅力
发布于 2022-04-03 20:59:56
这可能是网络问题而不是身份验证吗?
Spring引导应用程序在这里指定了一个"localhost“,所以我假设它不会运行在docker.
我建议尝试以下几种方法:
--bind_ip_all
选项将mongo自身绑定到所有接口,就像官方蒙戈文件建议的那样https://stackoverflow.com/questions/71729732
复制相似问题