简体   繁体   English

cx_Oracle LDAP 连接字符串语法

[英]cx_Oracle LDAP Connection String syntax

With JDBC, we can use the following syntax to connect to an Oracle database over an LDAP connection:使用 JDBC,我们可以使用以下语法通过 LDAP 连接连接到 Oracle 数据库:

jdbc:oracle:thin:@ldap://host:1234/service_name,cn=OracleContext,dc=org,dc=com

How can I connect over LDAP using cx_oracle?如何使用 cx_oracle 通过 LDAP 进行连接?

I ended up going with jaydebeapi.我最终选择了 jaydebeapi。

import pandas as pd 
import jaydebeapi
import jpype
import os
import sys

def run(f_name,command,username,pw ):
    jar='ojdbc8.jar'
    args = '-Djava.class.path=%s' % jar
    jvm_path = jpype.getDefaultJVMPath()
    jpype.startJVM(jvm_path, args)
    con = jaydebeapi.connect("oracle.jdbc.driver.OracleDriver", "jdbc:oracle:thin:@ldap://server.prod.company.com:3060/service,cn=OracleContext,dc=prod,dc=company,dc=com",[username, pw], jar)
    try:
        df= pd.read_sql(command,con)
        df.to_excel(f_name)
        print(df)
    except Exception as e:
        print(e)
    finally:
        con.close()



def Run_Program(myvars):
    os.chdir(sys._MEIPASS)    
    f_name = myvars.MyFileName
    command = myvars.plainTextEdit_CSVString.toPlainText()
    username = myvars.lineEdit_UserName.text()
    pw = myvars.lineEdit_Password.text()
    run(f_name,command,username,pw )

Saving the ojdbc8.jar file from Oracle Client in the same folder and specifying the location in the code.将来自 Oracle 客户端的 ojdbc8.jar 文件保存在同一文件夹中并在代码中指定位置。 And also downgrading the module JPype1 to JPype1==0.6.3 (its installed as a requirement for jaydebeapi )并且还将模块 JPype1 降级到 JPype1==0.6.3(它作为 jaydebeapi 的要求安装)

