I have an Ubuntu server that is trying to connect to a remote MongoDB server. When I try to connect to the remote MongoDB server via the following method:

mongo -u user -p pass yourdb.com:27017

It returns the following error:

2018-06-04T20:41:36.667-0400 W NETWORK  [thread1] Failed to connect 
to after 5000ms milliseconds, giving up.
2018-06-04T20:41:36.667-0400 E QUERY    [thread1] Error: couldn't 
connect to server yourdb.com:27017, connection attempt failed :
exception: connect failed

The bindIP on /etc/mongod.conf is set to

 port: 27017

When I use sudo iptables -L -n | grep 27017, I get the following result:

ACCEPT     tcp  --              tcp 

ACCEPT     tcp  --              tcp 
spt:27017 state ESTABLISHED

I can access the remote db from other servers and my own local machine so it seems the firewall on this machine wasn't configured properly.

The remote MongoDB server has its security groups ports opened to interact with this server so that isn't an issue either.

Have been looking for resources for hours so would need help.Thanks!


Tried to use telnet to test the remote mongo instance from the server instance and it responds with:

telnet: Unable to connect to remote host: Connection timed out

1 Answer 1


If there is some doubt about whether this TCP connection is actually open or not on port 27017, its time to use the TCPDUMP utility. This allows you to see the actual traffic to see if the TCP handshake completes or not.

Use a syntax like tcpdump -ni eth0 port 27017

Assuming you can reach the server on other ports, either the traffic is being dropped somewhere in between by a router/firewall or the server is mishandling the reply. TCPDUMP helps explain this and you can focus on log files or other potential causes.

  • Ran the command for a few minutes on the mongo server and I got this: 824 packets captured 824 packets received by filter 0 packets dropped by kernel Should I capture the packets by some sort of filter and write it down in a file?
    – user472666
    Jun 5, 2018 at 1:52
  • Ran the command on the server instance and it shows: 20831 packets captured 21321 packets received by filter 487 packets dropped by kernel
    – user472666
    Jun 5, 2018 at 3:10
  • Okay that is probably picking up traffic from other hosts on the network, refine the tcpdump command down to include only the host who is trying to connect like maybe "tcpdump -ni eth0 host x.x.x.x and port 27017 -w /tmp/capture1.pcap" This narrows down to the remote host who is connecting to the mongo server while you test trying to connect. You can then cat /tmp/capture1.pcap to examine the raw data or move that to a windows machine and use Wireshark.
    – manbearpig
    Jun 5, 2018 at 5:00
  • I ran the tcpdump code on the mongodb instance with the server as the host. When I used tcpick to make the tcpdump characters look nice, this is the output that I got Starting tcpick 0.2.1 at 2018-06-05 14:42 UTC Timeout for connections is 600 tcpick: reading from capture1.pcap tcpick: done reading from capture1.pcap 0 packets captured 0 tcp sessions detected
    – user472666
    Jun 5, 2018 at 14:44
  • Okay time to put on your troubleshooting hat. If your mongodb server is not seeing packets from the client trying to connect, something is blocking or mishandling it.
    – manbearpig
    Jun 5, 2018 at 23:41

You must log in to answer this question.