40

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

Community
  • 1
  • 1
Dims
  • 47,675
  • 117
  • 331
  • 600

10 Answers10

68

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

i.e. instead of

@SpringBootApplication

I should have

@SpringBootApplication
@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
Dims
  • 47,675
  • 117
  • 331
  • 600
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.

pinckerman
  • 4,115
  • 6
  • 33
  • 42
Vaibhav
  • 103
  • 1
  • 7
6

My IDEA suggested that I do this.

@SpringBootApplication(exclude = {MongoAutoConfiguration.class})
aszswaz
  • 609
  • 3
  • 10
2

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.

Saliou673
  • 209
  • 2
  • 10
  • 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.) – kaustubhd9 Mar 18 '23 at 12:28
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).

halfer
  • 19,824
  • 17
  • 99
  • 186
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
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.

Pulsara Sandeepa
  • 915
  • 10
  • 25
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>
Slava Rozhnev
  • 9,510
  • 6
  • 23
  • 39
Anisha
  • 1
0

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

Shashi Ranjan
  • 87
  • 1
  • 10
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

-3

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

rootExplorr
  • 575
  • 3
  • 17