I am using django with mongoengine. I am writing the following in the settings.py file:
from mongoengine import connect
URI = 'mongodb+srv://myusername:[email protected]/django?retryWrites=true&w=majority&ssl=false'
connect(host=URI)
After that, I have a model as follows:
from mongoengine import Document, StringField
class User(Document):
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
meta = {
'collection': 'users'
}
I have a view as follows:
def adduser(request):
userDict = json.loads(request.body)
newUser = User(first_name=userDict['firstName'],last_name=userDict['lastName'])
newUser.save()
return HttpResponse('user added')
When this view function is called, I get an error as follows:
ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-02.5apjp.mongodb.net:27017:
connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS:
20000.0ms),cluster0-shard-00-01.5apjp.mongodb.net:27017: connection closed (configured timeouts:
socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),cluster0-shard-00-00.5apjp.mongodb.net:27017:
connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),
Timeout: 30s, Topology Description: <TopologyDescription id: 65c3bdc13c9136a1191890e1,
topology_type: ReplicaSetNoPrimary, servers: [<ServerDescription ('cluster0-shard-00-00.5apjp.mongodb.net', 27017)
server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-00.5apjp.mongodb.net:27017:
connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>,
<ServerDescription ('cluster0-shard-00-01.5apjp.mongodb.net', 27017)
server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-01.5apjp.mongodb.net:27017:
connection closed (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>,
<ServerDescription ('cluster0-shard-00-02.5apjp.mongodb.net', 27017)
server_type: Unknown, rtt: None, error=AutoReconnect('cluster0-shard-00-02.5apjp.mongodb.net:27017:
connection closed (configured timeouts: socketTimeoutMS: 20000.0ms,
connectTimeoutMS: 20000.0ms)')>]>
[07/Feb/2024 23:02:38] "POST /user/adduser HTTP/1.1" 500 115166
I am using a mongodb free M0 cluster with database named as ‘django’ and collection named as ‘users’.
If I use a non-SRV URI string like localhost:27017, it works fine. But when I use a SRV URI, its giving me this error.
Moreover, I have added 0.0.0.0/0 in the IP access list of Network Access tab of MongoDB Atlas UI.
Please help me to get rid of this error so that I can proceed with basic CRUD operations on MongoDB using Django with MongoEngine.
2
Answers
Of course I need to remove
ssl=false
. But that does not fix my issue. I was getting SSL certificate error after that. From the MongoDB Developer Community, I got to know that my OS system certs are out of date and need to be updated. One can update them or can override them viacertifi
as follows:And then in my settings.py,
MongoDB Atlas server require all client connections to use SSL/TLS.
The mongodb+srv protocol implicitly enables TLS.
Putting
ssl=false
in connection string override the implicit default, resulting in an attempt to connect without using SSL/TLS.Since non-encrypted connections are not permitted, the server immediately closes the connection without a reply, which looks a lot like a network error after connecting.
Remove
ssl=false
from the connection string to connect.