2

I've just created a very basic Spring Boot project using Spring Tool Suite with mongoDB-reactive dependency and run the app but, I keep getting the following exceptions

2019-11-27 00:31:19.699 INFO 11988 --- [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.netty.NettyStream$OpenChannelFutureListener.operationComplete(NettyStream.java:410) ~[mongodb-driver-core-3.11.2.jar:na] at

I am using Spring Boot (2.2.1.RELEASE)

Inside applications.properties I've added the following line

spring.data.mongodb.uri=mongodb://localhost:27017/testdb

What am I missing?

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
rakesh mehra
  • 618
  • 1
  • 9
  • 21

4 Answers4

0

MongoSocketOpenException occurs when the spring boot app cannot connect to mongo db using the given connection details (or default localhost and 27017 port)

Check whether the MongoDB instance is running using mongo shell if fails check for mongo db server config for listening interface ip (to enable listening to all interfaces - use net config as

net: bindIp: 127.0.0.1 port: 27017

ashanshamika
  • 137
  • 4
0

If you want embedded mongo, add the dependency https://mvnrepository.com/artifact/de.flapdoodle.embed/de.flapdoodle.embed.mongo

Alternately you can easily install mongo instance on your local.

Prashant Thorat
  • 1,752
  • 11
  • 33
  • 54
0

I just noticed that the dependency I added is test scoped. Things have worked after removing it.

    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <version>2.2.0</version>
    </dependency>
rakesh mehra
  • 618
  • 1
  • 9
  • 21
0

In my case I was trying to connect to mongo service present in k8s, from other sprint boot app, I understood that SpringBoot has some auto configure features it was trying to connect to localhost:27017 by default during app start up, to skip it I had to exclude it in application yaml: spring.autoconfigure.exclude:org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration

Shiva
  • 1