Usman Khaliq
Usman Khaliq

Reputation: 363

Spring Boot Showing "Connection Refused" error when connecting with mongoDB

I am setting up MongoDB with a Spring Boot project. However, when I compile my app via gradle and run the jar, it throws the following error:

2018-03-26 16:17:00.634  INFO 9321 --- [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.SocketStream.open(SocketStream.java:62) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:114) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_121]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_121]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_121]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_121]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_121]
        at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_121]
        at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:59) ~[mongodb-driver-core-3.6.3.jar!/:na]
        at com.mongodb.connection.SocketStream.open(SocketStream.java:57) ~[mongodb-driver-core-3.6.3.jar!/:na]
        ... 3 common frames omitted

Before running the jar, I already have mongodb running on my mac via mongod, and it shows the following message on terminal I NETWORK [initandlisten] waiting for connections on port 27017

Also, my applications.properties file for the spring project is blank. Any leads on how to go about diagnosing this issue would be appreciated!

Upvotes: 0

Views: 5103

Answers (1)

Usman Khaliq
Usman Khaliq

Reputation: 363

After almost one day of debugging, the Spring Boot project is linking successfully with the mongodb installation on localhost. Still not sure what exactly caused the issue, but here is how I debugged it:

1) Run mongodb by binding the instance to all interfaces:

sudo mongod —bind_ip_all  

2) I structured the configuration file of my application as follows:

@Configuration
@EnableAutoConfiguration(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
@ComponentScan
public class AppConfiguration {
} 

This configuration file was then read into the main application file:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(AppConfiguration.class, args);
    }
}

And finally, leave the application.properties file blank.

Upvotes: 1

Related Questions