简体   繁体   中英

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. 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?

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.

I'm using 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:

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. 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. Thanks.

Here is the standard 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. It will be necessary to adjust the bound host/port in the 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.

In the /etc/mongod.conf, allow the bindIp to have more than just 127.0.0.1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM