Getting ReplicaSetNoPrimary error for M0 cluster when using Django with MongoEngine

I am using django with mongoengine. I am writing the following in the settings.py file:

from mongoengine import connect
URI = 'mongodb+srv://myusername:mypassword@cluster0.5apjp.mongodb.net/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.

I have run DNS diagnostics as suggested by MongoDB support as follows:

nslookup -q=SRV _mongodb._tcp.cluster0.5apjp.mongodb.net

All’s fine there:
mmm

You can’t set ssl=false (or tls=false) when connecting to Atlas. Atlas requires TLS.

If I remove the ssl=false, I get SSL certificate error:

raise ServerSelectionTimeoutError(
pymongo.errors.ServerSelectionTimeoutError: cluster0-shard-00-02.5apjp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1006) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),cluster0-shard-00-00.5apjp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1006) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms),cluster0-shard-00-01.5apjp.mongodb.net:27017: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1006) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms), Timeout: 30s, Topology Description: <TopologyDescription id: 65c3d74c5eedfb04df0d5a28, 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: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1006) (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: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1006) (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: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:1006) (configured timeouts: socketTimeoutMS: 20000.0ms, connectTimeoutMS: 20000.0ms)')>]>
[08/Feb/2024 00:48:12] "POST /user/adduser HTTP/1.1" 500 118003

How do I bypass this?

The likely issue is that your OS system certs are out of date and need to be updated. You can update them or you can override them via certifi (python -m pip install --upgrade certifi):

import certifi
from mongoengine import connect
URI = 'mongodb+srv://...'
connect(host=URI, tlsCAFile=certifi.where())
2 Likes

Thank you very much. It is working totally fine now.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.