MongoDB URI Connection String Using TLS?

2k Views Asked by At

I'm trying to connect to a local MongoDB using only a URI string over TLS. I can connect just fine using flags, but for my project's purposes I'd like to have a URI string too.

For example, the following works:

mongo mongodb://127.0.0.1:27017/dbName --tls --tlsCAFile=/path/to/ca.pem --tlsCertificateKeyFile=/path/to/key.pem

But I'd like for something like this to work:

mongo 'mongodb://127.0.0.1:27017/dbName?tls=true&tlsCAFile=/path/to/ca.pem&tlsCertificateKeyFile=/path/to/key.pem'

How do I write this URI string to get it to work for my intended purposes?

Any help is appreciated, thanks.

1

There are 1 best solutions below

0
On

I've figured these two variants to work the same way (this includes username and password in addition to the client certificate):

mongouri = 'mongodb://user_name:[email protected]:12345/db_name?tls=true&tlscafile=/path/to/certs/ca_cert.pem&tlscertificatekeyfile=/path/to/certs/client.pem&authSource=auth_db_name'
mongo_client = MongoClient(mongouri)

or

mongo_client = MongoClient('host.de:12345', tls=True, tlscafile='/path/to/certs/ca_cert.pem', tlscertificatekeyfile='/path/to/certs/client.pem', username='user_name', password='password', authSource='auth_db_name')

note: passwords not containing '@' make things easier ... and the case of True vs. true does matter.