连接到服务器本地时监视器线程中的异常主机:27017 使用 Java 访问 MongoDB 时

在为MongoDB运行Java应用程序时,我有以下异常:

[localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017 while accessing MongoDB with Java

调用堆栈如下:

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.0.4.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongodb-driver-core-3.0.4.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127) ~[mongodb-driver-core-3.0.4.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_45]
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_45]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_45]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_45]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_45]
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_45]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_45]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_45]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) ~[mongodb-driver-core-3.0.4.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.0.4.jar:na]
    ... 3 common frames omitted

这些名称都不属于我的应用程序。另外,我在本地主机上没有MONGODB服务器。我正在使用远程主机并在以后进行设置。在我关于Mongo的任何陈述之前,都会发生一个例外。

更新

这可能是一些提供的bean访问。如何禁用它们?SpringMongo

我的配置包含以下依赖项:

dependencies {
    compile('javax.media:jai_core:1.1.3')
    //compile('jai_core:1.1.3')

//  compile('org.springframework.boot:spring-boot-starter-data-mongodb')
    compile('org.mongodb:mongodb-driver:3.0.4')
    compile('org.mongodb:bson:3.0.4')

    compile('org.geotools:gt-api:14.2')
    compile('org.geotools:gt-shapefile:14.2')
    compile('org.geotools:gt-geometry:14.2')
    compile('org.geotools:gt-referencing:14.2')
    compile('org.geotools:gt-geojson:14.2')
    compile('org.geotools:gt-mongodb:14.2')

    compile('org.springframework.boot:spring-boot-starter-web')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

即我已经删除并认为将使用自己...org.springframework.boot:spring-boot-starter-data-mongodbMongo

更新2

我发现相关的问题:如何在spring-boot中禁用spring-data-mongodb自动配置


答案 1

我要将排除注释添加到我的主要注释类中,

即代替

@SpringBootApplication

我应该有

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})

答案 2

尝试添加

spring.data.mongodb.host=hostIpOnWhichMongoIsRunning
spring.data.mongodb.port=27017

到。application.properties

如果Mongo未在本地主机上运行,这应该可以解决问题。