简体   繁体   English

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

[英]Unable to connect to MongoDB running on AWS EC2 instance via SSH

I have an Amazaon AWS EC2 instance running MongoDB 3.2.10 which I set up using these instrunctions . 我有一个运行MongoDB 3.2.10的Amazaon AWS EC2实例,我使用这些功能设置了 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. 我可以将Putty放入我的EC2实例并通过在Putty shell中调用mongo --port 27017进入MongoDB shell,因此mongod守护程序在该机器上运行正常。

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. 我设置了从我的机器到EC2实例的SSH隧道,将我机器上的端口12345映射到远程EC2实例上的27017。 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: 但是,当我尝试通过调用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

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. 作为背景,我还有一个运行MySQL的Amazon RDS,在EC2实例上侦听端口3306,在我的本地机器上的一个SSH隧道映射端口6789到EC2实例上的端口3306,我没有连接到MySQL数据库通过mysql -P 6789 -u user_name -p ,所以问题是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: 至于连接,我将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

Check the bindIp in your mongod.conf 检查mongod.conf中的bindIp

This specifies which IP addresses the mongod can listen to. 这指定了mongod可以侦听的IP地址。 By default it only accept the localhost 默认情况下,它只接受localhost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM