在Scala中连接到MongoDB

时间:2018-07-10 00:55:13

标签: mongodb scala reactivemongo

我试图通过我在播放框架文档中找到的ReactiveMongo依赖项/库将我的Pl​​ay-scala应用程序连接到mongo db。

我已经在conf文件中添加了这样的

play.modules.enabled += "play.modules.reactivemongo.ReactiveMongoModule"
mongodb.uri = "mongodb://ege:ege123@localhost:27017/smug-studio"

但是在运行我的应用程序后,当我尝试通过邮递员api访问数据库时,会引发类似

的异常
{
    "Code": 0,
    "ERROR": "MongoError['Connection is missing metadata (like protocol version, etc.) The connection pool is probably being initialized.']"
}

在日志中我可以找到这样的

[warn] r.c.a.MongoDBSystem - [Supervisor-1/Connection-2] Error while processing getNonce response #1000
[error] r.c.a.MongoDBSystem - [Supervisor-1/Connection-2] Authentication failure
reactivemongo.core.commands.FailedAuthentication: BSONCommandError['Error while processing getNonce response #1000']
    at reactivemongo.core.actors.MongoScramSha1Authentication$$anonfun$authReceive$1.$anonfun$applyOrElse$5(MongoScramSha1Authentication.scala:46)
    at reactivemongo.core.actors.MongoDBSystem.$anonfun$authenticationResponse$1(MongoDBSystem.scala:1009)
    at reactivemongo.core.actors.MongoDBSystem.updateNodeSet(MongoDBSystem.scala:971)
    at reactivemongo.core.actors.MongoDBSystem.updateNodeSet$(MongoDBSystem.scala:965)
    at reactivemongo.core.actors.StandardDBSystem.updateNodeSet(MongoDBSystem.scala:1337)
    at reactivemongo.core.actors.MongoDBSystem.authenticationResponse(MongoDBSystem.scala:1002)
    at reactivemongo.core.actors.MongoDBSystem.authenticationResponse$(MongoDBSystem.scala:998)
    at reactivemongo.core.actors.StandardDBSystem.authenticationResponse(MongoDBSystem.scala:1337)
    at reactivemongo.core.actors.MongoScramSha1Authentication$$anonfun$authReceive$1.$anonfun$applyOrElse$1(MongoScramSha1Authentication.scala:46)
    at scala.util.Either.fold(Either.scala:189)
[warn] r.c.a.MongoDBSystem - [Supervisor-1/Connection-2] Error while processing getNonce response #1001
[error] r.c.a.MongoDBSystem - [Supervisor-1/Connection-2] Authentication failure
reactivemongo.core.commands.FailedAuthentication: BSONCommandError['Error while processing getNonce response #1001']
    at reactivemongo.core.actors.MongoScramSha1Authentication$$anonfun$authReceive$1.$anonfun$applyOrElse$5(MongoScramSha1Authentication.scala:46)
    at reactivemongo.core.actors.MongoDBSystem.$anonfun$authenticationResponse$1(MongoDBSystem.scala:1009)
    at reactivemongo.core.actors.MongoDBSystem.updateNodeSet(MongoDBSystem.scala:971)
    at reactivemongo.core.actors.MongoDBSystem.updateNodeSet$(MongoDBSystem.scala:965)
    at reactivemongo.core.actors.StandardDBSystem.updateNodeSet(MongoDBSystem.scala:1337)
    at reactivemongo.core.actors.MongoDBSystem.authenticationResponse(MongoDBSystem.scala:1002)
    at reactivemongo.core.actors.MongoDBSystem.authenticationResponse$(MongoDBSystem.scala:998)
    at reactivemongo.core.actors.StandardDBSystem.authenticationResponse(MongoDBSystem.scala:1337)
    at reactivemongo.core.actors.MongoScramSha1Authentication$$anonfun$authReceive$1.$anonfun$applyOrElse$1(MongoScramSha1Authentication.scala:46)
    at scala.util.Either.fold(Either.scala:189)

我应该怎么做才能解决此问题?

1 个答案:

答案 0 :(得分:1)

如果您可以在外壳上连接到mongo,则可能是因为您没有在URL上正确设置authModeauthMechanism

mongodb://ege:ege123@localhost:27017/smug-studio?authMechanism=SCRAM-SHA-1

mongodb://ege:ege123@localhost:27017/smug-studio?authMode=SCRAM-SHA-1

http://mongodb.github.io/mongo-java-driver/3.0/driver/reference/connecting/authenticating/