I have a huge flask application and I use the PyMongo class from flask_pymongo for MongoDB operations. My issue is in development environment.
I have a MONGO_URI in my config.py like this:
MONGO_URI = "mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority"
Usage in my app looks like this:
# This is how I have initialized it in '__init__.py'
from flask_pymongo import PyMongo
mongo = PyMongo(app)
# This is how I access collections in the specified DB
document = mongo.db[collection_name].find() # Throws the below error
This worked fine until a couple days ago when I reinstalled Windows and also Python from scratch.
Now the same code throws the following error:
pymongo.errors.ServerSelectionTimeoutError: cluster-name-shard-00-01.pihvl.gcp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate
But using MongoClient instead of PyMongo works just fine, like this:
# Using MongoClient and setting 'ssl_cer_reqs'
from flask_pymongo import MongoClient
import ssl
mongo = MongoClient(app.config['MONGO_URI'], ssl_cert_reqs=ssl.CERT_NONE)
# This works just fine
document = mongo[db_name][collection_name].find()
Questions
What changed with PyMongo when I reinstalled everything? I don't want to use MongoClient simply for the fact that there are over 100 endpoints and it is a nightmare to shift now, and also I prefer PyMongo because I don't have to specify the db_name in every query, it gets that from the MONGO_URI.
What am I missing and how do I make it work with PyMongo? Is there a way to set the ssl_cert_reqs while using PyMongo?
Something in the certificate chain will have changed. pymongo has specific documentation on how to troubleshoot this issue:
https://pymongo.readthedocs.io/en/stable/examples/tls.html#troubleshooting-tls-errors
IMPORTANT: Do this only if you're facing this issue in development environment
All I had to do was add &ssl=true&ssl_cert_reqs=CERT_NONE to my MONGO_URI in my Development Configuration so now my uri would change from:
mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority
to:
mongodb+srv://username:password#cluster-name.pihvl.gcp.mongodb.net/db_name?retryWrites=true&w=majority&ssl=true&ssl_cert_reqs=CERT_NONE
pip install certifi
import pymongo
import certifi
myclient = pymongo.MongoClient(CONNECTION_STRING, tlsCAFile=certifi.where())
...
Related
I am trying to fetch data from my mongodb but it fails with:
pymongo.errors.OperationFailure: Authentication failed.
It is weird because when I am trying to authenticate with same credentials through robo3T (MongoDB GUI) it is able to connect easily.
Through code it is like:
client = MongoClient(host=host,
port=int(port),
username=username_admin_db,
password=password_admin_db,
authSource=authSource_admin_db,
maxPoolSize=15,
MaxIdleTimeMS=120000
)
db_handle = client[database_name]
for i in db_handle[collection_name].find({'account_id': "1234"}):
print(i)
In mongod.conf:
security:
authorization: enabled
It is also weird because through the same code I am able to authenticate to another db on a different server with same credentials and same db name.
Versions:
Python 3.6.10
MongoDB 3.4
pymongo 3.5.1
Django 1.11.7
Facing issue importing golang mongodb driver.
go get -u go.mongodb.org/mongo-driver/mongo
go get -u go.mongodb.org/mongo-driver/mongo#~1.0.0
package go.mongodb.org/mongo-driver/mongo: unrecognized import path "go.mongodb.org/mongo-driver/mongo" (https fetch: Get https://go.mongodb.org/mongo-driver/mongo?go-get=1: dial tcp: lookup go.mongodb.org:
no such host)
I would suggest using the mgo library.
import (
mgo "gopkg.in/mgo.v2"
)
You can find more information about this library here: https://gopkg.in/mgo.v2
I used it for multiple websites and it worked like a charm every time.
It seems to be a network issue. Try some another network connection which is not ruled under firewall.
Or else you can use go mod to resolve dependencies automatically.
I am also using the same driver for mongo DB and it is working perfectly fine.
Kindly check here - Golang-Mongo-Driver-Example
I have python 2.7, have pymongo 3.4
and im trying to connect to mongo atlas using the following :
import certifi
import ssl
import os
from pymongo import MongoClient
MongoClient(
"mongodb://Admin:<PASSWORD>#ABC-shard-00-00-XYZ:27017",
"ABC-shard-00-01-XYZ:27017",
"ABC-shard-00-02-XYZ:27017/dev?ssl=true&replicaSet=ABC-0&authSource=admin",
ssl_cert_reqs=ssl.CERT_REQUIRED,
ssl_ca_certs=certifi.where()
)
it keeps failing with Authentication failed. code 18
As described in Using PyMongo with MongoDB Atlas:
Since PyMongo v3.4.0, you can connect to MongoDB Atlas by passing the Connection String URI provided by MongoDB Atlas to MongoClient, example:
from pymongo import MongoClient
client = MongoClient("mongodb://USER:PASSWORD#ABC-cluster-shard-00-00-XYZ.mongodb.net:27017,ABC-cluster-shard-00-01-XYZ.mongodb.net:27017,ABC-cluster-shard-00-02-XYZ.mongodb.net:27017/DATABASE?ssl=true&replicaSet=ABC-cluster-shard-0&authSource=admin")
Connections to MongoDB Atlas require TLS/SSL. With PyMongo 3.3+, you can install PyMongo 3.3+ and any TLS/SSL-related dependencies using the following Python pip command:
python -m pip install pymongo[tls]
I am trying to connect to mongo in MAC using pymongo. I am getting the following error-
>>> from pymongo import MongoClient
Traceback (most recent call last):
File "", line 1, in
from pymongo import MongoClient
ImportError: cannot import name 'MongoClient'
I have tried Connection also. But it gives the same error. Any help?
Steps for troubleshooting:
First of all, verify if your environment is activated and you are in the correct environment.
If it is active and you're in the correct environment, then verify if you have installed pymongo.
If it's not installed in your environment, install it using pip install pymongo in the environment you want to work.
I am making a heroku app using Flask with a mongoDB backend. I am using pymongo as my driver but when I push my code to git, it crashes and spits out this error:
2014-04-05T09:56:36.301695+00:00 app[web.1]: ImportError: No module named pymongo
The web-app works locally and pymongo works completely fine in that scenario. What do I have to do to have pymongo recognized on heroku's servers?
Thansks!
You need to make Heroku aware of PyMongo as a requirement. The easiest way to do this is my adding pymongo==x.x to your requirements.txt file.
If you are using a setup.py script instead of requirements.txt, add 'pymong==x.x', to the install_requires argument to setup.
In both cases, make sure to replace x.x with the appropriate version number.