springboot整合mongodb,配置账户名和密码登录验证

springboot引入

application.yml配置,这里之前碰到一个坑,mongodb的配置没有写host和port属性,只写了database和uri,这种方式在无密码验证的情况下,可以连接mongodb。但是在mongodb设置了密码登录后,就无法连接,一直提示

Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on wxsb_dev to execute command { insert

这里我们说下,application.yml关于mongodb的两种配置

第一种,yml方式,注意这里的host port username password database,每个属性都要配置。

spring:
  data:
    mongodb:
      host: 192.168.11.121
      port: 27017
      username: aroot
      password: 999999
      database: udb

第二种,uri方式

spring:
  data:
    mongodb:
      uri: mongodb://aroot:[email protected]:27017/udb
      

之前就是配了如下的参数,导致一直出错,还以为是mongodb的用户权限配置出错导致,原来是配置文件出错,报错信息

Caused by: com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on wxsb_dev to execute command { insert

spring:
  data:
    mongodb:
      database: udb
      uri: mongodb://192.168.11.111:27017
      username: aroot
      password: 999999

猜你喜欢

转载自blog.csdn.net/u012373281/article/details/93747350
今日推荐