springboot Mongotemplate 操作MongoDB错误信息

Caused by: org.springframework.data.mongodb.UncategorizedMongoDbException: Command failed with error 13: 'not authorized on test to execute command

原因

1.MongoDB开启了验证,在配置文件中没有设置用户名和密码
2. 如果用户名和密码都正确设置了那就是权限没有设置

解决步骤

  1. 第一种情况只需要配置MongoDB链接账号和密码
spring:
  data:
    mongodb:
      custom:
        database: admin
        hosts:
          - dds-u1.mongodb.rds.aliyuncs.com
          - dds-u2.mongodb.rds.aliyuncs.com
        ports:
          - 3717
          - 3717
        username: root
        password: 123456
        authenticationDatabase: admin //验证数据库
        maxConnections: 100
        minConnections: 20
        maxQueue: 10
  1. 第二种情况需要给账号权限
use admin
db.auth("root", "mima") //登录验证root账号
use test //进入要创建或修改用户的库
db.grantRolesToUser("test", [{role:"readWrite",db:"test"},{role:"dbAdmin", db:"test"}]) //修改用户权限
db.createUser({user:"test",pwd:"pwd",roles:[{role:"dbAdmin", db:"test"},{role:"readWrite",db:"test"}]}) //创建用户
Logo

鸿蒙生态一站式服务平台。

更多推荐