jeudi 25 juin 2015

Unable to connect to MongoDB in AWS, server times out

I have a VPC set up on AWS with 3 instances: One is the environment where my actual Java code is implemented, the 2nd is the PrimaryReplicaNode where the MongoDB database is stored, and the third is a NAT Instance for SSHing in a linux terminal to the database.

My security group for the PrimaryReplicaNode is to allow connections from ports: 27017-27030 tcp 0.0.0.0/0. So the security rules shouldn't be a problem.

I can directly access the database through SSHing to the NAT Instance, and then accessing the database through a linux terminal. All commands work and I can insert documents into the database.

Now, for some reason my application is not able to connect to the database. Here is the code:

public boolean establishConnection() {
        try{   
            client = new MongoClient("10.0.2.113" , 27017);
            return true;
        }catch(Exception e){
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            return false;
        }
    }

The default port is 27017 and I haven't changed that. And the IP address of the instance's host is 10.0.2.113. It is a Private IP Address, but I should be able to connect to it regardless since my development environment instance is in the same VPC as the mongoDB instance.

The error I get is this:

INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=10.0.2.113:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Jun 25, 2015 9:59:41 AM com.mongodb.diagnostics.logging.JULLogger log
INFO: Exception in monitor thread while connecting to server 10.0.2.113:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
    at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)
    at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:127)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketTimeoutException: connect timed out
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:50)
    at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
    ... 3 more

com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=10.0.2.113:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.SocketTimeoutException: connect timed out}}]

Running the command rs.status(), I get this: { "ok" : 0, "errmsg" : "not running with --replSet", "code" : 76 }

Running the command rs.config(), I get this:

2015-06-25T14:25:30.348+0000 E QUERY    Error: Could not retrieve replica set config: { "ok" : 0, "errmsg" : "not running with --replSet", "code" : 76 }
    at Function.rs.conf (src/mongo/shell/utils.js:1017:11)
    at (shell):1:4 at src/mongo/shell/utils.js:1017

Now, I have not set up any replica sets, so this could be a problem.

I have this in my mongod.conf file:

bind_ip=10.0.2.113
port=27017

Any help would be appreciated,

thanks.




2 commentaires:

  1. Try ssh'ing onto the box running mongodb.

    $ cat /etc/hosts

    look at the hostnames as a result of this command.

    I am running mongoDb in docker and got the line: (where '238723a36aaa' is the containers ID)
    172.17.0.2 238723a36aaa

    I then tried:

    return new MongoClient(new ServerAddress("172.17.0.2", 27017), mongoClientBuilder.build());

    and was able to connect!

    RépondreSupprimer
  2. I posted a StackOverflow answer here: http://stackoverflow.com/questions/38906058/unable-to-connect-to-mongodb-container-using-mongojavadriver

    RépondreSupprimer