如何将Micronaut API连接到在Docker容器中运行的MongoDB?

我正在尝试将micronaut API服务连接到在Docker容器上运行的MongoDB。我按照本指南中的步骤为MongoDB创建身份验证,但是在micronaut应用程序中创建客户端时,它说:

17:51:15.346 [pool-1-thread-1] ERROR c.ds.events.service.EventsService - com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1,username='admin',source='admin',password=<hidden>,mechanismProperties=<hidden>}

我在docker中提取了最新的mongo映像并启动了服务,并创建了这样的用户:

use admin
db.createUser(
  {
    user: "admin",pwd: "admin",roles: [
       { role: "userAdminAnyDatabase",db: "admin" },]
  }
)

然后我关闭mongo实例并使用身份验证凭据重新登录:

mongo -u admin -p admin --authenticationDatabase admin

我能够成功登录,并且可以将数据插入db中。但是,当尝试通过micronaut连接到它时,会出现身份验证错误。

这是我的micronaut API应用的摘要:

    @Value("\${MONGO_PASS}")
    var pass: String = "admin"

    @Value("\${MONGO_USER}")
    var user: String = "admin"

    @Value("\${MONGO_HOST}")
    var host: String = "localhost"

    @Value("\${MONGO_PORT}")
    var port: String = "27017"

    @Value("\${MONGO_DB_NAME}")
    var database: String = "admin"

    @Value("\${MONGO_COLLECTION}")
    var collectionName: String = "events"

    init {
        val client = KMongo.createclient(
                ServerAddress(host,port.toInt()),listOf(MongoCredential.createCredential(
                        user,database,pass.toCharArray()
                )),MongoClientOptions.builder().build())
        val database = client.getDatabase(database)
        collection = database.getcollection(collectionName)
    }

我没有设置任何环境变量,因此应使用默认值。以下是运行该服务后的完整日志。

> Task :run
17:51:14.930 [main] INFO  io.micronaut.runtime.micronaut - Startup completed in 826ms. Server Running: http://localhost:8080
17:51:15.037 [pool-1-thread-1] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017],mode=SINGLE,requiredClusterType=UNKNOWN,serverSelectionTimeout='30000 ms',maxWaitQueueSize=500}
17:51:15.055 [pool-1-thread-1] INFO  c.ds.events.service.EventsService - checking for events between 1573689074 and 1573692674
17:51:15.065 [cluster-ClusterId{value='5dcc96f3a49ea7512bce9dec',description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:1,serverValue:145}] to localhost:27017
17:51:15.068 [cluster-ClusterId{value='5dcc96f3a49ea7512bce9dec',description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017,type=STANDALONE,state=CONNECTED,ok=true,version=ServerVersion{versionList=[4,2,1]},minWireversion=0,maxWireversion=8,maxDocumentSize=16777216,logicalSessionTimeoutMinutes=30,roundTripTimeNanos=1864800}
17:51:15.346 [pool-1-thread-1] INFO  org.mongodb.driver.connection - Closed connection [connectionId{localValue:2}] to localhost:27017 because there was a socket exception raised by this connection.
17:51:15.346 [pool-1-thread-1] ERROR c.ds.events.service.EventsService - com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1,mechanismProperties=<hidden>}
17:51:15.346 [pool-1-thread-1] INFO  class com.ds.events.jobs.EventsJob - No events in the time range to be sent to RabbitMQ
WOJIAXIAOMIAN3 回答:如何将Micronaut API连接到在Docker容器中运行的MongoDB?

所以我找出了问题所在。 27017端口上运行的其他进程正在引起冲突。杀死端口上的所有进程,然后重新运行docker和Micronaut服务器,即可解决此问题。

本文链接:https://www.f2er.com/3106069.html

大家都在问