I have an application running Twisted Python 3.7, and we’re trying to convert it to run against Atlas and not our own managed MongoDB.
Our local MongoDB has no authentication so it was pretty straight forward. But moving to Atlas we now need to use TLS for the connection and I keep on getting TxMongo lost connection to MongoDB. after connecting.
My connection is of type mongodb+srv://…/?authSource=%24external&authMechanism=MONGODB-X509&retryWrites=true&w=majority and I have the certificate and private key files, of course.
I tried using txmongo example from their own example but it doesn’t work for me.
Any idea what might be wrong?
Thanks,
Oren
Edit: 1/1/23:
I changed the example code a bit, changing the SSL example as follows:
TxMongo original code
class ServerTLSContext(ssl.DefaultOpenSSLContextFactory):
def __init__(self, *args, **kw):
kw['sslmethod'] = SSL.TLSv1_METHOD
ssl.DefaultOpenSSLContextFactory.__init__(self, *args, **kw)
My change
class ServerTLSContext(ssl.DefaultOpenSSLContextFactory):
def __init__(self, *args, **kw):
kw['sslmethod'] = SSL.TLSv1_2_METHOD # v1_METHOD
ssl.DefaultOpenSSLContextFactory.__init__(self, *args, **kw)
The only change is the SSL TSL method.
Now my errors are different. No more disconnections, but a warning in case the initial connection was not made to the master node:
‘TxMongo: MongoDB host atlas-XXX-shard-00-02.XXX.mongodb.net:27017
is not master.’
Once it is connected to the master, I get an error:
TxMongo: not authorized for query on db.collection (real db and connection name in the original error, of course)
So again – any ideas?
2
Answers
pip install certifi
If you are using Atlas then from the web console navigate to the db view and press ‘connect’ you can get a connection string which you can test with Compass or mongosh. Also, remember to set the network access otherwise your connection will fail.