简体   繁体   English

在Amazon EC2上访问Mongodb的问题

[英]Problem with access to Mongodb on Amazon EC2

i've got another question for you. 我还有另一个问题要问你。 I have Amazon EC2 instance with mondodb installed. 我安装了mondodb的Amazon EC2实例。 It works great except one thing - i can't access (connect to) it from outside (my PC). 除了一件事之外它很好用 - 我无法从外面访问(连接)它(我的电脑)。 I think the problem with Security Groups. 我认为安全组的问题。 It's some sort of default firewall. 这是某种默认防火墙。 Does anyone know how to configure EC2 instance to have access to mongodb? 有谁知道如何配置EC2实例来访问mongodb? Thanks in advance. 提前致谢。

Think carefully before doing this. 在这之前要仔细考虑。 If you open the ports, make sure you restrict the IP numbers that can access it, otherwise anyone will be able to access your database. 如果您打开端口,请确保限制可以访问它的IP号,否则任何人都可以访问您的数据库。 You can enable authentication in MongoDB, but it's not particularly safe, just a username and password. 您可以在MongoDB中启用身份验证,但它不是特别安全,只是用户名和密码。 You should not have your database open to the internet, it is not a good idea. 你不应该把你的数据库打开到互联网,这不是一个好主意。

A better way than opening up ports in the EC2 firewall is to open an SSH tunnel an forward the port, this makes sure that only you can access the database, and only while the SSH tunnel is active. 比在EC2防火墙中打开端口更好的方法是打开SSH隧道并转发端口,这样可以确保只有在SSH隧道处于活动状态时才能访问数据库。

Open up a new terminal and run this command (replacing user and host with the user you use when SSH'ing to your server and the name of the server): 打开一个新终端并运行此命令(将用户和主机替换为SSH到服务器时使用的用户和服务器名称):

ssh user@host -N -L 27017:127.0.0.1:27017

The command will forward the port 27017 on your computer to the same port on the server. 该命令会将计算机上的端口27017转发到服务器上的同一端口。 To connect to the MongoDB instance simply run mongo in a terminal (if that doesn't work, try mongo --host 127.0.0.1 or even mongo --host 127.0.0.1 --port 27017 ). 要连接到MongoDB实例,只需在终端中运行mongo (如果这不起作用,请尝试mongo --host 127.0.0.1或甚至mongo --host 127.0.0.1 --port 27017 )。

If you run MongoDB on your local machine you will have to change the first port, since the local server is already using it. 如果在本地计算机上运行MongoDB,则必须更改第一个端口,因为本地服务器已在使用它。 In that case run this command instead: 在这种情况下,请运行此命令:

ssh user@host -N -L 27018:127.0.0.1:27017

and then connect with 然后连接

mongo --port 27018

(possibly adding --host 127.0.0.1 if it doesn't work). (如果不起作用,可能会添加--host 127.0.0.1 )。

When you're done working with the database, exit mongo and press ctrl-C in the terminal with the SSH command. 使用完数据库后,退出mongo并使用SSH命令在终端中按ctrl-C。

You need to add a security group exception for the port 27017 if you are using default config for you to access it from outside. 如果您使用默认配置从外部访问它,则需要为端口27017添加安全组例外。 For security group configuration, please check the amazon EC2 documentation. 有关安全组配置,请查看amazon EC2文档。 And if you are using a different port on Mongo, change the security group port accordingly. 如果您在Mongo上使用不同的端口,请相应地更改安全组端口。

--Sai --Sai

Is your EC2 instance a Windows server by any chance? 您的EC2实例是否有机会成为Windows服务器? If so, in addition to EC2's Security Groups you also need to configure Windows Firewall to allow the incoming connection. 如果是这样,除了EC2的安全组之外,您还需要配置Windows防火墙以允许传入连接。

Go To Administrative Tools, Windows Firewall with Advanced Security, and configure a new Rule that allows incoming connections on port 27017 (the default mongo port) or whatever port you've chosen. 转到管理工具,具有高级安全性的Windows防火墙,并配置允许端口27017(默认mongo端口)或您选择的任何端口上的传入连接的新规则。

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

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