[英]MongoDB trying to connect to localhost, Why?
I am currently developing a Java application connected to a remote MongoDB databse.我目前正在开发一个连接到远程 MongoDB 数据库的 Java 应用程序。
I have implemented the authentication methods fallowing the mongo guides:我已经实现了 mongo 指南中的身份验证方法:
MongoCredential credential = MongoCredential.createScramSha1Credential(username, credentialDatabase, password.toCharArray());
MongoClient client = new MongoClient(new ServerAddress(hostname, port), Arrays.asList(credential));
mongoDatabase = client.getDatabase(database);
The app connect properly to the database but there is a thing i can't understand.It connects well to the remote server,but I don't know why it try to connect to localhost:27017.该应用程序正确连接到数据库,但有一点我无法理解。它可以很好地连接到远程服务器,但我不知道它为什么尝试连接到 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
So, how can I tell it I don't want to connect to localhost ?那么,我怎么能告诉它我不想连接到 localhost 呢?
Thanks谢谢
You can exclude Mongo auto connect/(localhost:27017) by adding below annotation on spring boot Application.java
.您可以通过在 spring boot
Application.java
上添加以下注释来排除 Mongo auto connect/(localhost:27017)。
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
public class Application {
// ...
}
I'm not sure if this will help.我不确定这是否会有所帮助。
If you are using SpringBoot 1.4 and you will not have a bean for MongoClient in the context auto configuration will create MongoClient using default configuration.如果您使用的是 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;
}
...
So you have 3 options:所以你有3个选择:
spring.data.mongodb.host= spring.data.mongodb.port= spring.data.mongodb.database= spring.data.mongodb.username= spring.data.mongodb.password=
spring.data.mongodb.host= spring.data.mongodb.port= spring.data.mongodb.database= spring.data.mongodb.username= spring.data.mongodb.password=
这是 Spring Boot 的自动配置启动。您可以通过像这样扩展@SpringBootApplication
注释来禁用它:
@SpringBootApplication(exclude={MongoAutoConfiguration.class})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.