美文网首页
Springboot使用mongodb遇到问题及解决

Springboot使用mongodb遇到问题及解决

作者: xzf158 | 来源:发表于2020-11-25 11:30 被阅读0次

    网上看到使用mongodb好像很简单,没有什么问题,可我一用就怎么都连不上,先看看我的配置

    在pom.xml中添加依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-mongodb</artifactId>
     </dependency>
    

    再在yml中添加

    spring
      data:
        mongodb:
          uri: mongodb://root:example@127.0.0.1:27017/demo
    

    然后写了一些测试代码,出现下面报错,google好久,终于打到了原因,

    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>}
    

    直接说结果吧

    uri: mongodb://root:example@127.0.0.1:27017/demo?authSource=admin&authMechanism=SCRAM-SHA-1
    

    配置文件里的uri一直要指定authSrource,还是大意了。网上的教程里很多没有帐户密码,可以不指定。我这边有就一定要指定。authMechanism表示认证方式默认就是SCRAM-SHA-1

    authSrource=admin这是指定具有用户凭据的集合的数据库的名称

    小问题,浪费了不少时间,共勉下

    相关文章

      网友评论

          本文标题:Springboot使用mongodb遇到问题及解决

          本文链接:https://www.haomeiwen.com/subject/akagiktx.html