MongoDB"异常验证MongoCredential"使用java-driver

时间:2016-08-13 18:29:05

标签: java mongodb scala mongo-java-driver

我想使用mongo-java-driver 3.2.2连接到我的secondary-mongo节点,如下所示(我在私人网络中)

object Database {

  //val uri = new MongoClientURI("mongodb://staging-node3.shaharma.com") 
  val seeds = new ServerAddress("staging-node3.shaharma.com")
  val credentials : MongoCredential = MongoCredential.createScramSha1Credential(
       "userId", "events", "userP@ssword".toCharArray)
   val options = new MongoClientOptions.Builder().readPreference(ReadPreference.secondary()).build()
}



def something()  = {
      val connection = new MongoClient(Database.seeds, asList(Database.credentials), Database.options)

      println("read-preference : " + connection.getReadPreference) //secondary
      println("read-preference slaveOk: " + connection.getReadPreference.isSlaveOk) //true

      val db: MongoDatabase = connection.getDatabase("events")
      val logs = db.getCollection("MessageEvents")
      val events = messageLog.distinct("event", classOf[String]).iterator()

      //
}

但我不断收到以下Exception authenticating MongoCredential错误:

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=secondary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=staging-node3.shaharma.com:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='userId', source='events', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server staging-node3.shaharma.com:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]]]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:280)
        at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:206)
        at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160)
        at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188)
        at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:98)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100)
        at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344)
        at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343)
        at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32)
Caused by: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=secondary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=staging-node3.shaharma.com:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='userId', source='events', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server staging-node3.shaharma.com:27017. The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}]
        at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:369)
        at com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101)
        at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:75)
        at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.<init>(ClusterBinding.java:71)
        at com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63)
        at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:210)
        at com.mongodb.operation.DistinctOperation.execute(DistinctOperation.java:155)
        at com.mongodb.operation.DistinctOperation.execute(DistinctOperation.java:61)
        at com.mongodb.Mongo.execute(Mongo.java:773)
        at com.mongodb.Mongo$2.execute(Mongo.java:760)

我可以使用mongo命令行

成功连接到服务器
mongo --host staging-node3.shaharma.com --username userId --password userP@ssword --authenticationDatabase events
rs.slaveOk(true)

我使用scram-sha-1进行身份验证。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用MongoCredential.createCredential()代替MongoCredential.createScramSha1Credential并检查是否有效吗?