下载Oracle client需要的 安装包
rpm包下载地址:Oracle官方下载地址
选择系统版本
选择Oracle版本
下载3个rpm安装包
oracle-instantclient12.2-basic-12.2.0.1.0-1.i386.rpm
oracle-instantclient12.2-devel-12.2.0.1.0-1.i386.rpm
oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.i386.rpm
分别安装
rpm -ivh oracle-instantclient12.2-basic-12.2.0.1.0-1.i386.rpm
rpm -ivh oracle-instantclient12.2-devel-12.2.0.1.0-1.i386.rpm
rpm -ivh oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.i386.rpm
mkdir -p /usr/lib/oracle/12.2/client64/network/admin
vim ~/.bash_profile; 或者 /etc/profile 等等
export ORACLE_HOME=/usr/lib/oracle/12.2/client64
export TNS_ADMIN=$ORACLE_HOME/network/admin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export LANG=zh_CN.UTF-8
export NLS_LANG=AMERICAN_AMERICA.utf8
export PATH=$PATH:$ORACLE_HOME/bin
source ~/.bashrc
输入sqlplus命令测试:
[sun@node01 ~] sqlplus
SQL*Plus: Release 12.2.0.1.0 Production on Fri Aug 18 20:21:38 2023
Copyright © 1982, 2016, Oracle. All rights reserved.
一个批量安装的脚本
python 安装cx_oracle
cx_Oracle是一个用于Python的第三方库,提供了与Oracle数据库进行交互的功能。可以在python中用cx_Oracle来连接Oracle数据库。
在线安装
pip3 install cx_Oracle
离线安装
pip3 download cx_Oracle# 在有网络的环境下载到指定目录
pip3 install /xxxx/cx_Oracle-8.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl#复制到其他服务器上安装
编写脚本进行测试
vim oracle_test.py 输入以下内容:
import cx_Oracle
# 连接Oracle
connection = cx_Oracle.connect(user="用户名", password="密码", dsn="主机名:端口号/服务名")
# 创建游标
cursor = connection.cursor()
# 执行SQL
cursor.execute("SELECT * FROM 表名")
# 获取查询结果
result = cursor.fetchall()
# 打印查询结果
for row in result:
print(row)
# 关闭游标和连接
cursor.close()
connection.close()
批量安装脚本
下面是一个批量执行上面安装命令的shell脚本
#!/bin/bash
servers=" 服务器1 服务器2 ....."
env_path="xxxxx"
for server in ${servers}
do
echo "正在连接服务器 $server"
ssh $server "rpm -ivh /xxxxx/oracle-instantclient12.2-basic-12.2.0.1.0-1.x86_64.rpm"
ssh $server "rpm -ivh /xxxxx/oracle-instantclient12.2-devel-12.2.0.1.0-1.x86_64.rpm"
ssh $server "rpm -ivh /xxxxx/oracle-instantclient12.2-sqlplus-12.2.0.1.0-1.x86_64.rpm"
# env
ssh $server "echo 'export ORACLE_HOME=/usr/lib/oracle/12.2/client64' >> ${env_path}"
ssh $server "echo 'export TNS_ADMIN=\$ORACLE_HOME/network/admin' >> ${env_path}"
ssh $server "echo 'export LD_LIBRARY_PATH=\$ORACLE_HOME/lib' >> ${env_path}"
ssh $server "echo 'export LANG=zh_CN.UTF-8' >> ${env_path}"
ssh $server "echo 'export NLS_LANG=AMERICAN_AMERICA.utf8' >> ${env_path}"
ssh $server "echo 'export PATH=\$PATH:\$ORACLE_HOME/bin' >> ${env_path}"
# source
ssh $server "source ${env_path}"
# cx_oracle
ssh $server "pip3 install /xxxxx/cx_Oracle-8.3.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl"
echo "服务器 $server 安装完成"
done
echo "批量安装完成"