使用 Java 访问 MongoDB 时连接到服务器 localhost:27017 时监视器线程中的异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35415308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in monitor thread while connecting to server localhost:27017 while accessing MongoDB with Java
提问by Dims
I have the following exception when running Java app for MongoDB:
为 MongoDB 运行 Java 应用程序时出现以下异常:
[localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017 while accessing MongoDB with Java
Call stack is follows:
调用栈如下:
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
Neither of these names belong to my application. Also I have NO MONGODB server on local host. I am using remote host and setting it later. An exception occurs BEFORE any of my statements concerning Mongo.
这些名字都不属于我的申请。另外我在本地主机上没有 MONGODB 服务器。我正在使用远程主机并稍后进行设置。在我关于 Mongo 的任何陈述之前发生异常。
UPDATE
更新
This is probably some Spring
provided beans accessing Mongo
. How to disable them?
这可能是一些Spring
提供的 bean 访问Mongo
. 如何禁用它们?
My config contains following dependencies:
我的配置包含以下依赖项:
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')
}
i.e. I have removed org.springframework.boot:spring-boot-starter-data-mongodb
and was thinking will use Mongo
myself...
即我已经删除org.springframework.boot:spring-boot-starter-data-mongodb
并正在考虑将使用Mongo
我自己......
UPDATE2
更新2
I found related question: How to disable spring-data-mongodb autoconfiguration in spring-boot
我发现了相关问题:How to disable spring-data-mongodb autoconfiguration in spring-boot
采纳答案by Dims
I was to add exclusion annotation to my main annotated class,
我要在我的主要注释类中添加排除注释,
i.e. instead of
即代替
@SpringBootApplication
I should have
我应该
@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
回答by rootExplorr
It seems your server is not running. Also if it running, it is doing so on a different port.
您的服务器似乎没有运行。此外,如果它正在运行,它会在不同的端口上运行。
回答by Mark Bramnik
I can't say for sure. It's kind of not enough information question.
我不能肯定地说。这是一种信息不够的问题。
I can say that: Mongo driver by default tries to connect to the local host. Probably you haven't specified the Mongo host/port.
我可以这样说:默认情况下,Mongo 驱动程序会尝试连接到本地主机。可能您还没有指定 Mongo 主机/端口。
So you'll have to configure the MongoDB host/port/credentials (if you have those).
所以你必须配置 MongoDB 主机/端口/凭据(如果你有的话)。
Maybe it's network related issue or firewall (try to connect to MongoDB from your machine directly with cli / even running a basic program that uses only mongo driver).
也许是网络相关问题或防火墙(尝试使用 cli 直接从您的机器连接到 MongoDB/甚至运行仅使用 mongo 驱动程序的基本程序)。
From the stacktrace I don't see any usage of Spring, so more information is required to say for sure.
从堆栈跟踪中我没有看到 Spring 的任何用法,因此需要更多信息来确定。
In general you can analyze the dependencies in Gradle by using gradle dependencies
command (see here).
通常,您可以使用gradle dependencies
命令来分析 Gradle 中的依赖项(请参阅此处)。
回答by Vaibhav
Try adding
尝试添加
spring.data.mongodb.host=hostIpOnWhichMongoIsRunning
spring.data.mongodb.port=27017
into application.properties
.
进入application.properties
。
If Mongo is not running on localhost, this should fix the issue.
如果 Mongo 没有在 localhost 上运行,这应该可以解决问题。