MacOS下使用Oracle客户端

前言

使用Python进行数据建模时用到Oracle数据库,开发机用的是MacBook。在此之前并没有过Mac下使用Oracle数据库的经历,查过官网资料,配置比较复杂。

Installation

Installing Instant Client 12.2 on macOS

Instant Client 12.2 supports macOS High Sierra, Sierra and El Capitan.

  1. Download the desired Instant Client ZIP files. All installations require the Basic or Basic Light package.

  2. Unzip the packages into a single directory such as ~/instantclient_12_2 that is accessible to your application. For example:

cd ~
unzip instantclient-basic-macos.x64-12.2.0.1.0.zip

  1. Add links to $HOME/lib or /usr/local/lib to enable applications to find the libraries. For example, OCI based applications could do:

mkdir ~/lib
ln -s ~/instantclient_12_2/libclntsh.dylib ~/lib/
ln -s ~/instantclient_12_2/libclntsh.dylib.12.1 ~/lib/

Alternatively, copy the required libraries. For example, to use OCI applications:

mkdir ~/lib
cp /opt/oracle/instantclient_12_2/libclntsh.dylib.12.1 ~/lib/
cp /opt/oracle/instantclient_12_2/libclntshcore.dylib.12.1 ~/lib/
cp /opt/oracle/instantclient_12_2/libons.dylib ~/lib/
cp /opt/oracle/instantclient_12_2/libnnz12.dylib ~/lib/
cp /opt/oracle/instantclient_12_2/libociei.dylib ~/lib/
ln -s ~/lib/libclntsh.dylib.12.1 ~/lib/libclntsh.dylib

Note: Custom OCI applications, such as those that bundle Instant Client, may want to link with -rpath set to the directory containing Instant Client >12.2 instead of relying on libraries being in ~/lib.

  1. If you intend to co-locate optional Oracle configuration files such as tnsnames.ora, sqlnet.ora, ldap.ora, or oraaccess.xml with Instant Client, >then create a network/admin subdirectory. For example:

mkdir -p ~/instantclient_12_2/network/admin

This is the default Oracle configuration directory for applications linked with this Instant Client.

Alternatively, Oracle configuration files can be put in another, accessible directory. Then set the environment variable TNS_ADMIN to that directory >name.

  1. To use binaries such as sqlplus from the SQL*Plus package, unzip the package to the same directory as the Basic package and then update your PATH >environment variable, for example:

export PATH=~/instantclient_12_2:$PATH

  1. Run SQL*Plus and connect using your database credentials and connection string:

sqlplus hr/welcome@localhost/orclpdb

于是把以往的经验结合google总结出较为简单的客户端配置方法,测试可用。

软件环境

软件 版本 文件名
macOS 10.13.6
Python 3.7.0
cx-Oracle 6.4.1
Instant Client for macOS 12.2.0.1.0 instantclient-basic-macos.x64-12.2.0.1.0-2.zip

步骤

1. 下载Instant Client for macOS

Instant Client Downloads for macOS (Intel x86)

2. 安装

将instantclient-basic-macos.x64-12.2.0.1.0-2.zip解压到用户根目录~下,解压得到目录instantclient_12_2

anyone$ pwd
/Users/anyone
anyone$ unzip instantclient-basic-macos.x64-12.2.0.1.0-2.zip
Archive:  instantclient-basic-macos.x64-12.2.0.1.0-2.zip
  inflating: instantclient_12_2/BASIC_README  
  inflating: instantclient_12_2/adrci  
  inflating: instantclient_12_2/genezi  
    linking: instantclient_12_2/libclntsh.dylib  -> libclntsh.dylib.12.1 
  inflating: instantclient_12_2/libclntsh.dylib.12.1  
  inflating: instantclient_12_2/libclntshcore.dylib.12.1  
  inflating: instantclient_12_2/libnnz12.dylib  
    linking: instantclient_12_2/libocci.dylib  -> libocci.dylib.12.1 
  inflating: instantclient_12_2/libocci.dylib.12.1  
  inflating: instantclient_12_2/libociei.dylib  
  inflating: instantclient_12_2/libocijdbc12.dylib  
  inflating: instantclient_12_2/libons.dylib  
  inflating: instantclient_12_2/liboramysql12.dylib  
  inflating: instantclient_12_2/ojdbc8.jar  
  inflating: instantclient_12_2/uidrvci  
  inflating: instantclient_12_2/xstreams.jar  
finishing deferred symbolic links:
  instantclient_12_2/libclntsh.dylib -> libclntsh.dylib.12.1
  instantclient_12_2/libocci.dylib -> libocci.dylib.12.1
anyone$ ls -l
-rw-r--r--@  1 anyone  staff  68160048  9  5 14:49 instantclient-basic-macos.x64-12.2.0.1.0-2.zip
drwxr-xr-x@ 19 anyone  staff       608  9  5 17:00 instantclient_12_2

3. 环境变量

系统用的是bash,向.bash_profile中添加Oracle环境变量(可根据实际情况调整)。

anyone$ vi ~/.bash_profile
export ORACLE_HOME=~/instantclient_12_2
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export SQLPATH=$ORACLE_HOME
export NLS_LANG=AMERICAN_AMERICA.UTF8
export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS"
export PATH=$PATH:$ORACLE_HOME:.

重新登录终端或执行source ~/.bash_profile使环境变量生效。

4. 连接测试

将测试代码存到oracle_con.py文件中,执行查看结果:

import cx_Oracle

def test():
    conn = cx_Oracle.connect("user/password@IP/SERVICE_NAME")
    cur = conn.cursor()
    r = cur.execute("select sysdate from dual")
    print(r.fetchall())

if __name__ == '__main__':
    test()
antone$ python3 oracle_con.py
[(datetime.datetime(2018, 9, 12, 10, 23, 45),)]

5. 总结

首次在macOS下使用Oracle客户端,看过官网配置文档实在是复杂,经过摸索发现并没有那么艰难,凡事多思考。

你可能感兴趣的:(MacOS下使用Oracle客户端)