关于spring-data-mongodb使用用户名密码登录报错问题解决方法:Failed to authenticate to database

一.问题

  1.spring-data-mongodb用户名密码登录报错问题:Failed to authenticate to database 

     

信息: Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=80}
六月 13, 2017 9:56:51 下午 com.mongodb.diagnostics.logging.JULLogger log
信息: Exception in monitor thread while connecting to server 127.0.0.1:27017
com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=null, userName='root', source='testdb', password=<hidden>, mechanismProperties={}}
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:61)
at com.mongodb.connection.DefaultAuthenticator.authenticate(DefaultAuthenticator.java:32)
at com.mongodb.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:99)
at com.mongodb.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:44)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128)
at java.lang.Thread.run(Thread.java:748)
Caused by: com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server 127.0.0.1:27017. The full response is { "ok" : 0.0, "errmsg" : "Authentication failed.", "code" : 18, "codeName" : "AuthenticationFailed" }
at com.mongodb.connection.CommandHelper.createCommandFailureException(CommandHelper.java:170)
at com.mongodb.connection.CommandHelper.receiveCommandResult(CommandHelper.java:123)
at com.mongodb.connection.CommandHelper.executeCommand(CommandHelper.java:32)
at com.mongodb.connection.SaslAuthenticator.sendSaslStart(SaslAuthenticator.java:95)
at com.mongodb.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:45)
... 6 more

二.解决方法

2.1.首先是mongodb数据库设置密码

登录mong shell,

>use admin

>db.system.users.remove({})

>use testdb

>db.createUser({user:"abc",pwd:"123456"},roles["readWrite","dbAdmin"])

 

mongo服务以登录验证权限方式启动命令:

切换到mongod安装路径下,执行:./mongod --dbpath=/home/db/data -auth --logpath=/home/db/logs/mongodb.log

2.2项目中spring配置

(1)添加相应jar包:

   Maven项目的pom.xml添加spring-data-mongodb的jar包,如下

  <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.10.3.RELEASE</version>
    </dependency>

  由于spring-data-mongodb默认依赖的mongo-java-drive驱动包是2.*.*版本的,所以我们手动添加3.*版本的mongodb驱动包,如下

  <dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.2.2</version>
  </dependency>

(2)配置文件

  <mongo:mongo-client id="mongo" host="${mongo.host}"
  port="${mongo.port}" credentials="${mongo.username}:${mongo.password}@${mongo.dbname}">
  <mongo:client-options connections-per-host="${mongo.connectionsPerHost}"
  write-concern="SAFE"
  threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}"
  connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}"
  socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" />
  </mongo:mongo-client>
  <!-- 设置使用的数据库 名 -->
  <mongo:db-factory id="mongoDbFactory" dbname="${mongo.dbname}"
  mongo-ref="mongo" />
  <!-- mongodb的模板 -->
  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
  <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

其中,mongodb3.×的java驱动包,属性配置是<mongo:client-options>而不在是<mongo:options>.

 




posted @ 2017-06-13 21:16  东小邪  阅读(12958)  评论(0编辑  收藏  举报