使用TLS的MongoDB URI连接字符串



我正试图通过TLS仅使用URI字符串连接到本地MongoDB。我可以使用标志很好地连接,但出于项目的目的,我也希望有一个URI字符串。

例如,以下工作:

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

但我想让这样的东西发挥作用:

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

我该如何编写这个URI字符串以使其能够用于我的预期目的?

感谢您的帮助。

我已经将这两种变体配置为以相同的方式工作(除了客户端证书之外,还包括用户名和密码(:

mongouri = 'mongodb://user_name:password@host.de: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)

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')

注意:不包含"@"的密码会让事情变得更容易…而真实与真实的情况确实很重要。

最新更新