文档数据库服务 DDS-Python示例:连接代码

时间:2024-10-10 15:22:57

连接代码

  • SSL开启
    import ssl
    from pymongo import MongoClient
    conn_urls="mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin&replicaSet=replica"
    connection = MongoClient(conn_urls,connectTimeoutMS=5000,ssl=True, ssl_cert_reqs=ssl.CERT_REQUIRED,ssl_match_hostname=False,ssl_ca_certs=${path to certificate authority file})
    dbs = connection.database_names()
    print "connect database success! database names is %s" % dbs
  • SSL关闭
    import ssl
    from pymongo import MongoClient
    conn_urls="mongodb://rwuser:rwuserpassword@ip:port/{mydb}?authSource=admin&replicaSet=replica"
    connection = MongoClient(conn_urls,connectTimeoutMS=5000)
    dbs = connection.database_names()
    print "connect database success! database names is %s" % dbs
  • URL中的认证数据库必须为“admin”,即“authSource=admin”。
  • SSL方式连接,需要手动生成trustStore文件。
  • 认证数据库必须为“admin”,之后再切换至业务数据库。
support.huaweicloud.com/qs-dds/dds_02_0095.html