title: Spring boot connect to MongoDB replica set of mongo container running
body: <p>I have set 2 mongo container which having 27017:27017 and 27018:27017 as port. mongo1 is set as primary while mongo2 set as secondary and used replica set. </p>
  
  <p>mongo1 as primary has both write, read access and mongo2 has only read access</p>
  
  <pre><code>spring.data.mongodb.uri = mongodb://abc.xyz:27017/user_demo   
  //monog1, abc.xyz is the hostname
  
  spring.data.mongodb.uri = mongodb://abc.xyz:27018/user_demo   
  //mongo2
  </code></pre>
  
  <p>If I connect only monogo1 in my normal spring boot program, it works(read, write) and if I set mongo2 it works(only read), but when I set both in one line it gives error</p>
  
  <pre><code>spring.data.mongodb.uri = mongodb://abc.xyz:27017,abc.xyz:27018/user_demo?replicaSet=idea-mongo-set
  </code></pre>
  
  <p>I checked replica set is working by storing in mongo1, in mongo2 I can read</p>
  
  <p>So, what is wrong with the last line. Is there any other way to store uri for replica set</p>
  
  <p>In official <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-nosql.html" rel="nofollow noreferrer">spring site</a> it is given</p>
  
  <pre><code>spring.data.mongodb.uri=mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test
  </code></pre>
  
  <p>Error I am getting is this</p>
  
  <pre><code>2019-01-08 15:56:35.245  INFO 29804 --- [           main] c.j.bootifulmongodb.BootMongoDBApp       : Started BootMongoDBApp in 3.603 seconds (JVM running for 4.373)
  2019-01-08 15:56:54.466  INFO 29804 --- [azure.com:27018] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server abc.xyz:27018
  com.mongodb.MongoSocketOpenException: Exception opening socket
          at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.3.jar:na]
          at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.3.jar:na]
          at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.3.jar:na]
          at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
  Caused by: java.net.SocketTimeoutException: connect timed out
          at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_181]
          at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85) ~[na:1.8.0_181]
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_181]
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_181]
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_181]
          at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) ~[na:1.8.0_181]
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_181]
          at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_181]
          at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.3.jar:na]
          at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.3.jar:na]
          ... 3 common frames omitted
  </code></pre>