MongoDB试图连接到localhost,为什么?

ade*_*aud 3 java mongodb

我目前正在开发一个连接到远程MongoDB数据库的Java应用程序.

我已经实现了使用mongo指南的身份验证方法:

MongoCredential credential = MongoCredential.createScramSha1Credential(username, credentialDatabase, password.toCharArray());
MongoClient client = new MongoClient(new ServerAddress(hostname, port), Arrays.asList(credential));
mongoDatabase = client.getDatabase(database);
Run Code Online (Sandbox Code Playgroud)

应用程序正确连接到数据库,但有一件事我无法理解.它连接到远程服务器,但我不知道为什么它尝试连接到localhost:27017.

2016-03-07 16:13:29.662  INFO 12507 --- [*.*.*:25015] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:29}] to *.*.*.*:25015

2016-03-07 16:13:29.687  INFO 12507 --- [*.*.*:25015] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=*.*.*.*:25015, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 3]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=24485426}


2016-03-07 16:13:30.062  INFO 12507 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}


2016-03-07 16:13:30.220  INFO 12507 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
Run Code Online (Sandbox Code Playgroud)

那么,我怎么能告诉它我不想连接到localhost?

谢谢

小智 7

您可以通过在spring boot上添加以下注释来排除Mongo自动连接/(localhost:27017)Application.java.

@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
public class Application {
    // ...
}
Run Code Online (Sandbox Code Playgroud)

  • 现在您将参数放入@SpringbootApplication注释中。对于 Reactive Mongo,还有另一个自动配置类 - ```@SpringBootApplication(exclusion = {MongoAutoConfiguration.class, MongoReactiveAutoConfiguration.class})``` (2认同)