Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Micronaut API连接到docker容器中运行的MongoDB?_Mongodb_Docker_Kotlin_Micronaut_Mongo Shell - Fatal编程技术网

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

如何将Micronaut API连接到docker容器中运行的MongoDB?,mongodb,docker,kotlin,micronaut,mongo-shell,Mongodb,Docker,Kotlin,Micronaut,Mongo Shell,我正在尝试将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{mech

我正在尝试将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>}
然后,我关闭了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
>任务:运行
17:51:14.930[主]信息io.micronaut.runtime.micronaut-启动在826ms内完成。正在运行的服务器:http://localhost:8080
17:51:15.037[pool-1-thread-1]INFO org.mongodb.driver.cluster-使用以下设置创建的群集{hosts=[localhost:27017],mode=SINGLE,requiredClusterType=UNKNOWN,serverSelectionTimeout='30000ms',maxWaitQueueSize=500}
17:51:15.055[pool-1-thread-1]信息c.ds.events.service.events服务-检查1573689074和1573692674之间的事件
17:51:15.065[cluster ClusterId{value='5dcc96f3a49ea7512bce9dec',description='null'}-localhost:27017]INFO org.mongodb.driver.connection-打开了到localhost:27017的连接[connectionId{localValue:1,serverValue:145}]
17:51:15.068[cluster ClusterId{value='5dcc96f3a49ea7512bce9dec',description='null'}-localhost:27017]INFO org.mongodb.driver.cluster-监视器线程成功连接到服务器,描述为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-关闭了到localhost:27017的[connectionId{localValue:2}]连接,因为此连接引发了套接字异常。
17:51:15.346[pool-1-thread-1]错误c.ds.events.service.EventsService-com.mongodb.MongoSecurityException:验证MongoCredential{mechanism=SCRAM-SHA-1,userName='admin',source='admin',password=,mechanismProperties=}的异常
17:51:15.346[pool-1-thread-1]信息类com.ds.events.jobs.EventsJob-时间范围内没有要发送到RabbitMQ的事件

所以我解决了这个问题。
27017
端口上运行的其他进程导致冲突。关闭端口上的所有进程并重新运行docker和Micronaut服务器解决了此问题。

您是如何启动容器的?(您是否将MongoDB服务器端口发布到主机?)以及,客户端是在另一个容器中运行还是直接在主机上运行?
docker run-d-p 27017-27019:27017-27019——命名MongoDB mongo
docker exec-it MongoDB bash
运行Mongoth此客户端直接在主机上运行
> 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