Mongoengine connection timed out ReplicaSetNoPrimary - python

My project is running on docker and works perfectly fine on my machine. Once I upload it to the server (Ubuntu) and try the following:
print('a')
connect_db()
print('b')
record = Users.objects(id=ObjectId(userId)).first()
print('c')
It gets to b but stops working there. On line 4 it gives me the following error after some time:
bananadev-shard-00-02.u7tbh.mongodb.net:27017: timed out,bananadev-shard-00-00.u7tbh.mongodb.net:27017: timed
out,bananadev-shard-00-01.u7tbh.mongodb.net:27017: timed out, Timeout: 30s, Topology Description:
<TopologyDescription id: 61f261bc41e41fa3a3579d55, topology_type: ReplicaSetNoPrimary, servers:
[<ServerDescription ('bananadev-shard-00-00.u7tbh.mongodb.net', 27017) server_type: Unknown, rtt: None,
error=NetworkTimeout('bananadev-shard-00-00.u7tbh.mongodb.net:27017: timed out')>, <ServerDescription
('bananadev-shard-00-01.u7tbh.mongodb.net', 27017) server_type: Unknown, rtt: None,
error=NetworkTimeout('bananadev-shard-00-01.u7tbh.mongodb.net:27017: timed out')>, <ServerDescription
('bananadev-shard-00-02.u7tbh.mongodb.net', 27017) server_type: Unknown, rtt: None,
error=NetworkTimeout('bananadev-shard-00-02.u7tbh.mongodb.net:27017: timed out')>]>
This is my mongodb connection string:
mongodb+srv://{user}:{password}#bananadev.u7tbh.mongodb.net/Banana_TEST?retryWrites=true&w=majority
Tried adding lots of different parameters at the end of my connection string like: ssl, directconnection, tlsAllowInvalidHostnames, …
I whitelisted the required ip's, created new users to connect from on mongodb.
Tried some different versions of mongoengine, adding a try-except inside my connect_db function but that part seems to work normally, ping & telnet on every shard from my server which worked without any issue as well.
I have been searching for a while, I hope someone knows the solution. Thanks.

Related

MongoDB Connection refused, but solved by restart

