Exception in monitor thread when connecting to localhost server: 27017 when accessing MongoDB with Java

I have the following exception when starting a Java application for MongoDB:

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

The call stack is as 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 

None of these names apply to my application. Also I do not have a MONGODB server on the local host. I use a remote host and install it later. An exception occurs before any of my statements regarding Mongo.

UPDATE

This is probably part of Spring provided beans access to Mongo . How to disable them?

My config contains the 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 deleted org.springframework.boot:spring-boot-starter-data-mongodb and thought that Mongo would use it myself ...

UPDATE2

I found a related question: How to disable autoconfiguration spring -data-mongodb in spring-boot

+9
source share
4 answers

I had to add an exception annotation to my main annotated class,

i.e. instead

 @SpringBootApplication 

I had to

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

try adding

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

in application.properties. if mongo does not work on localhost, this should fix the problem.

+2
source

Your server seems to be down. Also, if it works, it does it on another port.

0
source

I can’t say for sure. This is not an informational question.

I can say that: the Mongo driver, by default, tries to connect to the local host. You may not have specified the Mongo host / port.

Therefore, you will have to configure the host / port / MongoDB credentials (if you have any).

Maybe this is a network-related problem or a firewall (try connecting to MongoDB directly from your computer using Cli / even using a basic program that uses only the Mongo driver).

From the stack trace, I don't see any use of Spring, so more information is needed to say for sure.

In general, you can analyze dependencies in Gradle using the gradle dependencies command (see here ).

0
source

Source: https://habr.com/ru/post/1243071/


All Articles