Forums

unable to connect to mongodb from bash console

I keep getting the error below due to issues with connecting to my mongodb database via a bash console on pythonanywhere:

pymongo.errors.ServerSelectionTimeoutError: lanfrica-shard-00-01.oap4d.mongodb.net:27017: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),lanfrica-shard-00-00.oap4d.mongo db.net:27017: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),lanfrica-shard-00-02.oap4d.mongodb.net:27017: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, co nnectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 67fe89198a05fbf674e3bae9, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('lanfrica-shard-00-00.oap4d.mo ngodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('lanfrica-shard-00-00.oap4d.mongodb.net:27017: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')> , <ServerDescription ('lanfrica-shard-00-01.oap4d.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('lanfrica-shard-00-01.oap4d.mongodb.net:27017: timed out (configured timeouts: socketT imeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>, <ServerDescription ('lanfrica-shard-00-02.oap4d.mongodb.net', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('lanfrica-shard-00-02.oap4d.mongod b.net:27017: timed out (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>

I have added the IPs of the bash console to mongodb network access, but it still doesn't work. This usually solves the issue for me in other environments (like on my PC, on another virtual server, etc). Some things I want to confirm:

  1. is the IP on the bash console dynamic?
  2. does pythonanywhere have strict network access rules where some ports ae restricted? Since MongoDB Atlas uses port 27017 by default (even when accessed via the SRV connection string), I am concerned that my connection attempts are likely being blocked by PythonAnywhere’s firewall.

The IP address of a Bash console is generally stable over time (except when we do system updates), but each Bash console is started on a random one of our console servers, so different consoles will probably (but not always) have different IP addresses. IPify can help you find the IP address of a specific console -- just run this:

curl 'https://api.ipify.org?format=json'

thank you for the code with ipify. I will use it. In the meantime, I have another issue/question: how do I get the IP address of an always-on task? I need it to be able to add the IP to the MongoDB network access.

Actually, I should have linked you to this help page before -- it has sample code you can use to use ipify at the top of your script to automatically allowlist the IP address it's running on every time it starts.

the help page is indeed very useful and practically solves my problems. thank you very much.

Glad to hear that!