侧边栏壁纸
博主头像
DBA的笔记本博主等级

think in coding

  • 累计撰写 18 篇文章
  • 累计创建 16 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

python通过oracle客户端连接数据库

张彤
2024-07-24 / 0 评论 / 0 点赞 / 22 阅读 / 57083 字

DALL·E 2024-07-24 11.27.39 - An enhanced digital art illustration emphasizing a Python script connecting to an Oracle database. The focus is on a large, central computer screen di.webp

安装包

  • Anaconda3-2019.10-Linux-x86_64.sh

  • oracle Version 11.2.0.4.0 client for centos 64-bit

  • SQLPlus: Additional libraries and executable for running SQLPlus with Instant Client

注意,python连接Oracle有一个非常苛刻的条件,就是你的python版本,系统版本,目标端的数据库,都必须是一样的位,一般都是64BIT,32位同学想办法改吧!

创建连接用户

adduser dwzyywzt
passwd dwzyywzt
​
# 变更权限,可写
chmod -v u+w /etc/sudoers
​
vi /etc/sudoers
-------------------------------------------------------
## Allow root to run any commands anywhere
root    ALL=(ALL)       ALL
dwzyywzt    ALL=(ALL)       ALL (添加这一行)
-------------------------------------------------------
# 撤销权限
chmod -v u-w /etc/sudoers 

安装python-conda

# 默认软件上载位置是/mnt
cd /mnt/
​
# 赋权
chown dwzyywzt:dwzyywzt Anaconda3-2019.10-Linux-x86_64.sh
​
su - dwzyywzt
chmod +x Anaconda3-2019.10-Linux-x86_64.sh
​
./Anaconda3-2019.10-Linux-x86_64.sh

进入安装界面,按回车看安装须知

# 记得一定要用sudo 不然路径权限会报错
[dwzyywzt@localhost mnt]$ sudo bash Anaconda3-2019.10-Linux-x86_64.sh 
​
Welcome to Anaconda3 2019.10
​
In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue

一直回车直到yes or no,写yes

Do you accept the license terms? [yes|no]
[no] >>> 
Please answer 'yes' or 'no':'
>>> yes
​

接下来,会叫你选择安装路径,我们手动选择/mnt/anaconda3

Anaconda3 will now be installed into this location:
/home/dwzyywzt/anaconda3
​
  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below
​
[/home/dwzyywzt/anaconda3] >>> /mnt/anaconda3
​

安装oracle 客户端

oracle客户端下载网址

使用如下语句,查看需要连接的Oracle数据库版本

select *
from v$version;
​
11g edition release 11.2.0.4.0-64bit production

所以选择 Version 11.2.0.4.0的版本进行下载。我们选择Base版本下载。

  • 下载的时候,需要注册一个Oracle账户,非常快速就可以注册完成了。

  • Basic: All files required to run OCI, OCCI, and JDBC-OCI applications

下载完毕后,通过xftp上载到/mnt目录下

# 赋权
chown dwzyywzt:dwzyywzt instantclient-basic-linux.x64-11.2.0.4.0.zip
​
# 解压
cd /mnt/package
unzip instantclient-basic-linux.x64-11.2.0.4.0.zip 
cd /mnt/package/instantclient_11_2/
ln -s libclntsh.so.11.1 libclntsh.so
# 如果软链错误,需要删除
rm libclntsh.so -y
# 继续加软链
ln -s libocci.so.11.1 libocci.so

依赖安装

yum list|grep libaio
#如果有则不用安装,无则需要
sudo yum install libaio

如果 Instant Client 是这个系统上唯一安装的 Oracle 软件,那么更新运行时链接路径,例如:

sudo sh -c "echo /mnt/package/instantclient_11_2 > \
      /etc/ld.so.conf.d/oracle-instantclient.conf"
​
# ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令.
sudo ldconfig

或者,在运行应用程序之前设置 LD_LIBRARY_PATH 环境变量,例如:

export LD_LIBRARY_PATH=/mnt/package/instantclient_11_2:$LD_LIBRARY_PATH

