简体   繁体   English

尝试连接 Java Spring 应用程序到远程 MongoDB 时出现异常打开套接字,连接被拒绝

[英]Exception opening socket, connection refused, when trying to connect Java Spring Application to a remote MongoDB

I'm seeing a java dump as soon as I start the Spring boot project after attempting to configure MongoDB, so before I'm even adding any code.在尝试配置 MongoDB 之后,我在启动 Spring 引导项目后立即看到 java 转储,所以在我添加任何代码之前。 Does this mean Spring is attempting to make the connection as soon as the application starts, and not waiting for me to open a connection?这是否意味着 Spring 正在尝试在应用程序启动后立即建立连接,而不是等待我打开连接?

This is just a local test environment.这只是一个本地测试环境。 A windows host, where I'm running Spring in Eclipse, and MongoDB in on a local Ubuntu VM, which I can reach otherwise. A windows host, where I'm running Spring in Eclipse, and MongoDB in on a local Ubuntu VM, which I can reach otherwise.

I'm using Gradle, build.gradle我正在使用 Gradle、build.gradle

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.benW'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
    mavenCentral()
}

ext {
    mapstructVersion = "1.4.2.Final"
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-artemis'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'io.projectreactor:reactor-test'
    
    implementation("org.mapstruct:mapstruct:${mapstructVersion}")
    compileOnly "org.mapstruct:mapstruct-processor:${mapstructVersion}"
    annotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
    testAnnotationProcessor "org.mapstruct:mapstruct-processor:${mapstructVersion}"
    
    implementation('org.springframework.boot:spring-boot-starter-data-mongodb')
    testImplementation('de.flapdoodle.embed:de.flapdoodle.embed.mongo')
}

test {
    useJUnitPlatform()
}

And here is what I've added to my application.properties file:这是我添加到 application.properties 文件中的内容:

spring.data.mongodb.host=192.168.56.102
spring.data.mongodb.port=27017
spring.data.mongodb.database=k2_dev
spring.data.mongodb.username=test
spring.data.mongodb.password=password

logging.level.org.springframework.data.mongodb.core.MongoTemplate: DEBUG

I can log into the MangoDB instance.我可以登录到 MangoDB 实例。 I created the user.我创建了用户。

> show users
{
        "_id" : "k2_dev.test",
        "userId" : UUID("8e9e3085-2f33-41f4-8722-8b6c879efaed"),
        "user" : "test",
        "db" : "k2_dev",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "k2_dev"
                },
                {
                        "role" : "dbAdmin",
                        "db" : "k2_dev"
                }
        ],
        "mechanisms" : [
                "SCRAM-SHA-1",
                "SCRAM-SHA-256"
        ]
}

I also made sure the port is opened.我还确保端口已打开。

:~$ sudo lsof -i -P -n | grep LISTEN
systemd-r   647 systemd-resolve   13u  IPv4  22537      0t0  TCP 127.0.0.53:53 (LISTEN)
sshd        710            root    3u  IPv4  24052      0t0  TCP *:22 (LISTEN)
sshd        710            root    4u  IPv6  24054      0t0  TCP *:22 (LISTEN)
mongod    16172         mongodb   11u  IPv4 137179      0t0  TCP 127.0.0.1:27017 (LISTEN)

Any advice on what I'm doing wrong here would be appreciated.任何关于我在这里做错的建议将不胜感激。 I've gone through a number of guides and other stackoverflow questions with now luck.现在运气好,我已经阅读了许多指南和其他 stackoverflow 问题。 Thanks.谢谢。

Here is the standard output:这是标准的 output:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.5)

2021-04-27 22:11:47.805  INFO 34344 --- [           main] c.b.R.RestClientPocApplication           : Starting RestClientPocApplication using Java 11.0.9 on PSPLT-F7VYYY2 with PID 34344 (C:\Users\Bwarrick\Workspaces\Java\RESTClientPOC\bin\main started by Bwarrick in C:\Users\Bwarrick\Workspaces\Java\RESTClientPOC)
2021-04-27 22:11:47.809  INFO 34344 --- [           main] c.b.R.RestClientPocApplication           : No active profile set, falling back to default profiles: default
2021-04-27 22:11:48.348  INFO 34344 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode.
2021-04-27 22:11:48.360  INFO 34344 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 9 ms. Found 0 MongoDB repository interfaces.
2021-04-27 22:11:49.890  INFO 34344 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[192.168.56.102:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-04-27 22:11:51.222  INFO 34344 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
2021-04-27 22:11:51.412  INFO 34344 --- [           main] c.b.R.RestClientPocApplication           : Started RestClientPocApplication in 3.978 seconds (JVM running for 5.351)
2021-04-27 22:11:51.946  INFO 34344 --- [68.56.102:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server 192.168.56.102:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.1.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.1.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.1.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.2.jar:na]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.ConnectException: Connection refused: connect
    at java.base/java.net.PlainSocketImpl.waitForConnect(Native Method) ~[na:na]
    at java.base/java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:107) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403) ~[na:na]
    at java.base/java.net.Socket.connect(Socket.java:608) ~[na:na]
    at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:78) ~[mongodb-driver-core-4.1.2.jar:na]
    at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.1.2.jar:na]
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.1.2.jar:na]
    ... 4 common frames omitted

The problem is that the mongo db is listening on localhost on the VM.问题是 mongo db 正在 VM 上的 localhost 上侦听。 It will be necessary to adjust the bound host/port in the VM.有必要调整 VM 中的绑定主机/端口。

$ sudo lsof -i -P -n | grep LISTEN
systemd-r   647 systemd-resolve   13u  IPv4  22537      0t0  TCP 127.0.0.53:53 (LISTEN)
sshd        710            root    3u  IPv4  24052      0t0  TCP *:22 (LISTEN)
sshd        710            root    4u  IPv6  24054      0t0  TCP *:22 (LISTEN)
mongod    16172         mongodb   11u  IPv4 137179      0t0  TCP 127.0.0.1:27017 (LISTEN)

Note that the Listen port for mongod is 127.0.0.1:27017 on the VM.请注意,mongod 的侦听端口是 VM 上的127.0.0.1:27017

In the /etc/mongod.conf, allow the bindIp to have more than just 127.0.0.1 .在 /etc/mongod.conf 中,允许bindIp不只是127.0.0.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法连接到远程VM。 拒绝连接。 尝试在Flash Builder 4.7中调试远程Java应用程序时 - Failed to connect to remote VM. Connection refused. when trying to debug remote java application in Flash Builder 4.7 连接被拒绝:connect,java socket - Connection refused: connect, java socket 连接拒绝尝试连接 spring 在 docker 启动应用程序 - Connection refused trying to connect spring boot application in docker 套接字连接异常说连接被拒绝 - Socket connect exception saying connection refused 连接拒绝尝试连接到 mongoDB docker 实例 - Connection refused trying to connect to mongoDB docker instance 线程“ main”中的异常java.net.ConnectException:连接被拒绝:connect套接字编程Java - Exception in thread “main” java.net.ConnectException: Connection refused: connect Socket Programming Java MongoDB 连接被拒绝 java spring 启动 - MongoDB connection refused java spring boot 尝试与外部IP连接时连接被拒绝 - Connection refused when trying to connect with an external IP 异常:连接被拒绝:连接 - exception: Connection refused: connect 从 Spring App (docker-compose) 获取 Mongodb 连接上的“异常打开套接字” - Getting “Exception opening socket” on Mongodb connection from Spring App (docker-compose)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM