cx_Oracleインストール

  • 前提パッケージをインストール
$ cat /etc/redhat-release 
CentOS Linux release 6.0 (Final)
$ rpm -qa | grep -E 'gcc|python-devel'
gcc-4.4.4-13.el6.x86_64
libgcc-4.4.4-13.el6.x86_64
python-devel-2.6.5-3.el6_0.2.x86_64

※何も表示されない(インストールされていない)場合は下記コマンドを実行
$ sudo yum install gcc python-devel
  • ダウンロード(最新版5.1)
$ cd /usr/local/src; pwd
 /usr/local/src
$ wget http://sourceforge.net/projects/cx-oracle/files/5.1/cx_Oracle-5.1.tar.gz/download
$ tar xfz cx_Oracle-5.1.tar.gz ; echo $?
0
$ ll -d /usr/local/src/cx_Oracle-5.1
  • 環境変数
$ su -
# . /home/oracle/.bash_profile       # oracle用プロファイルの読み込み
# echo $ORACLE_HOME
/u01/app/oracle/product/11.2.0/xe  # 定義されていること
# echo $LD_LIBRARY_PATH
/u01/app/oracle/product/11.2.0/xe/lib/:/lib:/usr/lib  # 定義されていること
  • インストール
#  cd /usr/local/src/cx_Oracle-5.1; pwd
/usr/local/src/cx_Oracle-5.1
#
# python setup.py build
running build
running build_ext
building 'cx_Oracle' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/u01/app/oracle/product/11.2.0/xe/rdbms/demo -I/u01/app/oracle/product/11.2.0/xe/rdbms/public -I/usr/include/python2.6 -c cx_Oracle.c -o build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -DBUILD_VERSION=5.1
creating build/lib.linux-x86_64-2.6-11g
gcc -pthread -shared build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -L/u01/app/oracle/product/11.2.0/xe/lib -L/u01/app/oracle/product/11.2.0/xe -L/usr/lib64 -lclntsh -lpython2.6 -o build/lib.linux-x86_64-2.6-11g/cx_Oracle.so
# python setup.py install
running install
running build
running build_ext
running install_lib
copying build/lib.linux-x86_64-2.6-11g/cx_Oracle.so -> /usr/lib64/python2.6/site-packages
running install_egg_info
Writing /usr/lib64/python2.6/site-packages/cx_Oracle-5.1-py2.6.egg-inf

接続確認

Oracle環境設定

  • ユーザ作成
SQL> create user HOGE
  2  identified by passwd
  3  default tablespace USERS
  4  temporary tablespace TEMP;

ユーザーが作成されました。

SQL> grant CONNECT,RESOURCE to HOGE;

権限付与が成功しました。

SQL> select GRANTEE,GRANTED_ROLE from dba_role_privs where GRANTEE='HOGE';

GRANTEE 		       GRANTED_ROLE
------------------------------ ------------------------------
HOGE			       CONNECT
HOGE			       RESOURCE

接続プログラム

  • Oracle Versinonを表示させる簡易プログラム
$ vi conn.py 
#!/usr/bin/python
# vim: fileencoding=utf-8

import cx_Oracle

con = cx_Oracle.connect('HOGE/passwd@127.0.0.1/XE')
print con.version
con.close()

$ chmod u+x ./conn.py 
$ ./conn.py 
11.2.0.2.0