我无法从我的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 。
如果您需要更多的细节,请在下面留下您的评论。
上云精选
2核2G云服务器 每月9.33元起,个人开发者专属3年机 低至2.3折
这可能是网络问题而不是身份验证吗?
Spring引导应用程序在这里指定了一个"localhost“,所以我假设它不会运行在docker.
我建议尝试以下几种方法:
--bind_ip_all
由于@Mark的回答,我意识到我无法使用 root:Pa$$w0rd 凭据对mongodb容器进行身份验证。
root:Pa$$w0rd
我创建了一个名为 init-mongo.js 的文件
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/ 。
./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo-js:ro
services:mongodb:volumes
the docker-compose.yml
/docker-entrypoint-initdb.d/
/docker-entrypoint-initdb.d 是一个已经在mongodb容器中创建的文件夹,用于启动数据库。
/docker-entrypoint-initdb.d
然后相应地更新spring数据mongodb属性:
spring: data: