Remotely connecting to MongoDB http interface on E

2019-01-23 07:07发布

问题:

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

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.

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

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

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

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

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

回答1:

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:

  • 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.

  • 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:

    • 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.



回答2:

For you EC2 instance, add two inbound custom TCP rules to allow inbound traffic on ports 27017 and 28017.

Make sure to comment out the “bind_ip = 127.0.0.1” in the /etc/mongod.conf file

restart mongod server: $sudo service mongod restart



回答3:

Sounds like there's something wrong with your security group settings. You could try accessing the admin interface from the box itself:

curl http://localhost:28017/

If you get content back, then you've probably got a problem with your security group settings (or the firewall on the box itself).



回答4:

Did you check it's listening on a public IP as well as that port? It may be listening on localhost.