title: Spring Boot - MongoDB - MongoLab configuration
body: <p>I'm trying to configure my Spring Boot backend to connect to a MongoDB hosted on MongoLab.</p>
  
  <p>The problem is it tries to connect to localhost. The error I get is:</p>
  
  <pre><code>2017-06-12 17:34:05.046  INFO 11843 --- [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.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:na]
          at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:na]
          at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:na]
          at java.lang.Thread.run(Thread.java:745) [na:1.8.0_25]
  Caused by: java.net.ConnectException: Connection refused
          at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_25]
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345) ~[na:1.8.0_25]
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_25]
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_25]
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_25]
          at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_25]
          at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:na]
          at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:na]
          ... 3 common frames omitted
  </code></pre>
  
  <p>I tried excluding MongoAutoConfiguration.class but I get the same error.</p>
  
  <p>My code:</p>
  
  <p><strong>FoodappBackendApplication.java:</strong></p>
  
  <pre><code>@SpringBootApplication
  @EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})
  //@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})
  public class FoodappBackendApplication {
  
      public static void main(String[] args) {
          SpringApplication.run(FoodappBackendApplication.class, args);
      }
  }
  </code></pre>
  
  <p><strong>application.properties:</strong></p>
  
  <pre><code>server.port=8181
  spring.data.mongodb.host=mongoserver
  spring.data.mongodb.database=foodapp
  spring.data.mongodb.uri=mongodb://&lt;myusername&gt;:&lt;mypassword&gt;@ds123456.mlab.com:12345/foodapp
  spring.data.mongodb.authentication-database: admin
  </code></pre>
  
  <p><strong>SpringMongoConfig.java:</strong></p>
  
  <pre><code>@Configuration
  public class SpringMongoConfig extends AbstractMongoConfiguration {
  
      @Override
      public String getDatabaseName() {
          return "foodapp";
      }
  
      @Override
      @Bean
      public Mongo mongo() throws Exception {
          return new MongoClient();
      }
  }
  </code></pre>
  
  <p><strong>pom.xml dependencies:</strong></p>
  
  <pre><code>    &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&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;org.mongodb&lt;/groupId&gt;
              &lt;artifactId&gt;mongodb-driver&lt;/artifactId&gt;
              &lt;version&gt;3.4.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
              &lt;artifactId&gt;spring-data-mongodb&lt;/artifactId&gt;
              &lt;version&gt;1.10.4.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  </code></pre>
  
  <p>Any help is very much appreciated!</p>