3

In my Spring-boot application, I was trying to use MongoDB which is in AWS instance. For that, I used the host and port number of the AWS Instance to connect it through Spring-boot

application.properties

server.port=8186 # MONGODB (MongoProperties) #spring.data.mongodb.host=localhost #spring.data.mongodb.port=27018 #spring.data.mongodb.database=educharge spring.data.mongodb.uri=mongodb://52.15.64.17:27017/educharge spring.data.mongodb.database=educharge 

when I run the application the program connect to localhost instead of host( 52.15.64.17)

Console output

 . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.6.RELEASE) 2017-09-16 03:10:08.402 INFO 9536 --- [ restartedMain] c.E.E.EduchargeApiApplication : Starting EduchargeApiApplication on Sankar with PID 9536 (G:\EduchargeAPI\EduchargeAPI\bin started by R Dinesh Kumar in G:\EduchargeAPI\EduchargeAPI) 2017-09-16 03:10:08.402 INFO 9536 --- [ restartedMain] c.E.E.EduchargeApiApplication : No active profile set, falling back to default profiles: default 2017-09-16 03:10:08.408 INFO 9536 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53b63b22: startup date [Sat Sep 16 03:10:08 IST 2017]; root of context hierarchy 2017-09-16 03:10:09.716 INFO 9536 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8186 (http) 2017-09-16 03:10:09.718 INFO 9536 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2017-09-16 03:10:09.718 INFO 9536 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.16 2017-09-16 03:10:09.750 INFO 9536 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-09-16 03:10:09.750 INFO 9536 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1342 ms 2017-09-16 03:10:09.816 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-09-16 03:10:09.826 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'metricsFilter' to: [/*] 2017-09-16 03:10:09.827 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-09-16 03:10:09.827 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-09-16 03:10:09.827 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-09-16 03:10:09.827 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-09-16 03:10:09.827 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'webRequestLoggingFilter' to: [/*] 2017-09-16 03:10:09.827 INFO 9536 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'applicationContextIdFilter' to: [/*] 2017-09-16 03:10:09.866 INFO 9536 --- [ restartedMain] org.mongodb.driver.cluster : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=50} 2017-09-16 03:10:10.071 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@53b63b22: startup date [Sat Sep 16 03:10:08 IST 2017]; root of context hierarchy 2017-09-16 03:10:10.088 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/showPost/{postid}],methods=[GET]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.showPost(int) 2017-09-16 03:10:10.089 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/{postid}/like/{userid}],methods=[GET]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.likePost(int,int) 2017-09-16 03:10:10.090 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/editPost],methods=[PUT]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.editPost(com.Educharge.EduchargeAPI.Model.Post) 2017-09-16 03:10:10.090 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/{postid}/comment/{userid}],methods=[PUT]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.commentPost(java.lang.String,int,int) 2017-09-16 03:10:10.090 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/createPost],methods=[POST]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.createPost(com.Educharge.EduchargeAPI.Model.Post) 2017-09-16 03:10:10.090 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/deletePost/{postid}],methods=[DELETE]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.deletePost(int) 2017-09-16 03:10:10.091 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Posts/{postid}/dislike/{userid}],methods=[GET]}" onto public com.Educharge.EduchargeAPI.Model.Post com.Educharge.EduchargeAPI.Controller.PostController.dislikePost(int,int) 2017-09-16 03:10:10.096 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Profiles/showProfile/{userid}],methods=[GET]}" onto public com.Educharge.EduchargeAPI.Model.Profile com.Educharge.EduchargeAPI.Controller.ProfileController.showProfile(int) 2017-09-16 03:10:10.097 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Profiles/createProfile],methods=[POST]}" onto public com.Educharge.EduchargeAPI.Model.Profile com.Educharge.EduchargeAPI.Controller.ProfileController.addProfile(com.Educharge.EduchargeAPI.Model.Profile) 2017-09-16 03:10:10.097 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Profiles/editProfile],methods=[PUT]}" onto public com.Educharge.EduchargeAPI.Model.Profile com.Educharge.EduchargeAPI.Controller.ProfileController.editProfile(com.Educharge.EduchargeAPI.Model.Profile) 2017-09-16 03:10:10.097 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/Profiles/deleteProfile/{userid}],methods=[DELETE]}" onto public com.Educharge.EduchargeAPI.Model.Profile com.Educharge.EduchargeAPI.Controller.ProfileController.deleteProfile(int) 2017-09-16 03:10:10.101 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2017-09-16 03:10:10.102 INFO 9536 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2017-09-16 03:10:10.118 INFO 9536 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-09-16 03:10:10.118 INFO 9536 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-09-16 03:10:10.141 INFO 9536 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-09-16 03:10:10.319 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/info || /info.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.321 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/heapdump || /heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException 2017-09-16 03:10:10.322 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/mappings || /mappings.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.323 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/configprops || /configprops.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.324 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/metrics/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String) 2017-09-16 03:10:10.324 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/metrics || /metrics.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.325 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/beans || /beans.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.325 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/dump || /dump.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.326 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/health || /health.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal) 2017-09-16 03:10:10.329 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/auditevents || /auditevents.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public org.springframework.http.ResponseEntity<?> org.springframework.boot.actuate.endpoint.mvc.AuditEventsMvcEndpoint.findByPrincipalAndAfterAndType(java.lang.String,java.util.Date,java.lang.String) 2017-09-16 03:10:10.330 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/env/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String) 2017-09-16 03:10:10.330 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/env || /env.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.332 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/loggers/{name:.*}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.get(java.lang.String) 2017-09-16 03:10:10.333 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/loggers/{name:.*}],methods=[POST],consumes=[application/vnd.spring-boot.actuator.v1+json || application/json],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.LoggersMvcEndpoint.set(java.lang.String,java.util.Map<java.lang.String, java.lang.String>) 2017-09-16 03:10:10.333 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/loggers || /loggers.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.334 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/trace || /trace.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.336 INFO 9536 --- [ restartedMain] o.s.b.a.e.mvc.EndpointHandlerMapping : Mapped "{[/autoconfig || /autoconfig.json],methods=[GET],produces=[application/vnd.spring-boot.actuator.v1+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke() 2017-09-16 03:10:10.367 INFO 9536 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2017-09-16 03:10:10.434 INFO 9536 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-09-16 03:10:10.443 INFO 9536 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0 2017-09-16 03:10:10.499 INFO 9536 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8186 (http) 2017-09-16 03:10:10.501 INFO 9536 --- [ restartedMain] c.E.E.EduchargeApiApplication : Started EduchargeApiApplication in 2.166 seconds (JVM running for 4203.36) 2017-09-16 03:10:10.873 INFO 9536 --- [127.0.0.1:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server 127.0.0.1:27017 com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:na] at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:na] at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:na] at java.lang.Thread.run(Unknown Source) [na:1.8.0_144] Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_144] at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_144] at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_144] at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_144] at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_144] at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_144] at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_144] at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_144] at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:na] at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:na] ... 3 common frames omitted 

and prints the Exception opening socket

Is there any other way to connect the remote MongoDB host server ?

0

2 Answers 2

4

If you are connecting to your remote Mongodb instance, have you checked 52.15.64.17 is pingable for you?

Based on Connecting to a MongoDB database:

spring.data.mongodb.uri=mongodb://user:[email protected]:12345,mongo2.example.com:23456/test 

Alternatively, You can mute the Springboot AutoConfiguration for mongoDB:

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class}) 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes Sir, It was responding when I tried to connect using MongoDB client
2

I had a similar problem using MongoDB and Embedded MongoDB, each one configured in a different spring profile.

In my case, the Spring was ignoring the MongoDB URI from my "production" profile and it always started the embedded mongoDb.

The problem was the EmbeddedMongoAutoConfiguration enabled. So, you can disable the Embedded MongoDB in a specific profile using the properties:

spring: autoconfigure: exclude: org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.