带有Oracle的AWS Python Lambda-OID生成失败 [英] AWS Python Lambda with Oracle - OID Generation Failed
问题描述
我正在尝试使用AWS Lambda Python代码连接到Oracle数据库.
I'm trying to connect to an Oracle DB using AWS Lambda Python code.
我的代码如下:
import sys, os
import cx_Oracle
import traceback
def main_handler(event, context):
# Enter your database connection details here
host = "server_ip_or_name"
port = 1521
sid = "server_sid"
username = "myusername"
password = "mypassword"
try:
dsn = cx_Oracle.makedsn(host, port, sid)
print dsn
connection = cx_Oracle.Connection("%s/%s@%s" % (username, password, dsn))
cursor = connection.cursor()
cursor.execute("select 1 / 0 from dual")
except cx_Oracle.DatabaseError, exc:
error, = exc.args
print >> sys.stderr, "Oracle-Error-Code:", error.code
print >> sys.stderr, "Oracle-Error-Message:", error.message
tb = traceback.format_exc()
else:
tb = "No error"
finally:
print tb
if __name__ == "__main__":
main_handler(sys.argv[0], None)
如果已在
运行此代码时,我得到: DatabaseError:ORA-21561:OID生成失败
When running this code, I'm getting: DatabaseError: ORA-21561: OID generation failed
我尝试使用Oracle服务器的IP和名称进行连接:相同的错误.
i've tried to connect using IP of the Oracle server and the name: same error.
这是错误的输出
Oracle-Error-Code: 21561
Oracle-Error-Message: ORA-21561: OID generation failed
Traceback (most recent call last):
File "/var/task/main.py", line 20, in main_handler
connection = cx_Oracle.Connection("%s/%s@%s" % (username, password, dsn))
DatabaseError: ORA-21561: OID generation failed
对于那些已成功在AWS Lambda Python中运行CX_Oracle的人,可以请您帮忙吗?
For those who have successfully run the CX_Oracle in AWS Lambda Python, can you please help ?
谢谢
推荐答案
好的,这是解释: Oracle有一个有趣的行为,即如果无法解析主机名给定的主机名,它将无法连接到数据库.幸运的是,在Linux中,可以通过在/tmp中写入别名文件,然后将环境变量HOSTALIASES设置为该文件来覆盖会话的DNS条目.
Ok, here is the explanation: Oracle has a funny behavior where if the hostname given by hostname can't be resolved, it will fail connecting to the DB. Fortunately, in Linux, one can override the DNS entry for a session by writing an alias file in /tmp, then setting the environment variable HOSTALIASES to that file.
因此,将此代码添加到函数帮助中以生成此文件,现在我可以成功连接:
So adding this code to my function help to generate this file, and now I can successfully connect:
f = open('/tmp/HOSTALIASES','w')
str_host = os.uname()[1]
f.write(str_host + ' localhost\n')
f.close()
希望它可以帮助其他人!
Hope it can help someone else !
这篇关于带有Oracle的AWS Python Lambda-OID生成失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!