远程连接到 EC2 服务器上的 MongoDB http 接口 [英] Remotely connecting to MongoDB http interface on EC2 server

查看:27
本文介绍了远程连接到 EC2 服务器上的 MongoDB http 接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法远程连接到我的 AWS ec2 服务器上的 mongodb http 接口.

I cannot connect remotely to my mongodb http interface on my AWS ec2 server.

我检查了日志并确认它正在侦听端口 28017.我已经使用 netstat -a 确认 tcp 端口 28017 已打开.我已经在安全组中启用了 28017.

I've checked the log and verified that it's listening on port 28017. I've verified with netstat -a that tcp port 28017 is open. I've enabled 28017 in the security group.

我使用 ifconfig 获取我的 IP,并尝试使用 IP:28017 访问 http 接口.没发生什么事.没有错误,什么都没有.

I get my IP with ifconfig, and try to get to the http interface with IP:28017. Nothing happens. No error, nothing.

我已经在我的 mongod.conf 文件中设置了 rest = true 并且我使用 'sudo service mongod restart' 将 mongod 作为服务启动

I've put rest = true in my mongod.conf file and I start mongod as a service with 'sudo service mongod restart'

我什至尝试使用 'sudo mongod --rest' 启动 mongod,但无济于事.

I even tried to start mongod with 'sudo mongod --rest' to no avail.

这是 tail -f/log/mongod.conf 显示的内容:

This is what tail -f /log/mongod.conf shows:

Sat Feb  2 02:06:54 [initandlisten] MongoDB starting : pid=3919 port=27017 dbpath=/data 64-bit host=domU-12-31-39-12-3A-A2
Sat Feb  2 02:06:54 [initandlisten] db version v2.2.2, pdfile version 4.5
Sat Feb  2 02:06:54 [initandlisten] git version: d1b43b61a5308c4ad0679d34b262c5af9d664267
Sat Feb  2 02:06:54 [initandlisten] build info: Linux ip-10-2-29-40 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64 BOOST_LIB_VERSION=1_49
Sat Feb  2 02:06:54 [initandlisten] options: { config: "/etc/mongod.conf", dbpath: "/data", fork: "true", logappend: "true", logpath: "/log/mongod.log", pidfilepath: "/var/run/mongodb/mongod.pid", rest: "true" }
Sat Feb  2 02:06:54 [initandlisten] journal dir=/data/journal
Sat Feb  2 02:06:54 [initandlisten] recover : no journal files present, no recovery needed
Sat Feb  2 02:06:54 [initandlisten] waiting for connections on port 27017
Sat Feb  2 02:06:54 [websvr] admin web console waiting for connections on port 28017

这是 netstat -a 显示的内容:

This is what netstat -a shows:

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 localhost:smtp              *:*                         LISTEN
tcp        0      0 *:27017                     *:*                         LISTEN
tcp        0      0 *:mysql                     *:*                         LISTEN
tcp        0      0 *:28017                     *:*                         LISTEN
tcp        0      0 *:ssh                       *:*                         LISTEN
tcp        0    272 domU-12-31-39-12-3A-A2.:ssh pool-108-54-50-116.ny:50722 ESTABLISHED
tcp        0      0 *:ssh                       *:*                         LISTEN
udp        0      0 *:bootpc                    *:*
udp        0      0 domU-12-31-39-12-3A-A2.c:ntp *:*
udp        0      0 localhost:ntp               *:*
udp        0      0 *:ntp                       *:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     5402   @/com/ubuntu/upstart
unix  2      [ ACC ]     STREAM     LISTENING     40948  /var/lib/mysql/mysql.sock
unix  2      [ ACC ]     SEQPACKET  LISTENING     5468   @/org/kernel/udev/udevd
unix  2      [ ACC ]     STREAM     LISTENING     6332   /var/run/dbus/system_bus_socket
unix  9      [ ]         DGRAM                    6278   /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     182622 /tmp/mongodb-27017.sock
unix  3      [ ]         STREAM     CONNECTED     182434
unix  3      [ ]         STREAM     CONNECTED     182433
unix  2      [ ]         DGRAM                    182429
unix  2      [ ]         DGRAM                    131031
unix  2      [ ]         DGRAM                    40170
unix  2      [ ]         DGRAM                    19247
unix  2      [ ]         DGRAM                    7231
unix  2      [ ]         DGRAM                    7212
unix  2      [ ]         DGRAM                    7194
unix  3      [ ]         STREAM     CONNECTED     6341   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     6340
unix  3      [ ]         STREAM     CONNECTED     6336
unix  3      [ ]         STREAM     CONNECTED     6335
unix  3      [ ]         DGRAM                    5484
unix  3      [ ]         DGRAM                    5483

推荐答案

我在尝试远程访问 mongo shell 时遇到了类似的问题,其中一些答案对部分解决方案有所帮助.总结一下:

I had a similar problem trying to access the mongo shell remotely, and several of these answers helped with parts of the solution. To summarize:

  • 公共 IP/DNS:在 EC2 管理控制台上选择您的实例并确保它具有公共 IP 或公共 DNS.AWS 正在将更多默认设置迁移到 Virtual Private Cloud (VPC) 中,并且可以启动到只有一个私有 IP 地址(在 VPC 内部).如果您没有公共 DNS 或 IP,则需要 分配弹性IP.

  • Public IP/DNS: Select your instance on the EC2 Management Console and make sure it has a Public IP or Public DNS. AWS is moving more of the defaults to live within a Virtual Private Cloud (VPC), and it's possible to launch into one with only a Private IP address (internal to the VPC). If you don't have a Public DNS or IP, you need to allocate an Elastic IP.

安全组端口:再次查看 EC2 控制台上的实例详细信息,找到安全组并选择查看规则".假设您使用默认的 Mongo 端口,您应该从 0.0.0.0/0 或(更安全)为 TCP 打开 2701728017从您的 IP 地址.如果没有,请为您的实例选择一个安全组,然后从控制台转到 Inbound > Edit > Add Rule > Custom TCP RulePort Range: 27017 和适当的 IP Source.对于 http 接口,为 Port Range: 28017 添加另一个规则.

Security Group ports: Again looking at instance details on the EC2 Console, find the Security Groups and select "View Rules". Assuming you're using default Mongo ports, you should have 27017 and 28017 open for TCP from 0.0.0.0/0 or (more secure) from your IP address. If not, select a Security Group for your instance and from the console go to Inbound > Edit > Add Rule > Custom TCP Rule, Port Range: 27017, and an appropriate IP Source. For http interface, add another rule for Port Range: 28017.

/etc/mongod.conf:

  • 取消注释 port=27017 以确保您拥有默认端口(我认为这实际上不是必需的,但它让我感觉更好,并且知道在哪里更改默认端口很好...)
  • 注释掉 bind_ip=127.0.0.1 以监听外部接口(例如远程连接)
  • 如果要使用http接口,请取消注释httpinterface=true
  • Uncomment port=27017 to make sure you have the default port (I don't think this is actually necessary, but it made me feel better and it's good to know where to change the default port...)
  • Comment out bind_ip=127.0.0.1 in order to listen to external interfaces (e.g. remote connections)
  • Uncomment httpinterface=true if you want to use the http interface

创建用户:您需要创建一个管理员和/或用户远程访问数据库.

Create User: You need to create an admin and/or user to access the database remotely.

这篇关于远程连接到 EC2 服务器上的 MongoDB http 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