【问题标题】:SQLAlchemy connection error with Db2 on IBM Cloud与 IBM Cloud 上的 Db2 的 SQLAlchemy 连接错误
【发布时间】:2020-03-31 13:58:21
【问题描述】:

我一直在尝试使用提供的凭据连接到托管在 IBM 云上的 IBM DB2 服务器。我按照说明进行操作,但收到以下错误:

SQLAlchemy 格式所需的连接信息,例如: postgresql://username:password@hostname/dbname 或现有连接:dict_keys([]) 无法加载插件:sqlalchemy.dialects:ibm_db_sa 需要的连接信息 SQLAlchemy 格式,示例: postgresql://username:password@hostname/dbname 或现有连接:dict_keys([])

这是我使用的代码:

# Remember the connection string is of the format:
# %sql ibm_db_sa://my-username:my-password@my-hostname:my-port/my-db-name
# Enter the connection string for your Db2 on Cloud database instance below
# i.e. copy after db2:// from the URI string in Service Credentials of your Db2 instance. Remove             the double quotes at the end.
%sql ibm_db_sa://user:password@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB

如果您能帮助我解决此错误,我将不胜感激。

【问题讨论】:

    标签: python db2 database-connection ibm-cloud


    【解决方案1】:

    查看Combining serverless and Cloud Foundry for data retrieval and analytics 上的 IBM Cloud 解决方案教程。它使用 Db2 和 backend is written in Python and SQLAlchemy。相关部分starts in line 78

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

    代码从 Cloud Foundry 环境中的凭据变量中读取 URI。如果端口是 50001,它使用 SSL/TLS 加密连接,您需要将 Security=SSL 添加到该 URI。

    另外,请确保您的代码已安装 Db2 modules for SQLAlchemy。有关依赖项,请参阅 requirements.txt

    【讨论】:

      【解决方案2】:

      你可以试试这个:

      !pip install --upgrade ibm_db
      !pip install --upgrade ibm_db_sa
      !pip install --upgrade SQLAlchemy
      
      import ibm_db
      import ibm_db_sa
      import sqlalchemy
      
      %load_ext sql
      

      然后:

      %sql ibm_db_sa://user:password@dashdb-txn-sbox-yp-dal09-04.services.dal.bluemix.net:50000/BLUDB
      

      【讨论】:

        【解决方案3】:

        我也有类似的问题。这对我有用:

        在运行 SQL 之前尝试安装 SQLAchemy 1.4 版

        pip install --upgrade 'SQLAlchemy<1.4'

        请务必按如下方式安装 IBM DB2 Python 库:

        !pip install --force-reinstall ibm_db ibm_db_sa

        【讨论】:

          猜你喜欢
          • 2022-07-17
          • 1970-01-01
          • 1970-01-01
          • 2023-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多