This worked well for packaging using pyinstaller so that it could be shared.这对于使用 pyinstaller 打包非常有效,因此可以共享。 (i created a pyqt5 UI for user to use. (我创建了一个 pyqt5 UI 供用户使用。

Here are my two cents using Python 3.7 and cx_Oracle v.8.2.0 on Win 10.这是我在 Win 10 上使用 Python 3.7 和 cx_Oracle v.8.2.0 的两分钱。

I wanted to issue queries to an Oracle database using Python, and what I already had was :我想使用 Python 向 Oracle 数据库发出查询,而我已经拥有的是:

  • a username (or schema)用户名(或架构)
  • a password密码
  • a JDBC connection string that looked like: jdbc:oracle:thin:@ldap://[LDAPHostname1]:[LDAPPort1]/[ServiceName],[DomainContext] ldap://[LDAPHostname2]:[LDAPPort2]/[ServiceName],[DomainContext] where the [DomainContext] was of the form cn=OracleContext,dc=foo,dc=bar一个 JDBC 连接字符串,看起来像: jdbc:oracle:thin:@ldap://[LDAPHostname1]:[LDAPPort1]/[ServiceName],[DomainContext] ldap://[LDAPHostname2]:[LDAPPort2]/[ServiceName],[DomainContext]其中[DomainContext]的形式为cn=OracleContext,dc=foo,dc=bar

First, you have to install cx_Oracle by following the Oracle documentation .首先,您必须按照Oracle 文档安装 cx_Oracle。

Note that:请注意:

  • cx_Oracle requires a series of library files that are part of the Oracle Instant Client "Basic" or "Basic Light" package (available here ). cx_Oracle 需要一系列库文件,这些文件是 Oracle Instant Client“Basic”或“Basic Light”包(可在此处获得)的一部分。 Let's say we unzip the package under C:\\path\\to\\instant_client_xx_yy假设我们解压C:\\path\\to\\instant_client_xx_yy下的包
  • Depending on the platform you're on, some other requirements are to be filled (like installing some Visual Studio redistributable on Windows)根据您所在的平台,需要满足其他一些要求(例如在 Windows 上安装一些 Visual Studio 可再发行组件)

For the LDAP part, there are two configuration files that are required:对于 LDAP 部分,需要两个配置文件:

  • sqlnet.ora : This is the profile configuration file for Oracle, but mine was simply containing : sqlnet.ora :这是 Oracle 的配置文件配置文件,但我的只是包含:

     NAMES.DIRECTORY_PATH = (LDAP)

    It tells the library to resolve names using LDAP only.它告诉库仅使用 LDAP 解析名称。

  • ldap.ora : This file tells where to look for when resolving names using LDAP. ldap.ora :此文件说明在使用 LDAP 解析名称时要查找的位置。 I knew I was accessing two OID servers, so mine was of the form :我知道我正在访问两个 OID 服务器,所以我的是以下形式:

     DIRECTORY_SERVERS=([LDAPHostname1]:[LDAPPort1], [LDAPHostname2]:[LDAPPort2]) DEFAULT_ADMIN_CONTEXT="dc=foo,dc=bar" DIRECTORY_SERVER_TYPE=oid

    Important Note : I had to remove the cn=OracleContext from the DEFAULT_ADMIN_CONTEXT entry in order to make the name resolution work重要说明:我必须从DEFAULT_ADMIN_CONTEXT条目中删除cn=OracleContext以使名称解析工作

    Let's say those two files were saved under C:\\path\\to\\conf假设这两个文件保存在C:\\path\\to\\conf

And now comes the Python part.现在是 Python 部分。 I used the cx_Oracle.init_oracle_client() method in order to point to the library and configuration files.我使用cx_Oracle.init_oracle_client()方法来指向库和配置文件。 (Note that there are other ways to give cx_Oracle access to those files, like setting environment variables or putting those in predefined places. This is explained under the install guide ) (请注意,还有其他方法可以让 cx_Oracle 访问这些文件,例如设置环境变量或将它们放在预定义的位置。这在安装指南中进行了解释)

Here is a little sample code:这是一个小示例代码:


import cx_Oracle

# username and password retrieved here

cx_Oracle.init_oracle_client(lib_dir=r'C:\path\to\instant_client_xx_yy', config_dir=r'C:\path\to\conf')

try:
    with cx_Oracle.connect(user=username, password=password, dsn='[ServiceName]') as connection:
        cursor = connection.cursor()
        cursor.execute('SELECT * FROM ALL_TAB_COLUMNS')
        # Outputs tables and columns accessible by the user
        for row in cursor:
            print(row[1], '-', row[2])
        cursor.close()

except cx_Oracle.DatabaseError as e:
    print("Oracle Error", e)

The short answer is that you use an ldap.ora configuration file and specify that it is to be used in your sqlnet.ora configuration file.简短的回答是您使用 ldap.ora 配置文件并指定它要在您的 sqlnet.ora 配置文件中使用。 Although this link talks about creating a database link and not directly connecting, the same principle applies and you can connect using any of the services referenced in your LDAP server.尽管此链接讨论的是创建数据库链接而不是直接连接,但适用相同的原则,您可以使用 LDAP 服务器中引用的任何服务进行连接。

http://technologydribble.info/2015/02/10/how-to-create-an-oracle-database-link-using-ldap-authentication/ http://technologydribble.info/2015/02/10/how-to-create-an-oracle-database-link-using-ldap-authentication/

Some more official documentation on how it works can be found here:可以在此处找到有关其工作原理的更多官方文档:

https://docs.oracle.com/cd/B28359_01/network.111/b28317/ldap.htm https://docs.oracle.com/cd/B28359_01/network.111/b28317/ldap.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM