如何避免在连接到mongodb时提示输入PEM密码短语?

时间:2019-06-18 04:07:12

标签: python mongodb

我正尝试使用ssl如下连接到mongodb,连接到mongo提示Enter PEM pass phrase:,如何避免出现此提示?

from pymongo import MongoClient

database_name = "iso_change_life_cycle_qa"
collection_name= "mymongocollection"

def get_db():
  try:
    # Following are the paths for certificates created by PIE environment. As long as
    # your PIE app environment is authroized by PIE FDB SRE, following should just work.
    client = MongoClient('iso-change-life-cycle-qa-s01.mongo-api.storage.pie.g.company.com',
                         ssl=True,
                         ssl_certfile='/Users/username/Documents/pythonscripts/change_lifecycle.apple.com.chain.pem',
                         ssl_keyfile='/Users/username/Documents/pythonscripts/change_lifecycle.apple.com.key.pem')

    db = client[database_name]
    return db
  except Exception as e:
    print (e.__str__())

def main():
  while True:
    try:
      db= get_db()
      print "db" 
      print db
    except Exception as e:
      print (e.__str__())

if __name__ == "__main__":
  main()

错误:- 运行脚本提示

python testdb.py 
Enter PEM pass phrase:

1 个答案:

答案 0 :(得分:1)

根据文档; https://api.mongodb.com/python/current/examples/tls.html

 client = pymongo.MongoClient('example.com',
                             ssl=True,
                             ssl_certfile='/path/to/client.pem',
                             ssl_keyfile='/path/to/key.pem',
                             ssl_pem_passphrase=<passphrase>)