SpringBoot 3操作MongoDB触发Error 18认证失败问题求助
SpringBoot 3 连接MongoDB插入操作时认证失败的解决办法
问题描述
SpringBoot 3连接Docker部署的MongoDB时,连接阶段无异常,但执行插入/保存文档操作时触发认证失败错误,错误信息如下:
Caused by: 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:205) ~[mongodb-driver-core-4.9.1.jar:na] at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:443) ~[mongodb-driver-core-4.9.1.jar:na] at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:365) ~[mongodb-driver-core-4.9.1.jar:na] at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:102) ~[mongodb-driver-core-4.9.1.jar:na] at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:49) ~[mongodb-driver-core-4.9.1.jar:na] at com.mongodb.internal.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:224) ~[mongodb-driver-core-4.9.1.jar:na] at com.mongodb.internal.connection.SaslAuthenticator.getNextSaslResponse(SaslAuthenticator.java:131) ~[mongodb-driver-core-4.9.1.jar:na] ... 62 common frames omitted
相关配置文件:
application.yml
server: port: 8400 spring: application: name: notification-service kafka: bootstrap: servers: https://localhost:9092 data: mongodb: username: "root" password: "example" host: "localhost" port: 27017 database: "notification" authentication-database: "admin" #uri: mongodb://root:example@127.0.0.1:27017/notification?authSource=admin (I tried uri but didnt work as it was provided solution on stackoverflow)
docker-compose.yml
mongo: image: mongo restart: always environment: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: example volumes: - mongodb:/data mongo-express: image: mongo-express restart: always ports: - 8081:8081 environment: ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: example ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/ ME_CONFIG_BASICAUTH_USERNAME: mexpress ME_CONFIG_BASICAUTH_PASSWORD: mexpress volumes: mongodb:
解决步骤
1. 重置MongoDB初始化数据
MongoDB的MONGO_INITDB_*环境变量仅在容器第一次创建并挂载空数据卷时生效,如果之前容器已经启动过,可能存在初始化异常。执行以下操作重置:
# 停止容器并删除数据卷 docker-compose down -v # 重新启动Mongo容器 docker-compose up -d mongo
等待容器启动后,进入容器验证root用户是否存在:
docker exec -it $(docker ps -q -f name=mongo) mongosh -u root -p example --authenticationDatabase admin use admin db.getUser("root")
如果返回用户信息,说明初始化正常;如果返回null,查看容器日志排查初始化问题:
docker logs $(docker ps -q -f name=mongo)
2. 修正SpringBoot MongoDB URI配置
直接使用URI配置避免分散参数冲突,同时指定认证机制,修改application.yml:
spring: data: mongodb: uri: mongodb://root:example@localhost:27017/notification?authSource=admin&authMechanism=SCRAM-SHA-1
SpringBoot 3搭配的Mongo驱动默认可能使用SCRAM-SHA-256,但旧版本Mongo容器初始化的用户可能仅支持SCRAM-SHA-1,显式指定可避免兼容性问题。
3. 验证驱动版本兼容性
SpringBoot 3.x默认搭配的MongoDB驱动版本为4.10+,确保你的依赖没有手动指定低版本:
Maven
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
Gradle
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
手动指定驱动版本时,必须保证和SpringBoot 3兼容。
4. 本地直连测试
先用Mongo Shell直接连接测试,排除网络或认证信息问题:
mongosh mongodb://root:example@localhost:27017/notification?authSource=admin
如果连接失败,先解决网络或认证信息问题,再调试SpringBoot应用。
5. 创建数据库专用用户(推荐)
避免使用root超级管理员操作业务数据库,为notification数据库创建专用读写用户:
进入Mongo容器Shell:
docker exec -it $(docker ps -q -f name=mongo) mongosh -u root -p example --authenticationDatabase admin use notification db.createUser({ user: "notification_user", pwd: "notification_pwd", roles: [ { role: "readWrite", db: "notification" } ] })
修改SpringBoot配置为专用用户:
spring: data: mongodb: username: notification_user password: notification_pwd host: localhost port: 27017 database: notification authentication-database: notification
内容的提问来源于stack exchange,提问作者zeyad ahmed
相关产品推荐
相关产品推荐

