title: How to set replica set in spring boot?
body: <p>I  Have a spring boot server and I want to connect it to my mongoDB replica set.
  (3 servers in the set)</p>
  
  <p>1.in application.properties I configured:</p>
  
  <pre><code>spring.data.mongodb.uri=mongodb://user:password@172.1.1.55:27017/admin
  </code></pre>
  
  <p>but if I try to write to server that is not the primary I get error. so how can I configure it that my spring boot app always know who is the primary and write to the primary?</p>
  
  <p>I know there is an option to write :</p>
  
  <pre><code>spring.data.mongodb.uri=mongodb://user:password@172.1.1.55:27017,172.1.1.2:27017,172.1.1.3:27017/admin
  </code></pre>
  
  <p>but it throws me an exception:</p>
  
  <pre><code>        2016-09-11 14:14:54.811  INFO 3128 --- [-31-61-35:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server ip-172-1-1-2:27017
  
      com.mongodb.MongoSocketOpenException: Exception opening socket
          at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.2.2.jar:na]
          at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114) ~[mongodb-driver-core-3.2.2.jar:na]
          at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:128) ~[mongodb-driver-core-3.2.2.jar:na]
          at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
      Caused by: java.net.SocketTimeoutException: connect timed out
          at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_101]
          at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_101]
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_101]
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_101]
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_101]
          at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_101]
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_101]
          at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_101]
          at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50) ~[mongodb-driver-core-3.2.2.jar:na]
          at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.2.2.jar:na]
          ... 3 common frames omitted
  </code></pre>
  
  <ol start="2">
  <li>I always connect to the admin DB. but I need access to another db because my collection is there. how can I configure that the user will connect to all db?
  because this user will work with different dbs and different collections?</li>
  </ol>
  
  <p>Thanks a lot </p>