MongoDB on Ubuntu EC2 instance - Primary member of replica set cannot be accessed using java

235 views
Skip to first unread message

Manoj P T

unread,
Feb 10, 2015, 7:53:09 AM2/10/15
to mongod...@googlegroups.com
Hello,

I am using Ubuntu t1.micro EC2 instance and installed MongoDB-2.6.7 using the link: [http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/]

The problem what I am facing is that I cannot access the replica set Primary member.

     ServerAddress address0 = new ServerAddress("<public_ip1>", 27017); 
     ServerAddress address1 = new ServerAddress("<public_ip2>", 27018); 
     ServerAddress address2 = new ServerAddress("<public_ip3>", 27019); 

I am getting MongoTimeoutException.

The issue here is: When I don't use PRIMARY's server address and set ReadPreference to secondaryPreferred, I could read from the available SECONDARY.

And I could read (and even write to PRIMARY), when I used any of these server address as individual connection.

     MongoClient mongoClient = new MongoClient("<public_ip1>", 27017);

No problem with security configs also. I have set ALL for inbound and outbound.

Could any one please help me out solving this problem.

The error is given below:

Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches {serverSelectors=[ReadPreferenceServerSelector{readPreference=secondaryPreferred}, LatencyMinimizingServerSelector{acceptableLatencyDifference=15 ms}]}. Client view of cluster state is {type=ReplicaSet, servers=[{address=ip-10-0-2-19:27018, type=Unknown, state=Connecting, exception= {com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: ip-10-0-2-19}}, {address=ip-10-0-3-10:27019, type=Unknown, state=Connecting, exception= {com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: ip-10-0-3-10}}, {address=ip-10-0-3-76:27017, type=Unknown, state=Connecting, exception= {com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.UnknownHostException: ip-10-0-3-76}}] at com.mongodb.BaseCluster.getServer(BaseCluster.java:82) at com.mongodb.DBTCPConnector.getServer(DBTCPConnector.java:656) at com.mongodb.DBTCPConnector.access$500(DBTCPConnector.java:40) at com.mongodb.DBTCPConnector$MyPort.getConnection(DBTCPConnector.java:505) at com.mongodb.DBTCPConnector$MyPort.get(DBTCPConnector.java:448) at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:284) at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:269) at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:84) at com.mongodb.DB.command(DB.java:320) at com.mongodb.DB.command(DB.java:299) at com.mongodb.DBCollection.getCount(DBCollection.java:1269) at com.mongodb.DBCursor.count(DBCursor.java:796) at com.test.replicaSetTest.main(replicaSetTest.java:41)

Jeff Yemin

unread,
Feb 10, 2015, 9:07:50 AM2/10/15
to mongod...@googlegroups.com
When you make a direct connection to a single member of a replica set, the driver uses whatever host/port that you supply it.    But when you make a replica set connection (by supplying more than one host/port), the driver treats those as a seed list to discover the actual members of the set.  It does this by calling the ismaster command on each member of the seed list, and extracting the list of hosts from the response.  This list of hosts have the names that were used to configure the replica set, which may be different than the names used in the seed list.  Your network must be configured so that all clients can connect to the replica set members via the names used in the replica set configuration itself.  


Regards,
Jeff
Message has been deleted

Manoj P T

unread,
Feb 10, 2015, 10:41:25 AM2/10/15
to mongod...@googlegroups.com
Replica Set configuration has been given below:

{
    "_id" : "replicaSet",
    "version" : 5,
    "members" : [
            {
                    "_id" : 0,
                    "host" : "ip-10-0-3-76:27017"  //**private_ip**
            },
            {
                    "_id" : 1,
                    "host" : "ip-10-0-2-19:27018"  //**private_ip**
            },
            {
                    "_id" : 2,
                    "host" : "ip-10-0-3-144:27019"   //**private_ip**
            }
    ] }

The doubt from your post, I have got is whether the "host:port" in the replicaSet configuration itself to be used in MongoClient???? Because I used private_ip to configure replicaSet andpublic_ip in MongoClient. Is this the actual problem I'm facing???   


"all clients can connect to the replica set members via the names used in the replica set configuration itself"

By seeing this statement, I found that I went wrong in using private_ip in ReplicaSet configuration and public_ip in MongoClient. So could you please suggest me right insight on this. Should I go for Elastic IP in production????

Jeff Yemin

unread,
Feb 10, 2015, 10:52:31 AM2/10/15
to mongod...@googlegroups.com
Network configuration is not my area of expertise, but ultimately, yes, the problem is that you are using a private ip to configure the replica set and a public ip to configure MongoClient.  The Java application must be able to resolve the host names used in the replica set configuration.

Regards,
Jeff


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/0652cf10-82a8-43ec-9bc1-97b7a2dd4a2d%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Manoj P T

unread,
Feb 10, 2015, 11:01:18 AM2/10/15
to mongod...@googlegroups.com
Yes correct.. Now I understood. that is why I was getting UnknownHostException. How we can have same ips in the ReplicaSet configuration and MongoClient connection?? Do we have to go for Elastic IP in the production??

Jeff Yemin

unread,
Feb 10, 2015, 12:48:47 PM2/10/15
to mongod...@googlegroups.com

Assuming your application is not in AWS and the MongoDB servers are, then yes.  There is some related documentation available.  See http://docs.mongodb.org/ecosystem/platforms/amazon-ec2/#communication-across-regions for example.

Regards,

Jeff

Manoj P T

unread,
Feb 10, 2015, 9:59:40 PM2/10/15
to mongod...@googlegroups.com
Our application's web servers and db servers are on AWS EC2 only.. how about in this case ?? Elastic ip is the solution ???
Message has been deleted
Message has been deleted

Manoj P T

unread,
Feb 11, 2015, 1:19:51 AM2/11/15
to mongod...@googlegroups.com
I got know from this link: http://blog.mongodirector.com/best-practices-for-deploying-mongodb-on-ec2/. We have two options configuring static ip address for ec2 instances (Elastic IP (this costs) and Route 53). Which would be the one which suits best for MongoDB production???

Manoj P T

unread,
Feb 11, 2015, 7:29:45 AM2/11/15
to mongod...@googlegroups.com
I got to solve the problem by attaching Elastic IP to all 3 instances. 
But 1 more problem... When I start replicaSet with rs.initiate(), this member will be configured with the private_ip of that EC2 instance. Then I reconfigured the host name of that member to Elastic IP and then added the other two members with Elastic IP : port and then it worked fine. 

The question is: Is it possible to have the Elastic IP directly (in the replica set config) as the host IP of the member where we execute rs.initiate() ???
Reply all
Reply to author
Forward
0 new messages