1

In my project, I need to setup replica set for two mongo db instances. I did setup the mongo db in two different PCs to be primary and secondary and tested, it works fine.

I connected this to Spring Boot and it shows the replica set is working fine, the service also giving the expected output. But when I stop the primary mongo db instance, the service connects to the secondary for a moment and then starts complaining about primary being down. The service is also giving 500 Internal server error.

Below is the mongo uri setup in properties file

app.mongo.uri=mongodb://localadmin:test123@firstIp:27017,secondIp:27017/mydb?replicaSet=rs0&readPreference=secondary

This is the MongoAppConfig class that is overridden so that I can pass the username and password dynamically.

    @Configuration
@EnableMongoRepositories
public class SampleMongoConfig extends AbstractMongoConfiguration {

    @Value("${app.mongo.uri}")
    private String mongoUri;

    @Override
    @Bean
    public Mongo mongo() throws Exception {
        //todo: username / pwd to be appended programatically.
        MongoClientURI uri = new MongoClientURI(mongoUri, MongoClientOptions.builder().readPreference(ReadPreference.secondary()));
        return new MongoClient(uri);
    }

    @Override
    protected String getDatabaseName() {
        return "mydb";
    }

}

This is the error I'm seeing when the primary is brought down.

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:589)
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
    ... 3 common frames omitted
INFO  o.m.d.cluster - Monitor thread successfully connected to server with description ServerDescription{address=secondIp:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=657772587, setName='rs0', canonicalAddress=secondIp:27017, hosts=[firstIp:27017], passives=[secondIp:27017], arbiters=[], primary='null', tagSet=TagSet{[]}, electionId=null, setVersion=3, lastWriteDate=Wed May 23 04:05:29 GMT 2018, lastUpdateTimeNanos=1833371701259}
INFO  o.m.d.cluster - No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=REPLICA_SET, connectionMode=MULTIPLE, serverDescriptions=[ServerDescription{address=firstIp:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused (Connection refused)}}, ServerDescription{address=secondIp:27017, type=REPLICA_SET_SECONDARY, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 4, 4]}, minWireVersion=0, maxWireVersion=5, maxDocumentSize=16777216, roundTripTimeNanos=657772587, setName='rs0', canonicalAddress=10.229.2.190:27017, hosts=[firstIp:27017], passives=[secondIp:27017], arbiters=[], primary='null', tagSet=TagSet{[]}, electionId=null, setVersion=3, lastWriteDate=Wed May 23 04:05:29 GMT 2018, lastUpdateTimeNanos=1833371701259}]}. Waiting for 30000 ms before timing out
Ragu
  • 51
  • 2
  • You set up the replica set? Show the `rs.config()` output. By the look of you connection string I would guess you did not use fully qualified names on the replica set configuration or at least the resolved host addresses differ between the replica set config and the "client" point of view. – Neil Lunn May 23 '18 at 04:19
  • Or of course the more glaringly obvious is you only show two nodes in your "seed list", but are there "more than two" nodes in the replica set itself? Because if there is not, then a primary cannot be elected. – Neil Lunn May 23 '18 at 04:20
  • There are only two nodes. One is primary and the another one is secondary. I can see in the mongo logs that the second one is elected as primary when the first one is brought down. I observed this error in spring boot and forced the secondary to never be primary by setting the priority to 0. – Ragu May 23 '18 at 04:23
  • And there's the issue. A replica set requires more than two nodes in order to elect a primary when another member goes down. This is covered in [all the documentation](https://docs.mongodb.com/manual/replication/) you can read. Configuration of "replica sets" itself is not a programming topic. You can ask about how to set up one on [dba.stackexchage.com](http://dba.stackexchange.com) if you need to do that. But you need at least one more node. – Neil Lunn May 23 '18 at 04:24
  • Thank you for the suggestion. I will try adding another mongo db and share the results. – Ragu May 23 '18 at 04:27
  • This is working fine after introducing additional mongo instance. Thanks for your help on this. – Ragu May 23 '18 at 10:38

0 Answers0