3

I am trying to connect my java bot to Mongo Atlas but facing this error

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused: no further information}}]

When I set up a local database (mongo shell -> mongod command) then it gets connected to it and everything is operating normally. But when I shut down the local database then it shows this error.

My credentials are also correct as when I use the same link to connect the Mongo Compass to Atlas, it gets connected but when I use the link to connect to the mongo atlas in the Main program then the above error is shown.

In error also it is showing address= localhost:27017

 final String uri = "mongodb+srv://<censored>:<censored>@cluster0.7wsis.mongodb.net/DB?retryWrites=true&w=majority";
    MongoClient mongoClient = MongoClients.create(uri);

This is the code I m using to connect. I am sure that the credentials are correct.

Java mongo driver version used - 4.2.3 (sync)

Can anyone please help me figure this out stuck on this one for quite some time.

build.gradle -

plugins {
id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
    center()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
    compile 'org.mongodb:mongodb-driver-sync:4.2.3'
    compile 'net.dv8tion:JDA:4.2.0_247'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', 
    version: '1.1.1'
}

test {
    useJUnitPlatform()
}

edit- I tried to connect to mongo atlas using same link(copy paste) with help of pymongo and everything is good. This means I don't have any network or blocked port problem and my link, as well as credentials, are correct. Problem should lie in the code. But if there was problem in the code, why does it work with local database in first place? Is there any dependency extra from the ones added above that I should add in order for the java driver to understand the link.

Aman Kumar
  • 58
  • 1
  • 7

0 Answers0