当前位置: 首页 > 知识库问答 >
问题:

Mongo尝试自动连接到端口27017(localhost)

祖浩淼
2023-03-14

我在Maven中使用SpringREST和Mongo来创建一个连接到服务器的web服务。问题是我还没有为Mongo编写任何代码,它试图连接到localhost,这给我带来了一个MongoSocketOpenException。我所写的唯一代码是从main开始的两行代码。这是stacktrace:

2015-12-22 12:46:43.193  INFO 5720 --- [           main] fhirepsos.ws.Server                      : Starting Server on HarisPC with PID 5720 (C:\Users\Haris\workspace\FHIRtoepSOSConversion\target\classes started by Haris in C:\Users\Haris\workspace\FHIRtoepSOSConversion)
2015-12-22 12:46:43.197  INFO 5720 --- [           main] fhirepsos.ws.Server                      : No profiles are active
2015-12-22 12:46:43.260  INFO 5720 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31f924f5: startup date [Tue Dec 22 12:46:43 EET 2015]; root of context hierarchy
2015-12-22 12:46:44.007  INFO 5720 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-12-22 12:46:44.735  INFO 5720 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9001 (http)
2015-12-22 12:46:44.750  INFO 5720 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2015-12-22 12:46:44.751  INFO 5720 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.28
2015-12-22 12:46:44.865  INFO 5720 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2015-12-22 12:46:44.866  INFO 5720 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1610 ms
2015-12-22 12:46:45.161  INFO 5720 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2015-12-22 12:46:45.166  INFO 5720 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2015-12-22 12:46:45.166  INFO 5720 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2015-12-22 12:46:45.167  INFO 5720 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2015-12-22 12:46:45.167  INFO 5720 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2015-12-22 12:46:45.393  INFO 5720 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31f924f5: startup date [Tue Dec 22 12:46:43 EET 2015]; root of context hierarchy
2015-12-22 12:46:45.467  INFO 5720 --- [           main] 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)
2015-12-22 12:46:45.468  INFO 5720 --- [           main] 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)
2015-12-22 12:46:45.499  INFO 5720 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-22 12:46:45.499  INFO 5720 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-22 12:46:45.539  INFO 5720 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2015-12-22 12:46:45.726  INFO 5720 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2015-12-22 12:46:45.796  INFO 5720 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2015-12-22 12:46:45.874  INFO 5720 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9001 (http)
2015-12-22 12:46:45.878  INFO 5720 --- [           main] fhirepsos.ws.Server                      : Started Server in 3.033 seconds (JVM running for 3.383)
2015-12-22 12:46:46.764  INFO 5720 --- [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:63) ~[mongo-java-driver-3.2.0.jar:na]
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongo-java-driver-3.2.0.jar:na]
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongo-java-driver-3.2.0.jar:na]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_65]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_65]
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_65]
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_65]
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_65]
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_65]
    at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_65]
    at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_65]
    at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_65]
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) ~[mongo-java-driver-3.2.0.jar:na]
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongo-java-driver-3.2.0.jar:na]
    ... 3 common frames omitted  

这是我的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>fhirepsos.ws</groupId>
  <artifactId>FHIRtoepSOSConversion</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>FHIR to epSOS Conversion</name>
  <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>1.3.0.RELEASE</version>
  </parent>
  <dependencies>
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-web</artifactId>
   </dependency> 
   <dependency>
       <groupId>org.mongodb</groupId>
       <artifactId>mongo-java-driver</artifactId>
       <version>3.2.0</version>
   </dependency>     
   <dependency>
       <groupId>ca.uhn.hapi.fhir</groupId>
       <artifactId>hapi-fhir-base</artifactId>
       <version>1.3</version>
   </dependency>
   <dependency>
       <groupId>ca.uhn.hapi.fhir</groupId>
       <artifactId>hapi-fhir-structures-dstu2</artifactId>
       <version>1.3</version>
   </dependency>        
  </dependencies>
  <properties>
      <java.version>1.8</java.version>
  </properties>
  <build>
      <plugins>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>
  <repositories>
      <repository>
          <id>spring-releases</id>
          <url>https://repo.spring.io/libs-release</url>
      </repository>
  </repositories>
  <pluginRepositories>
      <pluginRepository>
          <id>spring-releases</id>
          <url>https://repo.spring.io/libs-release</url>
      </pluginRepository>
  </pluginRepositories>  
</project>

最后,我必须声明,尽管存在异常,但程序运行良好

提前感谢!

共有3个答案

江浩慨
2023-03-14

当Mongo DB未运行时,Spring Boot会引发此异常。请确保MongoDB正在运行。启动Mongo DB后,它为我解决了。

李嘉胜
2023-03-14

在某些情况下,如果您使用反应式,您还需要删除MongoReactiveAutoConfiguration

spring:
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration
      - org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration
卫甫
2023-03-14

Spring Boot有一个叫做“自动配置”的功能。在这种情况下,只要在类路径上检测到 Mongo 驱动程序,就会使用指向 localhost:27017 的默认值激活 MongoAutoConfiguration。如果您不希望出现这种行为,现在可以为 MongoDB 配置属性(有关有效的属性键,请参阅 http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongodb)或禁用 MongoAutoConfiguration:

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
 类似资料:
  • 我只是补充道: 我的maven春装项目。我也有这个测试: 出于某种原因,当我使用 spring boot构建我的项目时,现在尝试连接到mongodb: 如果我只是删除测试或mongo依赖项,上面的mongo连接就不会启动。 但是在上面的测试中没有提到mongo,那么spring为什么要尝试启动一个mongo连接呢?

  • 问题内容: 我是nodeJS的新手,通过关注youtube上的预告片开始学习,直到我添加了connect函数(如果mongodb, 当我在cmd(节点启动应用)上运行代码时,出现以下错误, 有人可以解释一下我错过了哪一步吗?我的代码: 问题答案: 您必须先在系统中安装数据库服务器并启动它。 使用以下链接进行安装 https://docs.mongodb.com/manual/installatio

  • 当我尝试连接到AWS中设置的mongo副本时,我收到这个错误: slavenode: 27017:[Errno-2]名称或服务未知,ip-XXX-XX-XX-XX: 27017:[Errno-2]名称或服务未知 (其中XXX-XX...对应我的实际IP地址) 要连接的代码如下所示: (其中,在主PublicIP和从PublicIP中,我从AWS控制台获得了实际的IPv4公共IP) 我已经有一个副本

  • 我试图连接,以便在Derby中为一个类项目创建db,但遇到了以下问题:我从Derby官方网站下载了zip文件,目前我使用的是10.12.1.1版本,我已经执行了startNetwork命令,它向我显示了一个日志文件,该文件表示已准备好接受端口1527上的连接,但当我尝试使用该命令连接时(在ij中):connect

  • 我正在尝试使用自定义身份验证提供程序配置Spring Security性。但是,我想在服务中自动装配以处理实际的数据库身份验证。我已经验证它在没有这个自动装配bean的情况下工作(只是在提供程序中硬编码)。我也在使用SpringMVC与hibernate结合使用。我目前正在使用根配置(hibernate等)和webMVC使用java注释和Spring使用xml进行混合方法。我知道这是不赞成的,但我