无法通过SSH连接到AWS EC2实例上运行的MongoDB

时间:2016-10-31 21:13:50

标签: mongodb amazon-web-services ssh-tunnel

我有一个运行MongoDB 3.2.10的Amazaon AWS EC2实例,我使用these instrunctions设置。我能够将Putty放入我的EC2实例并通过调用Putty shell中的mongo --port 27017进入MongoDB shell,因此mongod守护程序在该机器上运行正常。

我设置了从我的机器到EC2实例的SSH隧道,将我的机器上的端口12345映射到远程EC2实例上的27017。但是,当我尝试通过调用mongo --port 12345从我的本地Windows机器连接到MongoDB时,mongo shell客户端挂起大约一分钟并返回:

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

作为背景,我还有一个运行MySQL的Amazon RDS并在EC2实例上侦听端口3306,在我的本地机器上有一个SSH隧道映射端口6789到EC2实例上的端口3306,我没有问题通过mysql -P 6789 -u user_name -p连接到MySQL数据库,因此问题特定于MongoDB。

至于连接,我将Putty的keepalive选项设置为1,并且以下PowerShell脚本测试对于两个SSH隧道端口都返回true,因此隧道肯定是连接的:

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

1 个答案:

答案 0 :(得分:0)

检查mongod.conf中的bindIp

指定mongod可以侦听的IP地址。默认情况下,它只接受localhost