该变量语句可以添加到配置文件(如 ~/.bash_profile)和应用程序配置文件(如/etc/sysconfig/httpd)中。

如果你打算用客户端进行可选的oracle配置,比如tnsnames.ora, sqlnet.ora, ldap.ora,或者oraaccess.xml,需要提前新增相关的路径,比如

mkdir -p /mnt/package/instantclient_11_2/network/admin

这是与Instant Client链接的应用程序的默认Oracle配置目录。

另外,也可以将Oracle配置文件放入另一个可访问目录中。然后将环境变量TNS_ADMIN设置为该目录名称。

要从 SQL * Plus 包中使用诸如 sqlplus 之类的二进制文件,请将包解压缩到 Basic 包所在的目录,然后更新PATH的环境变量

export PATH=/mnt/package/instantclient_11_2:$PATH

开启你的应用

安装sqlplus

下载地址和上方的客户端在一起,一般是在下方。

解压到和客户端一个文件夹内

unzip -d instantclient_11_2 instantclient-sqlplus-linux.x64-11.2.0.4.0.zip

将sqlplus 软链到/usr/bin 目录下,方便启动

ln -s /mnt/package/instantclient_11_2/sqlplus /usr/bin

测试

sqlplus /nolog
SQL*Plus: Release 11.2.0.4.0 Production on Sat May 7 09:47:18 2022
​
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
​
SQL>

配置网络环境

export ORACLE_HOME=/mnt/package/instantclient_11_2
export TNS_ADMIN=$ORACLE_HOME/network/admin
export ORACLE_SID=orcl
​

创建监听配置文件

cd $TNS_ADMIN
touch tnsnames.ora
vim tnsnames.ora
​

文件中写入以下内容

orcl =
# orcl 是客户端调用的实例名称,理论上没有什么规范
(DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 123.222.34.56)(PORT = 11551))
    # 这里需要填写远程的host和port
    (CONNECT_DATA =
    # 这一部分则严格按照 目标端的tns文件填写
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORA19C.dbaora.com)
    )
  )
​

测试你的连接

sqlplus /nolog
SQL> conn zhangtong/1234567@orcl
Connected.
SQL> select * from dual;  
​
D
-
X
​
​

cx_oracle安装

下载地址

目前最新的版本是8,当然如果你的Oracle库或者python版本比较老旧,需要旧版的oracle,下载地址点这里

python的Oracle开发环境介绍

python 在线安装,指定版本的命令如下

python -m pip install cx-oracle==5.3

下表总结了可用的不同 Python 版本以及相应的存储库和 RPM 名称

Oracle Linux 8

Python Version

AppStream Module

3.9

python39

3.8

python38

3.6

python36

2.7

python27

Oracle Linux 7

Repository: RPM name

Python Version

Base Oracle Linux

Software Collection Library

3.8

ol7_software_collections: rh-python38

3.6

ol7_latest: python3

ol7_software_collections: rh-python36

3.5

ol7_software_collections: rh-python35

3.4

ol7_software_collections: rh-python34

2.7

ol7_latest: python

cx-oracle whil安装包下载地址

离线安装cx-oracle

pip install cx_Oracle-8.3.0-cp37-cp37m-win_amd64.whl

ipython中测试联通性

from sqlalchemy.engine import create_engine
import cx_Oracle
​
DIALECT = 'oracle'
SQL_DRIVER = 'cx_oracle'
USERNAME = 'zhangtong' #enter your username
PASSWORD = '12345678' #enter your password
HOST = '124.223.33.57' #enter the oracle db host url
PORT = 11521 # enter the oracle port number
SERVICE = 'ORA19C.dbaora.com' # enter the oracle db service name
ENGINE_PATH_WIN_AUTH = DIALECT + '+' + SQL_DRIVER + '://' + USERNAME + ':' + PASSWORD +'@' + HOST + ':' + str(PORT) + '/?service_name=' + SERVICE
​
engine = create_engine(ENGINE_PATH_WIN_AUTH)
​
​
#test query
import pandas as pd
test_df = pd.read_sql_query('SELECT * FROM tab', engine)

0

评论区