访问 Amazon EC2 上的 Mongodb 时出现问题
我还有一个问题要问你。 我有安装了 mondodb 的 Amazon EC2 实例。 它工作得很好,除了一件事 - 我无法从外部(我的电脑)访问(连接到)它。 我认为安全组的问题。这是某种默认防火墙。 有谁知道如何配置EC2实例来访问mongodb? 提前致谢。
i've got another question for you.
I have Amazon EC2 instance with mondodb installed.
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?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用默认配置从外部访问端口 27017,则需要为端口 27017 添加安全组例外。有关安全组配置,请查看亚马逊 EC2 文档。如果您在 Mongo 上使用不同的端口,请相应地更改安全组端口。
——赛
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. For security group configuration, please check the amazon EC2 documentation. And if you are using a different port on Mongo, change the security group port accordingly.
--Sai
您的 EC2 实例是否是 Windows 服务器?如果是这样,除了 EC2 的安全组之外,您还需要配置 Windows 防火墙以允许传入连接。
转到“管理工具”、“高级安全 Windows 防火墙”,然后配置一条新规则,允许端口 27017(默认 mongo 端口)或您选择的任何端口上的传入连接。
Is your EC2 instance a Windows server by any chance? If so, in addition to EC2's Security Groups you also need to configure Windows Firewall to allow the incoming connection.
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.
这样做之前请仔细考虑。如果您打开端口,请确保限制可以访问它的 IP 号码,否则任何人都可以访问您的数据库。您可以在 MongoDB 中启用身份验证,但它不是特别安全,只是用户名和密码。您不应该将数据库向互联网开放,这不是一个好主意。
比在 EC2 防火墙中打开端口更好的方法是打开 SSH 隧道并转发端口,这可确保只有您可以访问数据库,并且只能在 SSH 隧道处于活动状态时访问。
打开一个新终端并运行此命令(将用户和主机替换为通过 SSH 连接到服务器时使用的用户和服务器名称):
该命令会将计算机上的端口 27017 转发到计算机上的相同端口服务器。要连接到 MongoDB 实例,只需在终端中运行
mongo
(如果这不起作用,请尝试mongo --host 127.0.0.1
甚至mongo --主机 127.0.0.1 --端口 27017
)。如果您在本地计算机上运行 MongoDB,则必须更改第一个端口,因为本地服务器已经在使用它。在这种情况下,请运行此命令:
然后连接
(如果不起作用,可能添加
--host 127.0.0.1
)。使用完数据库后,退出 mongo 并使用 SSH 命令在终端中按 ctrl-C。
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. You can enable authentication in MongoDB, but it's not particularly safe, just a username and password. 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.
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):
The command will forward the port 27017 on your computer to the same port on the server. To connect to the MongoDB instance simply run
mongo
in a terminal (if that doesn't work, trymongo --host 127.0.0.1
or evenmongo --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. In that case run this command instead:
and then connect with
(possibly adding
--host 127.0.0.1
if it doesn't work).When you're done working with the database, exit
mongo
and press ctrl-C in the terminal with the SSH command.