MongoDB试图连接到本地主机,为什么?

9

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

我已经按照MongoDB指南实现了身份验证方法:

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

应用程序能够正确连接到数据库,但有一件事我无法理解。它可以成功连接到远程服务器,但我不知道为什么它还试图连接到 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

所以,我怎样才能告诉它我不想连接到本地主机?谢谢。

你能发布 hostname 字符串吗? - Alvaro Silvino
这应该能回答你的问题。https://docs.mongodb.org/manual/reference/program/mongod/#bin.mongod - user2263572
主机名字符串是我的服务器的IP地址。 - adenaud
3个回答

13
您可以在Spring Boot的上添加以下注释来排除Mongo自动连接/(localhost:27017)。
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
public class Application {
    // ...
}

不确定为什么这个被踩了;这正是帮助我的信息。 - silent_h
2
现在你需要将参数放入@SpringbootApplication注解中。对于Reactive Mongo,还有另一个自动配置类 - @SpringBootApplication( exclude = {MongoAutoConfiguration.class, MongoReactiveAutoConfiguration.class}) - Zon

4

我不确定这样是否有所帮助。

如果您正在使用SpringBoot 1.4,并且在上下文中没有MongoClient bean,则自动配置将使用默认配置创建MongoClient。

@Configuration
---->@ConditionalOnClass(MongoClient.class)<----
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
public class MongoAutoConfiguration {
...
    @Bean
    ---->@ConditionalOnMissingBean<----
    public MongoClient mongo() throws UnknownHostException {
        this.mongo = this.properties.createMongoClient(this.options, this.environment);
        return this.mongo;
    }
...

所以你有三个选项:

  1. 排除自动配置的mongo。
  2. 将MongoClient公开为上下文中的bean。
  3. 使用SpringBoot / Mongo的默认配置方式,并依靠自动配置为您创建MongoClient:
  4. spring.data.mongodb.host = spring.data.mongodb.port = spring.data.mongodb.database = spring.data.mongodb.username = spring.data.mongodb.password =

3
这是 Spring Boot 的自动配置功能。您可以通过扩展您的 @SpringBootApplication 注解来禁用它,如下所示:
@SpringBootApplication(exclude={MongoAutoConfiguration.class})

4
这样做是正确的吗?在我的Spring Boot项目中,它仍然尝试连接到localhost:27017。 - Nguyễn Đức Tâm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接