网上看到使用mongodb好像很简单,没有什么问题,可我一用就怎么都连不上,先看看我的配置
在pom.xml中添加依赖
1 2 3 4 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> |
再在yml中添加
1 2 3 4 |
spring data: mongodb: uri: mongodb://root:[email protected]:27017/demo |
然后写了一些测试代码,出现下面报错,google好久,终于打到了原因,
1 2 |
2020-11-25 11:23:48.939 INFO 67135 --- [ main] org.mongodb.driver.connection : Closed connection [connectionId{localValue:2}] to 127.0.0.1:27017 because there was a socket exception raised by this connection. org.springframework.data.mongodb.UncategorizedMongoDbException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='root', source='demo', password=<hidden>, mechanismProperties=<hidden>}; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='root', source='demo', password=<hidden>, mechanismProperties=<hidden>} |
直接说结果吧
1 | uri: mongodb://root:[email protected]:27017/demo?authSource=admin&authMechanism=SCRAM-SHA-1 |
配置文件里的uri一直要指定authSrource,还是大意了。网上的教程里很多没有帐户密码,可以不指定。我这边有就一定要指定。authMechanism表示认证方式默认就是SCRAM-SHA-1
authSrource=admin这是指定具有用户凭据的集合的数据库的名称
小问题,浪费了不少时间,共勉下
标签:Springboot,遇到,demo,SCRAM,SHA,mongodb,org,root From: https://www.cnblogs.com/exmyth/p/16853116.html