连接到 Oracle RDS

Connecting to Oracle RDS

我正在尝试使用 AWS lambda 和 python 连接到 Oracle。

这些是我遵循的步骤。 (EC2实例都搞定了)

  1. 下载了 instantclient-basic-linux.x64-12.2.0.1.0.zip 和
    instantclient-sdk-linux.x64-12.2.0.1.0.zip
  2. 创建了这个文件夹结构~/lambda/lib/
  3. 提取了 ~/lambda/lib/
  4. 中的 zip 个文件
  5. libaio.so.1.0.1/lib64/ 复制到 ~/lambda/lib/
  6. 创建了 libaio.so.1.0.1 的符号 link 作为 libaio.so~/lambda
  7. 使用 pip 在 ~/lambda
  8. 中安装 cx_Oracle
  9. ~lambda
  10. 中写在index.py脚本下面

`

import cx_Oracle

def handler(event, context):
    message = ""
    cursor = None
    connection = None    
    try:
        connection = cx_Oracle.connect("USERNAME", "PASSWORD", "DOMAIN/orcl")
        cursor = connection.cursor()
        cursor.execute("""QUERY""")
    except Exception as e:
        message += " {Error in connection} " + str(e)
    finally:
        if cursor:
            cursor.close()
        if connection:
            connection.close()
    return {'message' : message}

`

  1. 然后使用zip -r9 ~/upload.zip *
  2. 压缩它

在 运行 AWS lambda 上的代码后出现以下错误。

Error while trying to retrieve text for error ORA-01804

我尝试设置 ENV ORACLE_HOME=/var/task 和 /var/task/lib 但没有成功

我看了下面的答案,但还没有找到帮助

Error while trying to retrieve text for error ORA-01019

Oracle with node-oracle: Error while trying to retrieve text for error ORA-01804

我通过使用符号链接正确压缩文件解决了这个问题

首先我创建了三个符号链接(相对于上面的目录结构):

ln -s ./lib/libaio.so.1.0.1 ./lib/libaio.so.1

ln -s ./lib/libaio.so.1.0.1 ./lib/libaio.so

ln -s ./lib/libaio.so.1.0.1 ./libaio.so.1.0.1

ln -s ./lib/libclntsh.so.12.1 ./lib/libclntsh.so

然后我不正确地压缩了它,我是这样做的:

zip --symlinks -r9 ~/lamda.zip *

成功了!正确地 then.Hope 它可以帮助某人。

这个 post 对我使用 cx_Oracle 使用 Lambda 函数很有帮助。它按预期工作。感谢您创建此 post @Sayed Zainul Abideen

但是我从 lambda 中得到另一个错误,指出 'cx_Oracle.DatabaseError: ORA-24454: client host name is not set'

我通过在我的 python 代码中添加以下行解决了这个问题:

import os

with open('/tmp/HOSTALIASES', 'w') as hosts_file:
    hosts_file.write('{} localhost\n'.format(os.uname()[1]))

之后我将以下环境变量添加到我的 Lambda 函数中:

HOSTALIASES = /tmp/HOSTALIASES

希望对大家有所帮助。

如果有任何与 cx_Oracle 相关的问题,请添加评论。我很乐意提供帮助,因为我费了很大劲才解决这个问题。