title: Remove Default Localhost from Spring Data Mongo
body: <p><strong>PROBLEM</strong>
  Springboot application tries to connect local Mongo instead of a remote one.</p>
  <p><strong>RESEARCH</strong>
  A simple example application.</p>
  <p><code>pom.xml</code></p>
  <pre><code>        &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
      &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
        xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
        xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
        &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
        &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.5.5&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
        &lt;/parent&gt;
        &lt;groupId&gt;com.example&lt;/groupId&gt;
        &lt;artifactId&gt;mongo-test&lt;/artifactId&gt;
        &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
        &lt;name&gt;mongo-test&lt;/name&gt;
        &lt;description&gt;mongo-test&lt;/description&gt;
        &lt;properties&gt;
          &lt;java.version&gt;11&lt;/java.version&gt;
        &lt;/properties&gt;
        &lt;dependencies&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-data-mongodb-reactive&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-webflux&lt;/artifactId&gt;
          &lt;/dependency&gt;
      
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
            &lt;scope&gt;runtime&lt;/scope&gt;
            &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
            &lt;artifactId&gt;lombok&lt;/artifactId&gt;
            &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
            &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;io.projectreactor&lt;/groupId&gt;
            &lt;artifactId&gt;reactor-test&lt;/artifactId&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
        &lt;/dependencies&gt;
      
        &lt;build&gt;
          &lt;plugins&gt;
            &lt;plugin&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
              &lt;configuration&gt;
                &lt;excludes&gt;
                  &lt;exclude&gt;
                    &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                    &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                  &lt;/exclude&gt;
                &lt;/excludes&gt;
              &lt;/configuration&gt;
            &lt;/plugin&gt;
          &lt;/plugins&gt;
        &lt;/build&gt;
      
      &lt;/project&gt;
  </code></pre>
  <p><code>Application.java</code> class</p>
  <pre><code>  package com.example.mongotest;
    
    import com.mongodb.ConnectionString;
    import com.mongodb.MongoClientSettings;
    import com.mongodb.connection.netty.NettyStreamFactoryFactory;
    import com.mongodb.reactivestreams.client.MongoClient;
    import com.mongodb.reactivestreams.client.MongoClients;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
    import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.data.mongodb.config.AbstractReactiveMongoConfiguration;
    import org.springframework.data.mongodb.core.ReactiveMongoClientFactoryBean;
    import org.springframework.data.mongodb.repository.config.EnableReactiveMongoRepositories;
    
    @EnableReactiveMongoRepositories
    @SpringBootApplication
    public class MongoTestApplication extends AbstractReactiveMongoConfiguration {
    
      public static void main(String[] args) {
        SpringApplication.run(MongoTestApplication.class, args);
      }
    
      @Bean
      public MongoClient mongoClient() {
        return Clients.create(
                &quot;mongodb://user:password@11.111.111.110:27017/database?authSource=admin&quot;);
      }
    
      @Override
      protected String getDatabaseName() {
        return &quot;database&quot;;
      }
    }
  </code></pre>
  <p>Logs:</p>
  <pre><code>  .   ____          _            __ _ _
        /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
        ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
        \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
        '  |____| .__|_| |_|_| |_\__, | / / / /
        =========|_|==============|___/=/_/_/_/
        :: Spring Boot ::                (v2.5.5)
    
        2021-10-06 14:51:54.302  INFO 1034758 --- [  restartedMain] c.e.mongotest.MongoTestApplication       : Starting MongoTestApplication using Java 11.0.10
        2021-10-06 14:51:54.303  INFO 1034758 --- [  restartedMain] c.e.mongotest.MongoTestApplication       : No active profile set, falling back to default profiles: default
        2021-10-06 14:51:54.465  INFO 1034758 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive MongoDB repositories in DEFAULT mode.
        2021-10-06 14:51:54.493  INFO 1034758 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 26 ms. Found 2 Reactive MongoDB repository interfaces.
        2021-10-06 14:51:54.706  INFO 1034758 --- [  restartedMain] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[11.111.111.11:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
        2021-10-06 14:51:54.719  INFO 1034758 --- [  restartedMain] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
        2021-10-06 14:51:54.725  INFO 1034758 --- [127.0.0.1:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server 127.0.0.1:27017
    
        com.mongodb.MongoSocketOpenException: Exception opening socket
        at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:124) ~[mongodb-driver-core-4.2.3.jar:na]
        at java.base/sun.nio.ch.Invoker.invokeUnchecked(Invoker.java:129) ~[na:na]
        at java.base/sun.nio.ch.Invoker$2.run(Invoker.java:219) ~[na:na]
        at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(AsynchronousChannelGroupImpl.java:112) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[na:na]
        at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[na:na]
        at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
        Caused by: java.net.ConnectException: Connection is refused
        at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method) ~[na:na]
        at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(UnixAsynchronousSocketChannelImpl.java:252) ~[na:na]
        at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(UnixAsynchronousSocketChannelImpl.java:198) ~[na:na]
        at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(UnixAsynchronousSocketChannelImpl.java:213) ~[na:na]
        at java.base/sun.nio.ch.EPollPort$EventHandlerTask.run(EPollPort.java:306) ~[na:na]
        ... 1 common frames omitted
    
        2021-10-06 14:51:54.782  INFO 1034758 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
        2021-10-06 14:51:54.980  INFO 1034758 --- [  restartedMain] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8080
        2021-10-06 14:51:54.986  INFO 1034758 --- [  restartedMain] c.e.mongotest.MongoTestApplication       : Started MongoTestApplication in 0.819 seconds (JVM running for 1.152)
        2021-10-06 14:51:55.451  INFO 1034758 --- [22.222.22:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:1, serverValue:289157}] to 11.111.111.11:27017
        2021-10-06 14:51:55.451  INFO 1034758 --- [22.222.22:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:2, serverValue:289158}] to 11.111.111.11:27017
        2021-10-06 14:51:55.451  INFO 1034758 --- [22.222.22:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=11.111.111.11:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=9, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=325549357}
  </code></pre>
  <p><strong>QUESTION</strong>
  I don't want to connect local MongoDb on 127.0.0.1. I want to connect 11.111.111.11 remote MongoDb. How to setup the Spring Data Mongo Application to NOT connect 127.0.0.1?</p>