Unable to connect to mongo on remote server

10,138

The issue was bindIp didn't change. There was some issue in restarting mongo from my side.

The habit should be to verify if the bindIp actually changed or not. (using sudo netstat -tulpn | grep 27017)

Thanks steeldriver for pointing it out.

Share:
10,138

Related videos on Youtube

Abhishek Gupta
Author by

Abhishek Gupta

I am a student with interest in everything

Updated on September 18, 2022

Comments

  • Abhishek Gupta
    Abhishek Gupta over 1 year

    I have installed mongo on machine1(Ubuntu 14.04.3 LTS server) in my local network. I have also opened the port 27017 as mentioned in this guide using following commands:

    sudo iptables -A INPUT -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT
    sudo iptables -A OUTPUT  -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT
    

    Current rules are (iptables -L):

    Chain INPUT (policy ACCEPT)
    target     prot opt source               destination
    ACCEPT     tcp  --  anywhere             anywhere             tcp dpt:27017 state NEW,ESTABLISHED
    
    Chain FORWARD (policy ACCEPT)
    target     prot opt source               destination
    
    Chain OUTPUT (policy ACCEPT)
    target     prot opt source               destination
    ACCEPT     tcp  --  anywhere             anywhere             tcp spt:27017 state ESTABLISHED
    

    But I am not able to connect to this port from machine2 (in the same network):

    $ mongo --host 192.168.0.108
    MongoDB shell version: 3.0.4
    connecting to: 192.168.0.108:27017/test
    2016-01-23T18:02:14.848+0530 W NETWORK  Failed to connect to 192.168.0.108:27017, reason: errno:61 Connection refused
    2016-01-23T18:02:14.850+0530 E QUERY    Error: couldn't connect to server 192.168.0.108:27017 (192.168.0.108), connection attempt failed
        at connect (src/mongo/shell/mongo.js:181:14)
        at (connect):1:6 at src/mongo/shell/mongo.js:181
    exception: connect failed
    

    I also tried checking if port is opened or not:

    $ nc -v 192.168.0.108 27017
    nc: connectx to 192.168.0.108 port 27017 (tcp) failed: Connection refused
    

    I am not sure what I am missing. Must be some silly mistake as I am setting up mongo for the first time. Pls help.

    Some more info

    1. Mongo daemon is running. I am able to connect to mongo form the same machine but not from the other machine. The daemon is running:

      $ service mongod status
      mongod start/running, process 31205
      

      and the port is 27017

      $ sudo netstat -tulpn | grep 27017
      tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 31205/mongod
      
    2. I changed the bind_ip to 0.0.0.0 (as suggested somewhere) and restarted mongo. Still the same error is coming.

    3. Distro Info:

      $ cat /etc/*release
      DISTRIB_ID=Ubuntu
      DISTRIB_RELEASE=14.04
      DISTRIB_CODENAME=trusty
      DISTRIB_DESCRIPTION="Ubuntu 14.04.3 LTS"
      NAME="Ubuntu"
      VERSION="14.04.3 LTS, Trusty Tahr"
      ID=ubuntu
      ID_LIKE=debian
      PRETTY_NAME="Ubuntu 14.04.3 LTS"
      VERSION_ID="14.04"
      
    • Abhishek Gupta
      Abhishek Gupta over 8 years
      Yes you are right. bind_ip didn't change because of some issue in restarting mongo. Pls answer so that I can accept Or I will answer with your solution.
    • steeldriver
      steeldriver over 8 years
      Please go ahead and add your own answer - I know nothing about mongo ;)