1、检查环境并下载cx_Oracle对应包
[root@kkk ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.4 (Maipo)
[root@kkk ~]# python3 -V
Python 3.6.5
cx_Oracle源码包:
https://pypi.python.org/pypi/cx_Oracle/5.2.1#downloads
2、上传cx_Oracle包到Linux并解压
[root@kkk soft]# ls -l /soft/cx_Oracle-5.2.1.tar.gz
-rw-r--r-- 1 root root 113416 Jan 27 20:40 /soft/cx_Oracle-5.2.1.tar.gz
tar -xzvf cx_Oracle-5.2.1.tar.gz
[root@kkk soft]# ls -ld cx_Oracle-5.2.1*
drwxrwxr-x 7 kkk kkk 4096 Jan 27 20:58 cx_Oracle-5.2.1
-rw-r--r-- 1 root root 113416 Jan 27 20:40 cx_Oracle-5.2.1.tar.gz
3、安装之前root用户相关环境变量设置
vi /etc/profile
export ORACLE_HOME=/u01/app/oracle/product/19.2.0.0.0/dbhome_1
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME/bin:$PATH
source /etc/profile
4、开始安装
cd /soft/cx_Oracle-5.2.1/
[root@kkk cx_Oracle-5.2.1]# python3 setup.py build
running build
running build_ext
building 'cx_Oracle' extension
gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/u01/app/oracle/product/19.2.0.0.0/dbhome_1/rdbms/public -I/u01/app/oracle/product/19.2.0.0.0/dbhome_1/sdk/include -I/usr/local/python3/include/python3.6m -c cx_Oracle.c -o build/temp.linux-x86_64-3.6-11g/cx_Oracle.o -DBUILD_VERSION=5.2.1
In file included from /u01/app/oracle/product/19.2.0.0.0/dbhome_1/rdbms/public/oci.h:3883:0,
from cx_Oracle.c:10:
/u01/app/oracle/product/19.2.0.0.0/dbhome_1/rdbms/public/ociap.h:11070:1: warning: function declaration isn?. a prototype [-Wstrict-prototypes]
void OCIThreadProcessInit();
^
/u01/app/oracle/product/19.2.0.0.0/dbhome_1/rdbms/public/ociap.h:11076:1: warning: function declaration isn?. a prototype [-Wstrict-prototypes]
boolean OCIThreadIsMulti();
^
In file included from /u01/app/oracle/product/19.2.0.0.0/dbhome_1/rdbms/public/oci.h:3886:0,
from cx_Oracle.c:10:
/u01/app/oracle/product/19.2.0.0.0/dbhome_1/rdbms/public/ocixmldb.h:216:1: warning: function declaration isn?. a prototype [-Wstrict-prototypes]
ub1 OCIXmlGetDiffBndVersion();
^
In file included from Variable.c:176:0,
from Cursor.c:246,
from Connection.c:761,
from SessionPool.c:138,
from cx_Oracle.c:203:
ObjectVar.c: In function ?.bjectVar_Initialize?.
ObjectVar.c:118:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < self->allocatedElements; i++) {
^
ObjectVar.c: In function ?.bjectVar_Finalize?.
ObjectVar.c:135:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < self->allocatedElements; i++) {
^
In file included from Cursor.c:246:0,
from Connection.c:761,
from SessionPool.c:138,
from cx_Oracle.c:203:
Variable.c: In function ?.ariable_NewByOutputTypeHandler?.
Variable.c:926:32: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (var->allocatedElements < cursor->fetchArraySize) {
^
In file included from Connection.c:761:0,
from SessionPool.c:138,
from cx_Oracle.c:203:
Cursor.c: In function ?.ursor_GetArrayDMLRowCounts?.
Cursor.c:2440:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < rowCountArraySize; i++) {
^
In file included from Connection.c:764:0,
from SessionPool.c:138,
from cx_Oracle.c:203:
Subscription.c: In function ?.essageTable_Initialize?.
Subscription.c:451:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < numRows; i++) {
^
Subscription.c: In function ?.essageQuery_Initialize?.
Subscription.c:526:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < numTables; i++) {
^
Subscription.c: In function ?.essage_Initialize?.
Subscription.c:618:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < numTables; i++) {
^
Subscription.c:660:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < numQueries; i++) {
^
creating build/lib.linux-x86_64-3.6-11g
gcc -pthread -shared build/temp.linux-x86_64-3.6-11g/cx_Oracle.o -L/u01/app/oracle/product/19.2.0.0.0/dbhome_1/lib -lclntsh -o build/lib.linux-x86_64-3.6-11g/cx_Oracle.cpython-36m-x86_64-linux-gnu.so
[root@kkk cx_Oracle-5.2.1]# python3 setup.py install
running install
running bdist_egg
running egg_info
writing cx_Oracle.egg-info/PKG-INFO
writing dependency_links to cx_Oracle.egg-info/dependency_links.txt
writing top-level names to cx_Oracle.egg-info/top_level.txt
reading manifest file 'cx_Oracle.egg-info/SOURCES.txt'
reading manifest template 'MANIFEST.in'
writing manifest file 'cx_Oracle.egg-info/SOURCES.txt'
installing library code to build/bdist.linux-x86_64/egg
running install_lib
running build_ext
creating build/bdist.linux-x86_64
creating build/bdist.linux-x86_64/egg
copying build/lib.linux-x86_64-3.6-11g/cx_Oracle.cpython-36m-x86_64-linux-gnu.so -> build/bdist.linux-x86_64/egg
creating stub loader for cx_Oracle.cpython-36m-x86_64-linux-gnu.so
byte-compiling build/bdist.linux-x86_64/egg/cx_Oracle.py to cx_Oracle.cpython-36.pyc
creating build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/PKG-INFO -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/SOURCES.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/dependency_links.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
copying cx_Oracle.egg-info/top_level.txt -> build/bdist.linux-x86_64/egg/EGG-INFO
writing build/bdist.linux-x86_64/egg/EGG-INFO/native_libs.txt
zip_safe flag not set; analyzing archive contents...
__pycache__.cx_Oracle.cpython-36: module references __file__
creating dist
creating 'dist/cx_Oracle-5.2.1-py3.6-linux-x86_64.egg' and adding 'build/bdist.linux-x86_64/egg' to it
removing 'build/bdist.linux-x86_64/egg' (and everything under it)
Processing cx_Oracle-5.2.1-py3.6-linux-x86_64.egg
creating /usr/local/python3/lib/python3.6/site-packages/cx_Oracle-5.2.1-py3.6-linux-x86_64.egg
Extracting cx_Oracle-5.2.1-py3.6-linux-x86_64.egg to /usr/local/python3/lib/python3.6/site-packages
Adding cx-Oracle 5.2.1 to easy-install.pth file
Installed /usr/local/python3/lib/python3.6/site-packages/cx_Oracle-5.2.1-py3.6-linux-x86_64.egg
Processing dependencies for cx-Oracle==5.2.1
Finished processing dependencies for cx-Oracle==5.2.1
5、测试是否安装成功
Python端:
[root@kkk soft]# python3
Python 3.6.5 (default, Jan 24 2020, 11:43:31)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> db1 = cx_Oracle.connect('scott/scott@192.168.59.16:1521/pdb01')
>>> print(db1.version)
19.0.0.0.0
Oracle端:
[oracle@kkk ~]$ sqlplus / as sysdba;
SQL*Plus: Release 19.0.0.0.0 - Production on Mon Jan 27 21:59:11 2020
Version 19.2.0.0.0
Copyright (c) 1982, 2018, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.2.0.0.0
6、安装时遇到的错误
ImportError: libclntsh.so.19.1: cannot open shared object file: No such file or directory
导入模块时会用到$ORACLE_HOME/lib将其加入到$LD_LIBRARY_PATH中
/etc/profie下添加
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
source /etc/profile