python:使用 Oracle 的 wallet/tnsnames 连接到 Oracle 数据库

python: connecting to an Oracle database using Oracle's wallet/tnsnames

所以我可以这样连接到 Oracle 数据库:

import cx_Oracle as ora
dsnStr = ora.makedsn(host="ABC.COM", port="ABC_PORT", sid="ABC")
con = ora.connect(user="ABC_USER", password="ABC_PASSWORD", dsn=dsnStr)
print(con.version)
con.close()

这很好用,但我想使用 Oracle wallet/tnsnames/database.properties 进行连接(我来自 Java 世界) .到目前为止找不到任何关于如何实现这一目标的信息。另外,我如何 "configure" cx_Oracle 就这些 Oracle wallet/tnsnames/database.properties 各自文件的路径而言。

cx_Oracle 使用与 SQLPlus 相同的技术连接到数据库。所以你可以先用 SQLPlus 测试,如果你觉得这样做更容易的话。

为此,您需要创建一个 sqlnet.ora 配置文件。这允许您设置诸如 wallet file. The environment variable TNS_ADMIN can be used to specify the location of this and other configuration files (like tnsnames.ora) if it isn't found in the default location. See the documentation 位置等参数以获取更多信息。

我无法帮助将 Java 端转换为 cx_Oracle 所需的 C 端,但以下内容可能有助于说明 cx_Oracle 的需求。我将使用基于 Oracle Exadata Express 的示例,它使用钱包进行连接,请参阅将脚本语言连接到 Exadata Express 的通用说明 here

您的确切配置和文件可能有所不同。正如 Anthony 所指出的,安全设置并非特定于 cx_Oracle。更多经验丰富且乐于助人的安全专家可能潜伏在其他论坛中。

对于 Exadata Express,会下载一个预先提供的钱包 zip 文件。对于像 cx_Oracle 这样的 Oracle Call Interface 应用程序,我们只需要 zip 中的这些文件:sqlnet.ora、tnsnames.ora 和 cwallet.sso。您将需要 create/find 这些(或您需要的)文件。

我的文件是:

sqlnet.ora:

WALLET_LOCATION = (SOURCE = (METHOD = file)
                   (METHOD_DATA = (DIRECTORY="$TNS_ADMIN")))

SSL_SERVER_DN_MATCH=yes

tnsnames.ora:

dbaccess = (description=
          (address=(protocol=tcps)(port=1522)(host=whereever.com))
          (connect_data=(service_name=whereever2.com))   
          (security=(ssl_server_cert_dn="CN=wherever2.com,O=Oracle Corporation,L=Redwood Shores,ST=California,C=US"))  
       )

cwallet.sso: 我会把这个留给你想象...

我把那三个文件放在了/Users/cjones/Cloud里,设置了环境可以找到它们:

$ export TNS_ADMIN=/Users/cjones/Cloud

现在我可以使用 tnsnames.ora 文件中的连接名称进行连接:

$ sqlplus -l cj/mypassword@dbaccess

SQL*Plus: Release 12.2.0.1.0 Production on Fri Jul 6 10:20:21 2018

Copyright (c) 1982, 2017, Oracle.  All rights reserved.

Last Successful login time: Tue Jul 03 2018 13:00:06 +10:00

Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL>

在 cx_Oracle 你的 dsn 也会是 dbaccess