Exception in monitor thread while connecting to server localhost:27017 while accessing MongoDB with Java
Asked Answered
B

13

42

I have the following exception when running Java app for MongoDB:

[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.

UPDATE

This is probably some Spring provided beans accessing Mongo. How to disable them?

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...

UPDATE2

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

Brendanbrenden answered 15/2, 2016 at 17:19 Comment(0)
B
69

I was to add exclusion annotation to my main annotated class,

i.e. instead of

@SpringBootApplication

I should have

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
Brendanbrenden answered 15/2, 2016 at 18:5 Comment(6)
you are god, bro.Overlie
Martin check this postTamratamsky
if above method is not working, you can try this https://mcmap.net/q/391580/-mongo-tries-to-connect-automatically-to-port-27017-localhostMusket
@Brendanbrenden what does @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class}) do?Thorite
@Brendanbrenden as of today it should be @SpringBootApplication(exclude={MongoAutoConfiguration.class})Jules
@Brendanbrenden since @SpringBootApplication is already decorated via @EnableAutoConfiguration you just have to add that exclusion to @SpringBootApplicationFeathercut
T
8

Try adding

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

into application.properties.

If Mongo is not running on localhost, this should fix the issue.

Tweeze answered 28/3, 2018 at 8:16 Comment(1)
Had a similar issue, setting these properties solved my problem, Thanks !!!Salmi
M
7

My IDEA suggested that I do this.

@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
Matter answered 23/7, 2021 at 4:18 Comment(0)
I
3

In my case the mistake was not to have authorized my development environment. So I allowed connections from any IP (not a good idea for prod env) by adding the rule 0.0.0.0/0. You add this rule from the Network Access section from the atlas dashboard.

Impossible answered 24/12, 2021 at 19:9 Comment(1)
to add to this, had to switch my network due to which the IP address got changed. check if your IP address is added for your MongoDB cluster. (Adding 0.0.0.0/0 will also do.)Silhouette
T
2

For future searchers.

In my case this was caused by the Mongo server not accepting connections from my IP. I had to whitelist it in MongoDB console.

Twirl answered 18/11, 2023 at 21:59 Comment(0)
S
1

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.

So you'll have to configure the MongoDB host/port/credentials (if you have those).

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).

From the stacktrace I don't see any usage of Spring, so more information is required to say for sure.

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

Safeguard answered 15/2, 2016 at 17:45 Comment(0)
H
1

In my case mongoDB internal port was not mapped to external. Using command docker run -p 27017:27017 -d mongo resolved my issue.

Haemo answered 1/6, 2022 at 11:3 Comment(0)
C
0

I had the same exception when trying to connect MongoDB with spring-boot. In my case, I forgot to add the @Configuration annotation in the MongoDB Configuration class I created.

After adding this it worked for me.

Cabasset answered 9/4, 2021 at 15:54 Comment(0)
F
0

I changed the scope(It was Test before)

<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<scope>4.2.3</scope>
</dependency>
Folderol answered 23/6, 2021 at 7:23 Comment(0)
C
0

check your mongo with cmd "mongo" if mongo is deathed follow step 1: open search windown 2: search "services" 3: find MongoDB Service and right click then click start on popup

Cornucopia answered 13/12, 2022 at 3:43 Comment(0)
G
0

In my case, the fix is:

@EnableAutoConfiguration(exclude={MongoReactiveAutoConfiguration.class})
Grooms answered 7/9, 2023 at 10:17 Comment(0)
F
0

If you don't want to disable AutoConfig as mentioned before and want to connect by passing the URI and database, try this inside application.properties:

spring.data.mongodb.uri=mongodb://username:password@hostIpOnWhichMongoIsRunning:27017
spring.data.mongodb.database=yourDatabaseName
Farnesol answered 19/6, 2024 at 16:9 Comment(0)
P
-3

It seems your server is not running. Also if it running, it is doing so on a different port.

Portis answered 15/2, 2016 at 17:36 Comment(4)
My server is running, but not on localhostBrendanbrenden
Check your address then.Portis
I checked. It is not localhost.Brendanbrenden
Try printing out the address. As per the error trace the code is reaching out to localhost.Portis

© 2022 - 2025 — McMap. All rights reserved.