I have an Amazaon AWS EC2 instance running MongoDB as a service which I set up using these instrunctions. I'm able to Putty into my EC2 instance and enter the MongoDB shell by calling mongo --port 27017 in the Putty shell, so the mongod daemon is running just fine on that machine.

I set up an SSH tunnel from my machine to the EC2 instance, mapping port 12345 on my machine to 27017 on the remote EC2 instance. However when I try to connect to MongoDB from my local windows machine by calling mongo --port 12345, the mongo shell client hangs for about a minute and returns:

connecting to:
2016-10-31T11:31:39.727-0700 I NETWORK  [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host.
2016-10-31T11:31:39.727-0700 I NETWORK  [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server []
2016-10-31T11:31:39.728-0700 E QUERY    [thread1] Error: network error while attempting to run command 'isMaster' on host ''  :

exception: connect failed

By way of background, I also have an Amazon RDS running MySQL and listening to port 3306 on the EC2 instance, and an SSH tunnel mapping port 6789 on my local machine to port 3306 on the EC2 instance, and I have no problems connecting to the MySQL database via mysql -P 6789 -u user_name -p, so the problem is specific to MongoDB.

As for connectivity, I have Putty's keepalive option set to 1, and the following PowerShell script test returns true for both SSH tunnel ports, so the tunnels are definitely connected:

function test-connection{
    $connected = $FALSE
    $s = New-Object Net.Sockets.TcpClient
        $connected = !!$s.connected
test-connection "" 12345
test-connection "" 6789
  • Running a sudo netstat -nlp --tcp on the aws instance might be useful. This will highlight any differences in the binding between mysql and mongo, and confirm it is properly listening on the host. – KHobbits Nov 1 '16 at 0:48

Problem was a 100% EBCAK. Turns out I set up the the initial SSH tunnel to the MySQL instance using these instructions which maps a port on my local machine to port 3306 on the Amazon RDS instance, whereas the MongoDB daemon (mongod) runs on the EC2 instnace. When setting up the SSH tunnel to the MongoDB instance I followed the instructions a little too closely and as result mapped port 12345 on my machine to port 27017 on the MySQL instance and not the EC2 instance running mongod.

Not surprisingly, updating the tunnel to connect to the EC2 instance and not to the RDS instance did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.