Oracle 19c 安装使用记录
Oracle 数据库安装及连接操作。
准备工作
下载 Oracle 19c 安装包,下载地址:https://www.oracle.com/database/technologies/oracle-database-software-downloads.html#19c
下载 Oracle 19c 预安装包,下载地址:http://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
安装过程
上传 rpm 包到服务器。
安装 Oracle 预安装rpm包。
yum -y localinstall oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
安装 Oracle 安装rpm包。
yum -y localinstall oracle-database-ee-19c-1.0-1.x86_64.rpm
配置 Oracle 环境。
/etc/init.d/oracledb_ORCLCDB-19c configure
配置过程执行结束后,查询 Oracle 进程,Oracle 进程存在表示安装完成。
创建 Oracle 用户,并配置 Orace 用户的环境变量。
执行 sqlplus / as sysdba 校验 Oracle 数据库是否启动成功。
使用连接工具测试连接是否成功。
Oracle 数据库启停操作
/etc/init.d/oracledb_ORCLCDB-19c start|stop|restart|configure|delete
Oracle 数据库简单操作
创建表空间
create tablespace tab_name
datafile 'path'
size n
[autoextend on next n1 maxsize m /of]
[permanent]
[extent management local/dictionary];
# tab_name 表空间的名字
# path 数据文件路径
# size n 指定数据文件的大小
create tablespace euansu datafile '/home/oracle/data' size 100M;
创建临时表空间
create temporary tablespace temp_name tempfile 'path' size n;
# tempname 表空间的名字
# path 临时表空间数据文件的位置
# size m 表示临时表空间的大小
create temporary tablespace euansu_temp tempfile '/home/oracle/euansu_temp' size 10M;
创建用户
create user euansu identified by "suwenhui123" default tablespace euansu temporary tablespace euansu_temp;
# tablespace 表空间
# temporary tablespace 临时表空间
# 创建用户时会报65096的错误,执行如下语句即可:
alter session set "_ORACLE_SCRIPT"=true;
Python连接Oracle
安装 cx_Oracle 第三方库
最新 cx_Oracle 并不支持 Python2.7,这里下载支持 Python2.7 的7.3.0 版本,下载地址:https://files.pythonhosted.org/packages/5e/e3/cfd4f413f8d47dd2aee09273d9bb3ee6aa7384e0a04e191d703c2199eb93/cx_Oracle-7.3.0.tar.gz
# 解压 cx_Oracle 源码包
tar -zxvf cx_Oracle-7.3.0.tar.gz
# 安装 cx_Oracle 第三方库
python2.7 setup.py install
安装 Oracle Instant Client
通过 python 连接 oracle 需要使用 cx_Oracle 第三方库,要求主机提前安装 Oracle Instant Client,否则连接 Oracle 会报如下错误。
下载 Oracle Instant Client
下载地址:https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html,下载对应版本的 Oracle Instant Client包即可。
创建 Oracle Instant Clinet 所需目录
sudo mkdir -p /opt/oracle
sudo mv instantclient-basic-linux.x64-21.7.0.0.0dbru.zip /opt/oracle/
cd /opt/oracle
sudo unzip instantclient-basic-linux.x64-21.7.0.0.0dbru.zip
安装系统 libaio 包
sudo yum install libaio
创建 Instant Client 的 .so 文件依赖
sudo sh -c "echo /opt/oracle/instantclient_21_7 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig
添加 Instant Clinet 的环境变量
export LD_LIBRARY_PATH=/opt/oracle/instantclient_21_7:$LD_LIBRARY_PATH
测试连接
Python 连接 Oracle 封装代码
import cx_Oracle
class OracleApi(object):
def __init__(self, host, port, user, password, instant):
self.host = host
self.port = port
self.user = user
self.password = password
self.instant = instant
def connect(self):
self.conn = cx_Oracle.connect(self.user, self.password, '%s:%s/%s' % (self.host, self.port,self.instant))
self.cursor = self.conn.cursor()
def close(self):
self.cursor.close()
self.conn.close()
def get_one(self, sql, params=()):
result = None
try:
self.connect()
self.cursor.execute(sql, params)
result = self.cursor.fetchone()
self.close()
except Exception, e:
print e.message
return result
def get_all(self, sql):
list = ()
try:
self.connect()
self.cursor.execute(sql)
list = self.cursor.fetchall()
self.close()
except Exception, e:
print e.message
return list