‘Authentication failed.‘ on server xx.xx.xxx.x:27017. The full response is { “ok“ : 0.0, “errmsg“

异常产生环境

在SpringBoot项目中使用yml文件连接mongodb产生(mongodb带密码验证)

异常描述

com.mongodb.MongoCommandException: Command failed with error 18 (AuthenticationFailed): 
'Authentication failed.' on server xx.xx.xx.xx:27017. 
The full response is {
    
     "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }

异常原因

yml配置连接信息错误造成的

解决办法

修改前

spring:
   data:
    mongodb:
      host: 主机号
      port: 27017(端口号)
      database: 数据库名
      username: root
      password: 密码

(在我印象中这种方法好像也是可以运行的)

修改后

spring:
  data:
    mongodb:
      database: 数据库名
      uri: mongodb://root:密码@主机号:27017(端口号)

(修改成这个配置项目可以运行)

猜你喜欢

转载自blog.csdn.net/qq_42025798/article/details/114417527