For a python program, I am able to connect fine to my Mongo database, but randomly after a few days the connection started being refused.
-----------:27017: [Errno 111] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: -----------, topology_type: Single, servers: [<ServerDescription ('-----------', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('-----------: [Errno 111] Connection refused')>]>
However, the issue resolves when restarting the Python program. Is this a misconfiguration of something? I am only creating a single connection and reusing it everywhere.

I cannot understand this error in Pymongo

I am working on a project and I need well... a Database (MongoDB atlas) and I see while trying to send data I get this error :
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-00.e4dtu.mongodb.net:27017: connection closed,cluster0-shard-00-01.e4dtu.mongodb.net:27017: connection closed,cluster0-shard-00-02.e4dtu.mongodb.net:27017: connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 61fa067baedd893a84a4269d, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.e4dtu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-00.e4dtu.mongodb.net:27017: connection closed')>, <ServerDescription ('cluster0-shard-00-01.e4dtu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-01.e4dtu.mongodb.net:27017: connection closed')>, <ServerDescription ('cluster0-shard-00-02.e4dtu.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-02.e4dtu.mongodb.net:27017: connection closed')>]>
Well I was being an Idiot and running the code on Replit... Which ofc runs on a different computer whose IP address was not SET tp my Database. I now run it on my local computer and it is fine.

sshtunnel timing out when used within a virtualenv

I have an application that connects to a mongo database on aws through an SSH Tunnel. When I run the application outside a virtualenv it works just fine and is able to connect to the database but within a virtualenv I always get the following timeout error
db-url:27017: timed out,db-url:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 61f46c0c4ce4a333018443c3, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('db-url', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('db-url: timed out')>, <ServerDescription ('db-url', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('db-url:27017: timed out')>]>
I have installed the same package versions (including dependencies) for sshtunnel in the virtualenv and outside it but the same error still occurs.
Just for reference, this is the code I'm using to create the SSH Tunnel
# Create the tunnel
server = SSHTunnelForwarder(
(SSH_HOST, SSH_PORT),
ssh_username= SSH_USER,
ssh_pkey=SSH_PEM,
remote_bind_address=(DB_HOST, DB_SOURCE_PORT),
local_bind_address=('127.0.0.1', DB_TARGET_PORT)
)
# Start the tunnel
server.start()
I then connect to mongoDB like so
client = MongoClient(
host='127.0.0.1',
port=DB_TARGET_PORT,
username=DB_USER,
password=DB_PASSWORD,
retryWrites=False
)
Thanks in advance for any help

Problems with connect to MongoDB Atlas from Python

I am new to MongoDB Atlas and Python. I am using Python 3.9 and PyMongo 4.0.1: I have trouble connecting to a free MongoDB Atlas cluster that I have created based on MongoDB instruction. Please see the attached image -- https://behainguyen.files.wordpress.com/2022/01/cloud-mongo-db-connection-string.jpg, my connection string is:
mongodb+srv://behai:<password>#cluster0.71o6u.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
I have replaced with the password for user "behai" in MongoDB Atlas. and I store it as MONGO_CONNECTION_STRING in my keys.py file.
I connect as:
import pymongo
import keys
client = pymongo.MongoClient( keys.MONGO_CONNECTION_STRING )
And I have the error:
ConfigurationError: All nameservers failed to answer the query cluster0.71o6u.mongodb.net. IN TXT: Server 192.168.0.1 UDP port 53 answered DNS message is malformed.
If I dropped "+srv" from the connection string, I could connect, but later accesses such as creating a database, creating a collection then inserting into the collection would result in an error:
ServerSelectionTimeoutError: cluster0.71o6u.mongodb.net:27017: [Errno 11001] getaddrinfo failed, Timeout: 30s, Topology Description: <TopologyDescription id: 61d8d475109bbda8e8b938f0, topology_type: Unknown, servers: [<ServerDescription ('cluster0.71o6u.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('cluster0.71o6u.mongodb.net:27017: [Errno 11001] getaddrinfo failed')>]>
When I try to ping cluster0.71o6u.mongodb.net:
C:\>ping cluster0.71o6u.mongodb.net
I would get this response:
Ping request could not find host cluster0.71o6u.mongodb.net. Please check the name and try again.
Does it seem that host "cluster0.71o6u.mongodb.net" does not exist?
But when I look into my MongoDB Atlas windows, I can see I have made 6 connections during the last 99 hours.
Try installing dnspython/pymongo[srv] <- (includes the dnspython) using pip and add the srv extra back.
import pymongo
db = pymongo.MongoClient("mongodb+srv://behai:<password>#cluster0.71o6u.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
If you are using Pycharm go setting Project:YOURPROJECT and check if dnspython is there if not then click + icon and seach dnspython and install it.
When i tried to install on command line it keep saying its already installed but didnt work. This will probably solve your problem.

Cannot connect to mongodb database from mongodb.com - problem with Primary Host?

Hi I'm using database hosted on mongodbb.com
It was working properly till I upgraded from tier M5 to M10.
Since there I get an error:
raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: mycluster0-shard-00-00.oolon.mongodb.net:27017: ("VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b'mycluster0-shard-00-00.oolon.mongodb.net'))])",),mycluster0-shard-00-02.oolon.mongodb.net:27017: ("VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b'mycluster0-shard-00-02.oolon.mongodb.net'))])",),mycluster0-shard-00-01.oolon.mongodb.net:27017: ("VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b'mycluster0-shard-00-01.oolon.mongodb.net'))])",), Timeout: 30s, Topology Description: <TopologyDescription id: 61c050abf8f28d05d8f5e73e, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('mycluster0-shard-00-00.oolon.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mycluster0-shard-00-00.oolon.mongodb.net:27017: ("VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b\'mycluster0-shard-00-00.oolon.mongodb.net\'))])",)')>, <ServerDescription ('mycluster0-shard-00-01.oolon.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mycluster0-shard-00-01.oolon.mongodb.net:27017: ("VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b\'mycluster0-shard-00-01.oolon.mongodb.net\'))])",)')>, <ServerDescription ('mycluster0-shard-00-02.oolon.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('mycluster0-shard-00-02.oolon.mongodb.net:27017: ("VerificationError(errors=[DNSMismatch(mismatched_id=DNS_ID(hostname=b\'mycluster0-shard-00-02.oolon.mongodb.net\'))])",)')>]>
I have no clue what can be the issue.
From mongodb.com UI everything looks fine. There is one Primary and two secondary hosts.
The problem seems to be in DNS resolving but I don't know how to fix it..
Regards
Pawel

Categories