'Connection error while connecting linode mongodb server

I have installed mogodb linode "mongodb-app" and its running .. created user and collections and able to access it from shell now trying to connect to the server from my local but getting server timeout '''

pymongo.errors.ServerSelectionTimeoutError: 170.187.248.15:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 6218a8cccb94c35a2569ce55, topology_type: Single, servers: [<ServerDescription ('170.187.248.15', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('170.187.248.15:27017: timed out')>]>

default_config = {'MONGODB_SETTINGS': {
#Local DB
'db': 'test_db',
'host': 'localhost',
'port': 27017

#linode "mongodb-app"
'db': '<db name>',
'host': '<server ip>',
'port': 27017,
'username': '<db user>,
'password': '<db password>',
'authentication_source': 'admin'
}

''' I have followed couple of steps : '''

1. updated /etc/mongod.conf from the server
    net:
    port: 27017
    bindIp:0.0.0.0

'''

2. sudo ufw status
    sudo: unable to resolve host 170-187-248-15
    Status: active

    To                         Action      From
    --                         ------      ----
    22                         ALLOW       Anywhere
    27017                      ALLOW       0.0.0.0
    22 (v6)                    ALLOW       Anywhere (v6)

but still facing the same error



Solution 1:[1]

On your linux server where mongodb is running, run these two commands:

iptables -A INPUT -s <ip-address-of-your-app> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT

iptables -A OUTPUT -d <ip-address-of-your-app> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

Taken from https://docs.mongodb.com/manual/tutorial/configure-linux-iptables-firewall/#traffic-to-and-from-mongod-instances

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jamomani