oracle客户端配置

oracle客户端安装

 
下载32位客户端
https://www.oracle.com/database/technologies/instant-client/linux-x86-32-downloads.html



需要安装
oracle客户端,instantclient-basic-linux.x64-11.2.0.4.0.zip

其中 oracle客户端不仅是必须的,而且还需要把它的库文件加载到系统中

vim /etc/ld.so.conf
/opt/app/instantclient_11_2
然后执行ldconfig将库文件直接加载到运行的OS中
ldconfig

上面的操作等价于
rpm -ivh oracle-instantclient-basic-21.8.0.0.0-1.x86_64.rpm
当前(2023-01)最新的客户端是21版本,经测试它可以连接11g,
安装这个后,就可以连接所有的oracle数据库了


还有两个必要条件:
oracle的端口不能被防火墙禁用,默认是禁用的,要么关闭防火墙,要么防火墙开放其端口;
oracle监听程序不能限制IP,默认只限本地访问;

防火墙问题会报超时 cx_Oracle.DatabaseError: ORA-12170: TNS: 连接超时

监听
D:\app\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN 
默认:

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    )
  )

修改为:

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
      (ADDRESS = (PROTOCOL = TCP)(HOST = 0.0.0.0)(PORT = 1521))
    )
  )

实例启动
win11右键我的电脑-->服务,可以找到类似OracleServiceORCL的服务
可以右键服务启动
也可以在cmd命令行执行
net start OracleServiceCASE
net stop OracleServiceCASE

win11重启监听
lsnrctl stop
lsnrctl start
lsnrctl status

 

    

 

    

 


 

  

 


python连接oracle示例

本例运行环境为linux,需要提前安装oracle客户端,前面已进行安装

cx_Oracle包依赖oracle客户端中的这些库文件

import cx_Oracle

conn = cx_Oracle.connect('user','password','ip:port/DB',encoding="UTF-8") 
cur = conn.cursor() 

query = cur.execute("select * from tablename where rownum <= 10")
col = [c[0] for c in cur.description] #获取列名
wq = query.fetchall() #获取数据

cur.close()
conn.close()

import pandas as pd
data = pd.DataFrame(wq,columns=col)

自动关闭连接

Connections should be released when they are no longer needed by calling Connection.close(). Alternatively, you may prefer to let connections be automatically cleaned up when references to them go out of scope. This lets cx_Oracle close dependent resources in the correct order. One other approach is the use of a “with” block, for example:
with cx_Oracle.connect(user=user, password=password,
    dsn="dbhost.example.com/orclpdb1",encoding="UTF-8") as connection:
    cursor = connection.cursor()
    cursor.execute("insert into SomeTable values (:1, :2)",(1, "Some string"))
    connection.commit()

 

    

 
https://www.oracle.com/database/technologies/instant-client/downloads.html

rpm必须使用root用户安装,zip普通用户解压即可

注意这里使用的是64位客户端,同时强调一下,之前安装的rpm是32位的
rsync -rltDv /mnt/d/soft/instantclient-basic-linux.x64-12.2.0.1.0.zip ./
unzip instantclient-basic-linux.x64-12.2.0.1.0.zip
    
export LD_LIBRARY_PATH=~/app/python/instantclient_12_2:$LD_LIBRARY_PATH

export LD_LIBRARY_PATH=/data/jupyter/instantclient_21_15:$LD_LIBRARY_PATH

python中设置LD_LIBRARY_PATH, 系统中设置后则不需要python重复设备

 
import os  

# 设置LD_LIBRARY_PATH环境变量  
# 注意:这里我们假设instantclient_12_2是你的Oracle Instant Client的路径  
oracle_client_path = "/home/ai-aml/app/python/instantclient_12_2"  
os.environ['LD_LIBRARY_PATH'] = oracle_client_path + ':' + os.environ.get('LD_LIBRARY_PATH', '')  
  
# 验证设置是否成功  
print("LD_LIBRARY_PATH:", os.environ['LD_LIBRARY_PATH'])  
  
# 现在你可以尝试导入cx_Oracle并连接到Oracle数据库了  
import cx_Oracle  
  
# 你的数据库连接代码...
    

  
如果设置了 LD_LIBRARY_PATH,则不需要下面的设置了,但在windows上可能还需要
# 如果报DPI-1047  找不到client的lib  用一下两句
#cx.init_oracle_client(lib_dir=r'/home/ai-aml/app/python/instantclient_12_2')
  

 


 


 


 

  

 


python连接pg
rpm -qa | grep python-psycopg2

在线安装
yum install python-psycopg2          
apt-get install python3-psycopg2
apt install libpq-dev 

离线安装rpm 
yum search python-psycopg2
mkdir -p /opt/soft/pg/rpm
yum install --downloadonly --downloaddir=/opt/soft/pg/rpm python-psycopg2.x86_64

离线下载
pip3 download -d /opt/soft/pg/py psycopg2 -i https://pypi.tuna.tsinghua.edu.cn/simple

离线安装 
pip3 install /opt/soft/pg/py/psycopg2

python定时连接数据库

pip install func_timeout

import time 
from func_timeout import func_set_timeout

@func_set_timeout(7)
def select_data(time_limit=6):
    for num in range(0, time_limit):
        time.sleep(1)
        print(num)
参考
oracle客户端下载-夸克网盘 cx-oracle官方API