使用云上 Db2(仓库)的凭据来初始化 flask-sqlalchemy

Using credentials from Db2 (Warehouse) on Cloud to initialize flask-sqlalchemy

在具有 flask-sqlalchemy 的 Flask 应用程序中,我试图通过将 SQLALCHEMY_DATABASE_URI 设置为其中一个部分来初始化与 Db2 Warehouse on Cloud 的连接在服务凭证中提供。过去,使用 uri 组件工作正常,但我的新服务只有 SSL 连接。

app.config['SQLALCHEMY_DATABASE_URI']=dbInfo['uri']

这会导致连接错误

File "/home/vcap/deps/0/python/lib/python3.6/site-packages/ibm_db_dbi.py", line 592, in connect
conn = ibm_db.connect(dsn, '', '', conn_options)
Exception: [IBM][CLI Driver] SQL30081N A communication error has been detected. Communication protocol being used: "TCP/IP". Communication API being used: "SOCKETS". Location where the error was detected: "52.117.199.197". Communication function detecting the error: "recv". Protocol specific error code(s): "104", "*", "0". SQLSTATE=08001 SQLCODE=-30081 During handling of the above exception, another exception occurred:

似乎驱动程序不接受 URI 字符串中指定的 ssl=true 选项。我应该使用服务凭证的哪些部分?我需要手动构建 URI 字符串吗?

由于有解决方法,这只是部分答案。我正在使用服务凭证中的端口信息来修改连接 URI:

if dbInfo['port']==50001:
    # if we are on the SSL port, add additional parameter for the driver
    app.config['SQLALCHEMY_DATABASE_URI']=dbInfo['uri']+"Security=SSL;"
else:
    app.config['SQLALCHEMY_DATABASE_URI']=dbInfo['uri']

通过将 Security=SSL 添加到 uri,驱动程序获取有关 SSL 的信息并使用正确的设置连接到 Db2。