No se puede conectar a MongoDB ejecutándose en la instancia de AWS EC2 a través de SSH

Tengo una instancia de Amazaon AWS EC2 ejecutando MongoDB como un service que configuro usando estas instrunciones . Soy capaz de Putty en mi instancia de EC2 y entrar en el shell de MongoDB llamando a mongo --port 27017 en el shell de masilla, por lo que el daemon mongod se ejecuta muy bien en esa máquina.

Configuré un túnel SSH de mi máquina a la instancia de EC2, mapeando el puerto 12345 en mi máquina a 27017 en la instancia remota de EC2. Sin embargo, cuando bash conectarme a MongoDB desde mi máquina local de windows llamando a mongo --port 12345 , el cliente mongo shell cuelga durante un minuto y devuelve:

 connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed conectándose a: 127.0.0.1:12345/test connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed 2016-10-31T11: 31: 39.727-0700 I NETWORK [thread1] Socket recv () errno: 10054 Una connection existente fue forzadamente cerrada por el host remoto. connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed 127.0.0.1:12345 connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed 2016-10-31T11: 31: 39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE): 0 error: 9001 soquete [RECV_ERROR] server [127.0.0.1:12345] connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed 2016-10-31T11: 31: 39.728-0700 E QUERY [thread1] Error: error de networking al intentar ejecutar el command 'isMaster' en el host '127.0.0.1:12345': connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed connect@src/mongo/shell/mongo.js: 231: 14 connecting to: 127.0.0.1:12345/test 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] Socket recv() errno:10054 An existing connection was forcibly closed by the remote host. 127.0.0.1:12345 2016-10-31T11:31:39.727-0700 I NETWORK [thread1] SocketException: remote: (NONE):0 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:12345] 2016-10-31T11:31:39.728-0700 E QUERY [thread1] Error: network error while attempting to run command 'isMaster' on host '127.0.0.1:12345' : connect@src/mongo/shell/mongo.js:231:14 @(connect):1:6 exception: connect failed 

A modo de background, también tengo un Amazon RDS ejecutando MySQL y escuchando el puerto 3306 en la instancia EC2, y un túnel SSH mapeando el puerto 6789 en mi máquina local al puerto 3306 en la instancia EC2, y no tengo problemas para conectarme a la Base de datos MySQL a través de mysql -P 6789 -u user_name -p , por lo que el problema es específico de MongoDB.

En cuanto a la conectividad, tengo la opción Keepalive de Putty establecida en 1 y la siguiente testing de script de PowerShell devuelve true para los dos puertos de túnel SSH, por lo que los túneles están definitivamente conectados:

 function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 testing de la function de connection { function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 param ($ IP, $ PORT) function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 $ connected = $ FALSE function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 $ s = Nuevo object Net.Sockets.TcpClient function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 $ s.Connect ($ IP, $ PORT) function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 $ connected =! $ s.connected function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 $ s.close () function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 $ conectado function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 testing de connection "127.0.0.1" 12345 function test-connection{ param($IP,$PORT) $connected = $FALSE $s = New-Object Net.Sockets.TcpClient try{ $s.Connect($IP,$PORT) $connected = !!$s.connected }catch{ #PASS }finally{ $s.close() } $connected } test-connection "127.0.0.1" 12345 test-connection "127.0.0.1" 6789 

El problema era un 100% EBCAK. Resulta que configuré el túnel SSH inicial a la instancia de MySQL usando estas instrucciones que mapean un puerto en mi máquina local al puerto 3306 en la instancia de Amazon RDS, mientras que el demonio MongoDB ( mongod ) se ejecuta en la instancia EC2. Al configurar el túnel de SSH a la instancia de MongoDB seguí las instrucciones un poco demasiado de cerca y como resultado el puerto mapeado 12345 en mi máquina al puerto 27017 en la instancia de MySQL y no la instancia de EC2 que funciona mongod .

No es sorprendente que la actualización del túnel para conectarse a la instancia de EC2 y no a la instancia de RDS hiciera el truco.