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

问题描述 投票:0回答:1

我正在尝试将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

我能够成功登录,并且可以将数据插入数据库。但是,当尝试通过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, userName='admin', source='admin', password=<hidden>, 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
mongodb docker kotlin micronaut mongo-shell
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.