"Exception opening socket" and "connect timed out" - How to connect Spring Boot to MongoDB on Remote Server



Hey there, fellow devs!❤

In this blog, you will only see my love for databases and servers, all backend cravings😌

So, for a heavy introduction, I am imagining that you have set up a Spring Boot application with MongoDB. When using MongoDB on localhost, everything functions perfectly. The issue is that you get "Exception opening socket" and "connect timed out" when you attempt to connect to MongoDB on a remote server.

Right? 😁

Of course! Because you might have didn't follow the below-mentioned steps😟: 

1. Add Equivalent MongoDB Dependencies

Here we have two options:

Maven

<dependency>
<parent>
<groupId>
    org.springframework.boot
</groupId>
<artifactId>
    spring-boot-starter-parent
</artifactId>
<version>
    3.1.5
</version>
</parent>
</dependency>

The spring-boot-starter-parent project is a unique beginning project that offers our application's default configurations together with a full dependency tree so that we may rapidly construct our Spring Boot project. Additionally, it offers pre-configured versions of the Maven plugins maven-jar, maven-surefire, maven-war, and maven-failsafe.

In addition, spring-boot-dependencies, the parent of spring-boot-starter-parent, passes on dependency management to it.

Gradle

implementation 'org.springframework.boot:spring-boot-starter-data-mongodb

2. Set up the connection properties for MongoDB

public class MongoConnectionApplicationLiveTest {
    private static final String HOST = "localhost";
    private static final String PORT = "20019";
    private static final String DB = "codegirl";
    private static final String USER = "admin";
    private static final String PASS = "password";

    // test cases
}

Put the MongoDB connection details in your application.properties or application.yml file. You will need to supply the hostname or IP address, port, username, and password for a remote server.

3. Java Configuration

Now, let's extend the AbstractMongoConfiguration base class for MongoDB configuration to generate a comparable setup using Java config:

@Configuration
public class MongoConfig extends AbstractMongoClientConfiguration {
 
    @Override
    protected String getDatabaseName() {
        return "test";
    }
 
    @Override
    public MongoClient mongoClient() {
        ConnectionString connectionString = new ConnectionString("mongodb://localhost:20019/test");
MongoClientSettings mongoClientSettings = MongoClientSettings.builder() .applyConnectionString(connectionString) .build(); return MongoClients.create(mongoClientSettings); } @Override public Collection getMappingBasePackages() { return Collections.singleton("com.codegirl"); } }

4. Specify Repositories for MongoDB

With the help of Spring Data MongoDB's repository support, create repositories to communicate with MongoDB collections.

import org.springframework.data.mongodb.repository.MongoRepository;

public interface UserRepository extends MongoRepository(User, String) {
    // Define custom queries here if needed
}

5. Use repositories and inject

Use the MongoDB repositories to communicate with the database by injecting them into your Spring components.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    // Use userRepository to perform CRUD operations
}

Happy Coding ✌

codegirl

Hello, I’m Sangita, person behind "codegirl", a dedicated web developer. Crafting digital experiences is not just my job; it’s my passion. Let’s build something exceptional together!

Post a Comment

Previous Post Next Post