报错:Command failed with error 13: 'not authorized on XXX---SpringBoot整合MongoDB数据库(配置数据库用户名和密码)

1.之前测试SpringBoot测试MongoDB数据库时没有设置数据库账户和密码,在配置application.yml文件时,使用以下配置:

    spring:
      data: 
	mongodb:
	  database: test
	    database: IntelligentGuidance
	    uri: mongodb://106.12.111.157:27017
在没有配置用户名和密码的情况下,增删改查操作无任何问题,但是在配置了MongoDB的用户名和密码后,如果在yml中配置错误的话会出现以下报错:
com.mongodb.MongoCommandException: Command failed with error 13: 'not authorized on XXX

错误信息为没有权限访问,原因就是配置错误,见以下配置:
仍使用uri的方式配置,格式如下:uri: mongodb://用户名:密码@106.12.111.157:27017/datasource名称?authSource=admin&authMechanism=SCRAM-SHA-1

uri: mongodb://username:password@106.12.111.157:27017/IntelligentGuidance?authSource=xxx&authMechanism=SCRAM-SHA-1

亲测可用。
另外也可以使用属性配置的方式,要注意每个属性都需要配置(建议使用上一种)

spring:
  data: 
     mongodb:
       database: test
       database: IntelligentGuidance
       host: 106.12.111.157
       port: 27017
       username: root
       password: 111111
       database: IntelligentGuidance

猜你喜欢

转载自blog.csdn.net/weixin_40550118/article/details/105631266