一、环境准备
- 预安装包获取:
https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/index.html
也可以直接:wget https://yum.oracle.com/repo/OracleLinux/OL7/latest/x86_64/getPackage/oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
。
然后再安装:
rpm -ivh oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm
- 下载数据库安装包:
下载地址:https://www.oracle.com/cn/database/technologies/oracle19c-linux-downloads.html
二、安装介绍
- 安装预依赖包
[root@localhost ~]# rpm -ivh oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm >yes.txt
warning: oracle-database-preinstall-19c-1.0-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
error: Failed dependencies:
bind-utils is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
compat-libcap1 is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
compat-libstdc++-33 is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
glibc-devel is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
ksh is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
libaio-devel is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
libstdc++-devel is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
net-tools is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
nfs-utils is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
psmisc is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
smartmontools is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
sysstat is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
unzip is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
xorg-x11-utils is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
xorg-x11-xauth is needed by oracle-database-preinstall-19c-1.0-1.el7.x86_64
把上述文字复制到Notepad++编辑器中,首先利用查找替换把“is needed”替换称几个空格,目的是为了方便列编辑。替换好后,按住alt键鼠标列选取并编辑,形成如下的安装依赖指令。
yum install -y bind-utils
yum install -y compat-libcap1
yum install -y compat-libstdc++-33
yum install -y glibc-devel
yum install -y ksh
yum install -y libaio-devel
yum install -y libstdc++-devel
yum install -y net-tools
yum install -y nfs-utils
yum install -y psmisc
yum install -y smartmontools
yum install -y sysstat
yum install -y unzip
yum install -y xorg-x11-utils
yum install -y xorg-x11-xauth
- 安装数据库包
[root@localhost ~]# rpm -ivh oracle-database-ee-19c-1.0-1.x86_64.rpm
warning: oracle-database-ee-19c-1.0-1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID ec551f03: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:oracle-database-ee-19c-1.0-1 ################################# [100%]
[INFO] Executing post installation scripts...
[INFO] Oracle home installed successfully and ready to be configured.
To configure a sample Oracle Database you can execute the following service configuration script as root: /etc/init.d/oracledb_ORCLCDB-19c configure
[root@localhost ~]#
接下来按照提示:
[root@localhost ~]# /etc/init.d/oracledb_ORCLCDB-19c --help
Usage: /etc/init.d/oracledb_ORCLCDB-19c {start|stop|restart|configure|delete}
[root@localhost ~]#
那么用root用户执行安装命令,一路默认即可
/etc/init.d/oracledb_ORCLCDB-19c configure
示例:
[root@localhost ~]# /etc/init.d/oracledb_ORCLCDB-19c configure
Configuring Oracle Database ORCLCDB.
Prepare for db operation
8% complete
Copying database files
31% complete
Creating and starting Oracle instance
32% complete
36% complete
40% complete
43% complete
46% complete
Completing Database Creation
51% complete
54% complete
Creating Pluggable Databases
58% complete
77% complete
Executing Post Configuration Actions
100% complete
Database creation complete. For details check the logfiles at:
/opt/oracle/cfgtoollogs/dbca/ORCLCDB.
Database Information:
Global Database Name:ORCLCDB
System Identifier(SID):ORCLCDB
Look at the log file "/opt/oracle/cfgtoollogs/dbca/ORCLCDB/ORCLCDB.log" for further details.
Database configuration completed successfully. The passwords were auto generated, you must change them by connecting to the database using 'sqlplus / as sysdba' as the oracle user.
[root@localhost ~]#
到这里,其实数据库已经安装好了。
三、使用介绍
- 设置环境变量
登录root用户,vim /home/oracle/.bashrc
# Oracle相关环境配置
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=${ORACLE_BASE}/product/19c/dbhome_1
export ORACLE_SID=ORCLCDB
export ORACLE_UNQNAME=$ORACLE_SID
export PATH=${ORACLE_HOME}/bin:/user/sbin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
# 设置和服务器一样的编码
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
# 配置地区语言时间
export LC_ALL="en_US"
export LANG="en_US"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
# 设置sqlplus可以使用回退等
alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'
alias lsnrctl='rlwrap lsnrctl'
立即生效:source vim /home/oracle/.bashrc
- 安装rlwrap使用方向键
采用RPM安装或者源码安装
https://www.rpmfind.net/linux/rpm2html/search.php?query=rlwrap
这里采用源码安装:
使用root用户登录
wget https://mirrors.aliyun.com/macports/distfiles/rlwrap/rlwrap-0.46.1.tar.gz
tar zxvf rlwrap-0.46.1.tar.gz
cd rlwrap-0.46.1
autoreconf --install
./configure
make && make install && make clean
- 切换oracle 用户登录
[root@localhost ~]# id oracle
uid=54321(oracle) gid=54321(oinstall) groups=54321(oinstall),54322(dba),54323(oper),54324(backupdba),54325(dgdba),54326(kmdba),54330(racdba)
[root@localhost ~]#
采用rpm安装的方式,默认给我们安装了oracle 用户和进行了一些列的配置
su - oracle
sqlplus /nolog
conn sys as sysdba;
示例:
[oracle@localhost ~]$ sqlplus /nolog
SQL*Plus: Release 19.0.0.0.0 - Production on Sun Jun 1 18:51:08 2025
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
SQL> conn sys as sysdba;
Enter password:
Connected.
SQL> select 'x' from dual;
'
-
x
SQL>
到这里,就表示已经安装好,可以用了。
- 开机启动
在/etc/rc.d/rc.local文件中添加启动命令。
vim /etc/rc.d/rc.local
su root -lc '/etc/init.d/oracledb_ORCLCDB-19c start'
这里需要注意的是该文件需要添加执行权限才能使用。
chmod 750 /etc/rc.d/rc.local
重启reboot之后,查看一下状态
[root@localhost ~]# su - oracle
Last login: Sun Jun 1 19:06:41 CST 2025
[oracle@localhost ~]$ lsnrctl status
LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 01-JUN-2025 19:07:07
Copyright (c) 1991, 2019, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 01-JUN-2025 19:06:41
Uptime 0 days 0 hr. 0 min. 25 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Listener Parameter File /opt/oracle/product/19c/dbhome_1/network/admin/listener.ora
Listener Log File /opt/oracle/diag/tnslsnr/localhost/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=localhost)(PORT=5500))(Security=(my_wallet_directory=/opt/oracle/product/19c/dbhome_1/admin/ORCLCDB/xdb_wallet))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "3680bf68299c2859e065000000000001" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "ORCLCDB" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "ORCLCDBXDB" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
Service "orclpdb1" has 1 instance(s).
Instance "ORCLCDB", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@localhost ~]$
然后登录看看是否正常:
SQL> conn sys as sysdba;
Enter password:
Connected.
SQL> select username from dba_users where account_status='OPEN';
USERNAME
--------------------------------------------------------------------------------
SYS
SYSTEM
SQL>
至此,就表示已经完全搭建好了。
- 创建演示用户
由于使用了容器,因此要加上c##
来创建,加上alter session set "_ORACLE_SCRIPT"=true;
就可以不使用前缀也可以了。
[oracle@localhost ~]$ sqlplus /nolog
SQL*Plus: Release 19.0.0.0.0 - Production on Sun Jun 1 19:18:55 2025
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle. All rights reserved.
SQL> conn sys as sysdba;
Enter password:
Connected.
SQL> create user c##scott identified by tiger;
User created.
SQL> grant connect,resource,unlimited tablespace to c##scott container=all;
Grant succeeded.
SQL> alter user c##scott default tablespace users;
User altered.
SQL> alter user c##scott temporary tablespace temp;
User altered.
SQL> connect c##scott/tiger
Connected.
SQL> show user
USER is "C##SCOTT"
SQL>
连接到 数据库=> 帐号:c##scott,密码为:tiger
连接成功后新建查询并执行如下sql
CREATE TABLE DEPT
(DEPTNO NUMBER(2) CONSTRAINT PK_DEPT PRIMARY KEY,
DNAME VARCHAR2(14) ,
LOC VARCHAR2(13) ) ;
CREATE TABLE EMP
(EMPNO NUMBER(4) CONSTRAINT PK_EMP PRIMARY KEY,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2) CONSTRAINT FK_DEPTNO REFERENCES DEPT);
INSERT INTO DEPT VALUES
(10,'ACCOUNTING','NEW YORK');
INSERT INTO DEPT VALUES (20,'RESEARCH','DALLAS');
INSERT INTO DEPT VALUES
(30,'SALES','CHICAGO');
INSERT INTO DEPT VALUES
(40,'OPERATIONS','BOSTON');
INSERT INTO EMP VALUES
(7369,'SMITH','CLERK',7902,to_date('17-12-1980','dd-mm-yyyy'),800,NULL,20);
INSERT INTO EMP VALUES
(7499,'ALLEN','SALESMAN',7698,to_date('20-2-1981','dd-mm-yyyy'),1600,300,30);
INSERT INTO EMP VALUES
(7521,'WARD','SALESMAN',7698,to_date('22-2-1981','dd-mm-yyyy'),1250,500,30);
INSERT INTO EMP VALUES
(7566,'JONES','MANAGER',7839,to_date('2-4-1981','dd-mm-yyyy'),2975,NULL,20);
INSERT INTO EMP VALUES
(7654,'MARTIN','SALESMAN',7698,to_date('28-9-1981','dd-mm-yyyy'),1250,1400,30);
INSERT INTO EMP VALUES
(7698,'BLAKE','MANAGER',7839,to_date('1-5-1981','dd-mm-yyyy'),2850,NULL,30);
INSERT INTO EMP VALUES
(7782,'CLARK','MANAGER',7839,to_date('9-6-1981','dd-mm-yyyy'),2450,NULL,10);
INSERT INTO EMP VALUES
(7788,'SCOTT','ANALYST',7566,to_date('13-JUL-87','dd-mm-rr')-85,3000,NULL,20);
INSERT INTO EMP VALUES
(7839,'KING','PRESIDENT',NULL,to_date('17-11-1981','dd-mm-yyyy'),5000,NULL,10);
INSERT INTO EMP VALUES
(7844,'TURNER','SALESMAN',7698,to_date('8-9-1981','dd-mm-yyyy'),1500,0,30);
INSERT INTO EMP VALUES
(7876,'ADAMS','CLERK',7788,to_date('13-JUL-87', 'dd-mm-rr')-51,1100,NULL,20);
INSERT INTO EMP VALUES
(7900,'JAMES','CLERK',7698,to_date('3-12-1981','dd-mm-yyyy'),950,NULL,30);
INSERT INTO EMP VALUES
(7902,'FORD','ANALYST',7566,to_date('3-12-1981','dd-mm-yyyy'),3000,NULL,20);
INSERT INTO EMP VALUES
(7934,'MILLER','CLERK',7782,to_date('23-1-1982','dd-mm-yyyy'),1300,NULL,10);
CREATE TABLE BONUS
(
ENAME VARCHAR2(10) ,
JOB VARCHAR2(9) ,
SAL NUMBER,
COMM NUMBER
) ;
CREATE TABLE SALGRADE
( GRADE NUMBER,
LOSAL NUMBER,
HISAL NUMBER );
INSERT INTO SALGRADE VALUES (1,700,1200);
INSERT INTO SALGRADE VALUES (2,1201,1400);
INSERT INTO SALGRADE VALUES (3,1401,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
COMMIT;
查看一下,已经安装好了。
SQL> set linesize 200 pagesize 1000;
SQL>
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 1980-12-17 00:00:00 800 20
7499 ALLEN SALESMAN 7698 1981-02-20 00:00:00 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 00:00:00 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 00:00:00 2975 20
7654 MARTIN SALESMAN 7698 1981-09-28 00:00:00 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 00:00:00 2850 30
7782 CLARK MANAGER 7839 1981-06-09 00:00:00 2450 10
7788 SCOTT ANALYST 7566 1987-04-19 00:00:00 3000 20
7839 KING PRESIDENT 1981-11-17 00:00:00 5000 10
7844 TURNER SALESMAN 7698 1981-09-08 00:00:00 1500 0 30
7876 ADAMS CLERK 7788 1987-05-23 00:00:00 1100 20
7900 JAMES CLERK 7698 1981-12-03 00:00:00 950 30
7902 FORD ANALYST 7566 1981-12-03 00:00:00 3000 20
7934 MILLER CLERK 7782 1982-01-23 00:00:00 1300 10
14 rows selected.
SQL>
至此,19C已经全部搭建好,并建立了演示用户及数据。