Mongo connection remote address error

The remote address is clearly configured, but it is still connected to the localhost

Error log:

[localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket

terms of settlement:

1. Springboot startup class, remove these two configuration classes

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

2. Add mongoconfiguration.java configuration class, and then you can connect it. Next, enjoy mongotemplate

@Configuration
public class MongoConfiguration {
    @Value("${spring.data.mongodb.uri}")
    private String mongodbUri;

    @Value("${spring.data.mongodb.option.min-connection-per-host}")
    private Integer minConnectionPerHost;

    @Value("${spring.data.mongodb.option.max-connection-per-host}")
    private Integer maxConnectionPerHost;

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoClientOptions.Builder builder = new Builder();
        builder.minConnectionsPerHost(minConnectionPerHost);
        builder.connectionsPerHost(maxConnectionPerHost);
        final SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(new MongoClientURI(mongodbUri, builder));
        MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory);
        return mongoTemplate;
    }
}


Read More: