pyMongo connection with MongoDB Atlas is showing ServerSelectionTimeoutError - mongodb

client = pymongo.MongoClient("mongodb+srv://<username>:<password>#resume.vrpe7.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
db = client.get_database('resumeDB')
job = db.job #code works upto here
job.count_documents({})
The code works up to job = db.job but the code keeps loading in the last line and finally shows the following error.
ServerSelectionTimeoutError: resume-shard-00-01.vrpe7.mongodb.net:27017: connection closed,resume-shard-00-02.vrpe7.mongodb.net:27017: connection closed,resume-shard-00-00.vrpe7.mongodb.net:27017: connection closed, Timeout: 30s, Topology Description: <TopologyDescription id: 62108e85845eec7528b1dde9, topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('resume-shard-00-00.vrpe7.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('resume-shard-00-00.vrpe7.mongodb.net:27017: connection closed')>, <ServerDescription ('resume-shard-00-01.vrpe7.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('resume-shard-00-01.vrpe7.mongodb.net:27017: connection closed')>, <ServerDescription ('resume-shard-00-02.vrpe7.mongodb.net', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('resume-shard-00-02.vrpe7.mongodb.net:27017: connection closed')>]>

Pymongo doesn't make a connection to the database until the first database call. That's why your initial code looks like it is working fine.
The reasons for a ServerSelectionTimeoutError are many (there's plenty of examples on this site), but with Atlas it's most likely that You can't reach the Atlas server due to network reasons, and the most likely reason for that is you haven't opened the firewall on the Atlas site to connect from wherever your code is running.

Related

Mongodb server selection error due network timeout

I have a Go program which uses Mongo DB. I run it from my home pc and I keep getting this error very often:
panic: server selection error: server selection timeout, current
topology: { Type: ReplicaSetNoPrimary, Servers: [{ Addr:
cluster0-shard-00-00.tvm1o.mongodb.net:27017, Type: Unknown, Last
error: connection() error occurred during connection handshake: dial
tcp
3.216.112.85:27017: i/o timeout }, { Addr: cluster0-shard-00-01.tvm1o.mongodb.net:27017, Type: Unknown, Last
error: connection() error occurred during connection handshake: dial
tcp 34.197.85.254:27017: i/o timeout }, { Addr:
cluster0-shard-00-02.tvm1o.mongodb.net:27017, Type: Unknown, Last
error: connection() error occurred during connection handshake: dial
tcp 18.206.5.2:27017: i/o timeout }, ] }
And this is the exact code where it breaks:
if err := clientMongo.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}
I understand this is a connection timeout, but I don't understand how can this happen at all during a simple client connection. I made a speedtest and my current upload speed is 22 Mbps, I am not uploading big json arrays or anything. It happens always when I try to connect to the client. So I would like to know if this can be caused because my internet connection or something on Mongo's end?
You might need to add your IP to the whitelist of MongoDB.
A few things --
we would need to see the complete code for creating a connection. I'm going to assume you're using exactly what is in the documentation here?
You should try to connect with mongosh and Compass also. If you have problems with another tool, then the odds are it is your Atlas cluster OR your security settings on the cluster, rather than your application code.
that being said about 95% of the time the issue is whitelist or database users. do you have a user created in database access area of the UI that has admin/read/write any database? is your IP in the whitelist?
if 3 is good, and 2 doesn't work, there is free Atlas support available in the green chat button of the UI in MongoDB.

Motor/PyMongo connection timeout with AWS DocumentDB

I have used Python's Motor library to connect to an AWS DocumentDB (MongoDB) instance. Motor uses PyMongo behind the scenes.
This was all working fine, but suddenly I have been getting a connection timeout anytime I attempt to do anything that requires a database operation:
pymongo.errors.ServerSelectionTimeoutError: aws-documentdb-endpoint:27017: timed out, Timeout: 30s, Topology Description: <TopologyDescription id: 628376f4a53a913de7752368, topology_type: Single, servers: [<ServerDescription ('aws-documentdb-endpoint', 27017) server_type: Unknown, rtt: None, error=NetworkTimeout('aws-documentdb-endpoint:27017: timed out')>]>
This is how I am creating the connection, which has worked fine for weeks, but now gives this timeout error every time:
from motor.motor_asyncio import AsyncIOMotorClient
client = AsyncIOMotorClient(
host=os.environ["MONGO_URI"],
ssl=True,
ssl_cert_reqs=ssl.CERT_NONE,
ssl_ca_certs=os.path.join(os.environ["ROOT_DIR"], "cert", "aws", "rds-combined-ca-bundle.pem")
)
I have no idea what is going wrong since this has been working fine for so long, but suddenly is not even thought I don't think I've made any changes to this part of the code.

Unable to import json into mongo db atlas

I'm attempting to import json into an atlas db.
The attempt uses this code:
mongoimport --uri "mongodb://usr:pass#myurlname.uia8q.mongodb.net/paintura?retryWrites=true&w=majority" --collection products --file pathtojson
all parameters in the above are correct via the connect docs.
exact error is:
error connecting to host: could not connect to server: server selection error: server selection timeout, current topology: { Type: Single, Servers: [{ Addr: myurlname.uia8q.mongodb.net:27017, Type: Unknown, Last error: connection() error occured during connection handshake: dial tcp: lookup paintura.uia8q.mongodb.net: no such host }, ] }
For the user I have atlas admin selected so I assume it should have full privelege.
I also have cleared my ip in the ip list.
Any ideas what I'm missing?

mongodb connection error with node and express

mongodb connection error with node and express.
The error:
at QueryReqWrap.onresolve [as oncomplete] (dns.js:206:19) {
errno: undefined,
code: 'ECONNREFUSED',
syscall: 'querySrv',
hostname: '_mongodb._tcp.clustr0-a1rol.mongodb.net'
}
Here are some possible issues with this case:
Check your IP address is whitelisted with your MongoDB client
Check your connection URI once again to check the possible error with that.
Please check this other problem and solution. link
I have faced the same issue, whatever I done is:
restart my node server.
change my database user.
change my network and waiting sometime.
then tried to connect.
this time mongo connection was fine.
N.B- you can check your connection URI too, if its ok. then try with a different network.

using mongdb in docker with Go

i tried to run MongoDB in docker-compose and connect my Go server with MongoDB in docker seem not to work for me, thought i already use mongodb:MyContanerName:27017
when i run my go server and then i tired to request on my client, the error something like this
2019/09/08 14:10:37 Unexpected error: rpc error: code = Internal desc = internal error: server selection error: server selection timeout
current topology: Type: Unknown
Servers:
Addr: mongo:27017, Type: Unknown, State: Connected, Average RTT: 0, Last error: connection(mongo:27017[-125]) connection is closed
i already did docker-compose up -d for my docker, and it was running on my docker ,
the full code is trigger here golang and mongodb