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
Related
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 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.
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.
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
I am trying to locally connect to my db. I've established a connection to the database on MongoDB Compass, but when I run my simple code, I get this error:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno
111] Connection refused, Timeout: 30s, Topology Description:
<TopologyDescription id: 60e2b31fab1da2bb146bb38c, topology_type:
Single, servers: [<ServerDescription ('localhost', 27017) server_type:
Unknown, rtt: None, error=AutoReconnect('localhost:27017: [Errno 111]
Connection refused')>]>
root#LAPTOP-8OKVP35I:/portfolio/myProjects/webDevelopment/shopify_db#
This is the code I am running:
import pymongo
from pymongo import MongoClient
client = pymongo.MongoClient()
db = client["Shopper_Info"]
my_collection = db["Names"]
shopper_data = {'name': 'Yoni', 'email': 'test#gmail.com'}
my_collection.insert_one(shopper_data)
results= collection.find({"name": 'Yoni'})
for result in results:
print(result)
This problem was also happening in my stack, I had my connection string in an env file to connect to mongo atlas.
MONGO_URI=mongodb://<username>:<password>#cluster-details
But the right way to do this is
MONGO_URI="mongodb://<username>:<password>#cluster-details"
This happend to me as well.
make sure that you have started the mongod server.that worked for me.
sudo service mongod start.
just add the above code to your settings.py in django :-
DATABASE = {
'default': {
'ENGINE': 'djongo',
"CLIENT": {
"name": <your_database_name>,
"host": <your_connection_string>,
"username": <your_database_username>,
"password": <your_database_password>,
"authMechanism": "SCRAM-SHA-1",
},
}
}
but in host you may get this issue
:-"pymongo.errors.ServerSelectionTimeoutError:"[SSL:
CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get
local issuer certificate (_ssl.c:997) so for this you add like :-
"mongodb+srv://sampleUser:samplePassword#cluster0-gbdot.mongodb.net/sampleDB??ssl=true&ssl_cert_reqs=CERT_NONE&retryWrites=true&w=majority"