網站首頁 編程語言 正文
Python連接Oracle本地測試
依賴安裝準備
Python、鏈接Oracle需要Python依賴和本地Oracle客戶端,測試環境Oracle版本12.1.0.2.0,開發和測試環境為linux,先安裝linux客戶端,選擇zip解壓免安裝版本
Oracle linux客戶端
解壓到某個目錄
unzip instantclient-basic-linux.x64-12.1.0.2.0.zip
解壓后新建/network/admin文件夾
cd /opt/instantclient_12_1/
mkdir -p /network/admin
修改root用戶的環境變量
vim /etc/profile
export ORACLE_HOME=/opt/instantclient_12_1
export TNS_ADMIN=$ORACLE_HOME/network/admin
export NLS_LANG="SIMPLIFIED CHINESE_CHINA".ZHS16GBK
export NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
export LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
export PATH=$ORACLE_HOME:$PATH
source /etc/profile
下一步安裝Python依賴
pip install cx_Oracle
Python腳本測試
root@ubuntu:~# python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle as cx
>>> con = cx.connect('username', 'password', 'xxx.xxx.xxx.xxx:1521/ORCL')
>>> cursor = con.cursor()
>>> cursor.execute("select * from emp")
<cx_Oracle.Cursor on <cx_Oracle.Connection to c##als770ud1@192.168.61.79:1521/ORCL>>
>>> cursor.fetchall()
[(1, '張三'), (2, '李四'), (3, '王五')]
>>>
制作Docker鏡像
創建Dockerfile
touch Dockerfile
# 將oracle本地客戶端文件夾移動到同一級目錄
cp -r /opt/instantclient_12_1/ ./
Dockerfile
FROM python:3.7
ENV PIPURL "https://mirrors.aliyun.com/pypi/simple/"
RUN pip install cx_Oracle --default-timeout=1000
COPY instantclient_12_1 /opt/instantclient_12_1
ENV ORACLE_HOME=/opt/instantclient_12_1
ENV TNS_ADMIN=$ORACLE_HOME/network/admin
ENV NLS_LANG="SIMPLIFIED CHINESE_CHINA".ZHS16GBK
ENV NLS_DATE_FORMAT="yyyy-mm-dd hh24:mi:ss"
ENV LD_LIBRARY_PATH=$ORACLE_HOME:$LD_LIBRARY_PATH
ENV PATH=$ORACLE_HOME:$PATH
RUN apt-get update
RUN apt-get install -y libaio1
鏡像構建
docker build -t xiaogp/python_oraqcle:v3 .
構建完成
root@ubuntu:~/docker/PYTHON_ORACLE# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
xiaogp/python_oraqcle v3 bb0100d9c3f5 39 seconds ago 1.1GB
啟動鏡像測試一下
root@ubuntu:~/docker/PYTHON_ORACLE# docker run -it bb0100d9c3f5 /bin/bash
root@fbff875ba4d5:/# python
Python 3.7.9 (default, Jan 12 2021, 17:26:22)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle as cx
>>> con = cx.connect('username', 'password', 'xxx.xxx.xxx.xxx:1521/ORCL')
>>> cursor = con.cursor()
>>> cursor.execute("select * from emp")
<cx_Oracle.Cursor on <cx_Oracle.Connection to c##als770ud1@192.168.61.79:1521/ORCL>>
>>> cursor.fetchall()
[(1, '張三'), (2, '李四'), (3, '王五')]
可以鏈接,制作結束
原文鏈接:https://www.jianshu.com/p/432801a6e73f
相關推薦
- 2022-07-12 jmm內存模型及volatile實現原理
- 2022-10-29 Android 開發 | API 指南- Content Provider 應用程序的使用方法
- 2022-03-14 @Builder 實際參數列表和形式參數列表長度不同
- 2023-06-13 C語言中函數返回值不一致問題_C 語言
- 2022-04-18 C語言數組全面總結梳理_C 語言
- 2022-01-20 關于 Symbol() 能不能當作 key值使用
- 2023-04-18 C#?TabControl手動觸發DrawItem的實現_C#教程
- 2022-03-29 關于使用Redisson訂閱數問題_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支