1.環境準備
軟件準備
序號 軟件 下載地址
1 VirtualBox
https://www.virtualbox.org/wiki/Downloads
2 CentOS Stream 8
https://mirrors.tuna.tsinghua.edu.cn/centos/8-stream/isos/x86_64/CentOS-Stream-8-x86_64-latest-dvd1.iso
3 oracle-database-free-23c
# cd ~/Downloads
wget https://download.oracle.com/otn-pub/otn_software/db-free/oracle-database-free-23c-1.0-1.el8.x86_64.rpm
4 oracle-database-preinstall-23c
wget https://yum.oracle.com/repo/OracleLinux/OL8/developer/x86_64/getPackage/oracle-database-preinstall-23c-1.0-0.5.el8.x86_64.rpm
5 rlwrap
wget https://github.com/hanslub42/rlwrap/releases/download/0.46.1/rlwrap-0.46.1.tar.gz
2.安裝操作系統
操作系統使用virtualbox虛擬機安裝的centos8服務器,該步驟省略,教程很多的。
該環境是有網的,所以不設置yum源,如果是沒網的內網環境,自行掛載鏡像包作為yum源。
服務器使用的是ubuntu 22.04 上 Oracle VM VirtualBox 虛擬機安裝的 CentOS 8 ,基于windows和基于linux安裝虛擬機沒有影響。
服務器的規格是: 2C4G
CentOS 8 采用最小化安裝
修改主機名和配置hosts
是要設置的主機名: centos8
是主機的 IP: 10.0.2.15
設置主機名
hostnamectl set-hostname centos8 # 設置
hostname # 查看
1
2
關閉防火墻與Selinux
防火墻和selinux按照需求關閉,我這是測試環境,索性關閉
關閉 selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
setenforce 0
輸入 getenforce 返回 Permissive 則是臨時關閉狀態,
重啟后是 Disabled ,完全關閉狀態。
getenforce
sestatus
關閉防火墻
systemctl stop firewalld.service
systemctl disable firewalld.service
查看是否關閉
systemctl status firewalld.service
3.安裝數據庫
下面的安裝步驟是在 root 下執行的
執行預安裝
dnf -y install oracle-database-preinstall-23c-1.0-0.5.el8.x86_64.rpm
安裝的依賴具體內容如下:
bc
bind-libs
bind-libs-lite
bind-license
bind-utils
binutils
checkpolicy
compat-openssl10
fstrm
glibc-devel
glibc-headers
gssproxy
kernel-headers
keyutils
ksh
libICE
libSM
libX11
libX11-common
libX11-xcb
libXau
libXcomposite
libXext
libXi
libXinerama
libXmu
libXrandr
libXrender
libXt
libXtst
libXv
libXxf86dga
libXxf86misc
libXxf86vm
libdmx
libev
libmaxminddb
libnsl
libpkgconf
libverto-libev
libxcb
libxcrypt-devel
lm_sensors-libs
make
net-tools
nfs-utils
pkgconf
pkgconf-m4
pkgconf-pkg-config
policycoreutils-python-utils
protobuf-c
python3-audit
python3-bind
python3-libsemanage
python3-ply
python3-policycoreutils
python3-pyyaml
python3-setools
quota
quota-nls
rpcbind
smartmontools
sysstat
tar
unzip
xorg-x11-utils
xorg-x11-xauth
Installing
geolite2-city
geolite2-country
可以在 /var/log/oracle-database-preinstall-23c 目錄下查看相關日志
安裝Oracle 23c
使用安裝命令:
dnf -y install oracle-database-free-23c-1.0-1.el8.x86_64.rpm
創建和配置Oracle數據庫服務實例
運行腳本:
/etc/init.d/oracle-free-23c configure
輸出:
[root@centos8 ~]# /etc/init.d/oracle-free-23c configure
Specify a password to be used for database accounts. Oracle recommends that the password entered should be at least 8 characters in length, contain at least 1 uppercase character, 1 lower case character and 1 digit [0-9]. Note that the same password will be used for SYS, SYSTEM and PDBADMIN accounts:
Confirm the password:Passwords do not match. Enter the password:
Confirm the password:
Configuring Oracle Listener.
Listener configuration succeeded.
Configuring Oracle Database FREE.
Enter SYS user password:
**********
Enter SYSTEM user password:
**********
Enter PDBADMIN User Password:
************
Prepare for db operation
7% complete
Copying database files
29% complete
Creating and starting Oracle instance
30% complete
33% complete
36% complete
39% complete
43% complete
Completing Database Creation
47% complete
49% complete
50% complete
Creating Pluggable Databases
54% complete
71% complete
Executing Post Configuration Actions
93% complete
Running Custom Scripts
100% complete
Database creation complete. For details check the logfiles at:/opt/oracle/cfgtoollogs/dbca/FREE.
Database Information:
Global Database Name:FREE
System Identifier(SID):FREE
Look at the log file "/opt/oracle/cfgtoollogs/dbca/FREE/FREE.log" for further details.Connect to Oracle Database using one of the connect strings:Pluggable database: centos8/FREEPDB1Multitenant container database: centos8
4.設置oracle用戶的環境配置
切換用戶然后編輯 ~/.bash_profile
su - oracle
vi ~/.bash_profile
# oracle settings
export ORACLE_SID=FREE
export ORAENV_ASK=NO
export ORACLE_HOME=/opt/oracle/product/23c/dbhomeFree
export PATH=$ORACLE_HOME/bin:$PATH
刷新環境變量
source ~/.bash_profile
5.簡單使用
啟動數據庫
1、啟動監聽
lsnrctl start
查看監聽狀態:
$ lsnrctl status
LSNRCTL for Linux: Version 23.0.0.0.0 - Developer-Release on 09-MAY-2023 10:02:25Copyright (c) 1991, 2023, Oracle. All rights reserved.Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.2.15)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 23.0.0.0.0 - Developer-Release
Start Date 09-MAY-2023 00:27:46
Uptime 0 days 9 hr. 34 min. 39 sec
Trace Level off
Security ON: Local OS Authentication
SNMP OFF
Default Service FREE
Listener Parameter File /opt/oracle/product/23c/dbhomeFree/network/admin/listener.ora
Listener Log File /opt/oracle/diag/tnslsnr/centos8/listener/alert/log.xml
Listening Endpoints Summary...(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=10.0.2.15)(PORT=1521)))(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "FREE" has 1 instance(s).Instance "FREE", status READY, has 1 handler(s) for this service...
Service "FREEXDB" has 1 instance(s).Instance "FREE", status READY, has 1 handler(s) for this service...
Service "faf3c4adb9132c47e0550a0027434e0c" has 1 instance(s).Instance "FREE", status READY, has 1 handler(s) for this service...
Service "freepdb1" has 1 instance(s).Instance "FREE", status READY, has 1 handler(s) for this service...
The command completed successfully
2、啟動數據庫
使用sqlplus連接數據庫后輸入startup
$ sqlplus / as sysdba
SQL*Plus: Release 23.0.0.0.0 - Developer-Release on Tue May 9 10:06:07
2023 Version 23.2.0.0.0Copyright ? 1982, 2023, Oracle. All rights reserved.
Connected to: Oracle Database 23c Free, Release 23.0.0.0.0 -
Developer-Release Version 23.2.0.0.0
SQL> startup;
查詢數據庫信息
查看數據庫版本信息:
SQL> SELECT BANNER_FULL FROM v$version;
BANNER_FULL
--------------------------------------------------------------------------------
Oracle Database 23c Free, Release 23.0.0.0.0 - Developer-Release
Version 23.2.0.0.0
查詢數據庫和實例以及容器數據庫信息
SQL> SELECT name, db_unique_name, log_mode, flashback_on, cdb FROM v$database;
> NAME DB_UNIQUE_NAME LOG_MODE FLASHBACK_ON
> CDB
> --------- ------------------------------ ------------ ------------------ --- FREE FREE NOARCHIVELOG NO YES
>
> SQL> SELECT instance_name, version, status, database_status FROM
> v$instance;
>
> INSTANCE_NAME VERSION STATUS DATABASE_STATUS
> ---------------- ----------------- ------------ ----------------- FREE 23.0.0.0.0 OPEN ACTIVE
查看sga信息
SQL> SELECT * FROM v$sgainfo;
NAME BYTES RES CON_ID
-------------------------------- ---------- --- ----------
Fixed SGA Size 10042432 No 0
Redo Buffers 4530176 No 0
Buffer Cache Size 436207616 Yes 0
In-Memory Area Size 0 No 0
Shared Pool Size 671088640 Yes 0
Large Pool Size 16777216 Yes 0
Java Pool Size 0 Yes 0
Streams Pool Size 0 Yes 0
Shared IO Pool Size 67108864 Yes 0
Data Transfer Cache Size 0 Yes 0
Granule Size 16777216 No 0NAME BYTES RES CON_ID
-------------------------------- ---------- --- ----------
Maximum SGA Size 1155423296 No 0
Startup overhead in Shared Pool 235549832 No 0
Free SGA Memory Available 16777216 014 rows selected.
創建測試用戶和表
1、 查看當前連接
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
2、查看有哪些可拔插數據庫
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO3 FREEPDB1 READ WRITE NO
3、 進入可拔插數據庫
SQL> alter session set container=FREEPDB1;
Session altered.
SQL> show con_name
CON_NAME
------------------------------ FREEPDB1
4、 查看插拔數據庫中有哪些表空間及其數據文件
SELECT TABLESPACE_NAME, FILE_NAME, BYTES/1024/1024 MB FROM DBA_DATA_FILES;
5、創建測試用戶
SQL> create user testuser identified by 123456 default tablespace users;User created.SQL> GRANT connect, resource, unlimited tablespace to testuser;Grant succeeded.SQL>
6、使用新建的測試用戶連接可拔插數據庫
$ sqlplus testuser/123456@10.0.2.15:1521/FREEPDB1
SQL*Plus: Release 23.0.0.0.0 - Developer-Release on Tue May 9 11:20:07
2023 Version 23.2.0.0.0Copyright ? 1982, 2023, Oracle. All rights reserved.
Connected to: Oracle Database 23c Free, Release 23.0.0.0.0 -
Developer-Release Version 23.2.0.0.0
SQL>
7、創建測試表
SQL> CREATE TABLE personnel (id NUMBER(2) CONSTRAINT PK_EMP PRIMARY KEY, name VARCHAR2(20), age NUMBER(2));
Table created.
8、增刪改查數據
SQL> INSERT INTO personnel(id, name, age) VALUES(1, 'test1', 18);
1 row created.
SQL> INSERT INTO personnel(id, name, age) VALUES(2, 'test2', 19);
1 row created.
SQL> INSERT INTO personnel(id, name, age) VALUES(3, 'test3', 20);
1 row created.
SQL> INSERT INTO personnel(id, name, age) VALUES(4, 'test4', 21);
1 row created.
SQL> SELECT * FROM personnel;
ID NAME AGE
1 test1 182 test2 193 test3 204 test4 21
SQL> DELETE FROM personnel WHERE id = 2;
1 row deleted.
SQL> SELECT * FROM personnel;
ID NAME AGE
1 test1 183 test3 204 test4 21
SQL> UPDATE personnel SET name = 'testname' WHERE id = 3;
1 row updated.
SQL> SELECT * FROM personnel;
ID NAME AGE
1 test1 183 testname 204 test4 21
SQL> truncate table personnel;
Table truncated.
SQL> SELECT * FROM personnel;
no rows selected
9、刪除表
SQL> drop table personnel;
Table dropped.
10、刪除用戶
刪除用戶時,該用戶必須沒有連接,且用dba用戶在創建用戶的可拔插數據庫下刪除。
SQL> drop user testuser;
User dropped.
停止數據庫
sqlplus連接數據庫并停止
SQL> shutdown
Database closed.
Database dismounted.
ORACLE instance shut down.
sqlplus優化
使用歷史命令
在使用默認sqlplus時,使用方向鍵上下左右時會報亂碼。可使用readline和rlwrap解決。
下載安裝readline
dnf -y install readline readline-devel
下載安裝rlwrap
wget https://github.com/hanslub42/rlwrap/releases/download/0.46.1/rlwrap-0.46.1.tar.gz
tar -zxvf rlwrap-0.46.1.tar.gz
cd rlwrap-0.46.1
./configure
make
make install
若缺少gcc、make之類的依賴自己安裝一下即可。
使用:
rlwrap sqlplus / as sysdba
如果嫌棄這樣麻煩,可以使用 alias 做重命名一下。把 alias 添加到.bash_profile 文件中。
su - oracle
echo "alias sqlplus='rlwrap sqlplus'" >> .bash_profile
source .bash_profile
設置行列寬度
pagesize 默認是14,linesize 默認是80 。 為了 美觀,我們一般根據自己的屏幕分辨率來設置。
SQL> show pagesize;
pagesize 14
SQL> show linesize;
linesize 80
SQL> set pagesize 40;
SQL> set linesize 200;
SQL> show pagesize;
pagesize 40
SQL> show linesize;
linesize 200
卸載基于RPM安裝的Oracle數據庫
1、以安裝所有者登錄
su - oracle
2、刪除關聯庫
cd $ORACLE_HOME/bin
./dbca
3、刪除關聯偵聽器
cd $ORACLE_HOME/bin
./netca
4、切換到 root,然后刪除數據庫
dnf -y remove oracle-database-free-23c
dnf -y remove oracle-database-preinstall-23c