Oracle LiveLabs实验: Oracle多租户基础
Posted dingdingfish
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Oracle LiveLabs实验: Oracle多租户基础相关的知识,希望对你有一定的参考价值。
本文为Oracle LiveLabs中实验Pluggables, Clones and Containers: Oracle Multitenant Fundamentals Workshop的过程记录。
实验指南请参见文档。
实验虽然也包括Application containers部分,但由于其应用较少,本文不涉及此部分。
整个实验预计耗时4个小时,其实如果概念熟悉的话,2个小时就够。
实验默认的界面是通过noVNC,但图形界面操作不便,而且会较慢。因此建议使用noVNC的粘贴板将私钥加入到虚拟机中,然后就可以用PuTTY登录。公网地址在页面顶端有显示。
cd $HOME; mkdir .ssh; cd .ssh
vi authorized_keys
Lab 2: Initialize Environment
验证所需的进程已启动并正在运行。
[CDB1:oracle@dbhol:~]$ ps -ef|grep LIST|grep -v grep
oracle 3188 1 0 08:37 ? 00:00:00 /opt/oracle/product/19c/dbhome_1/bin/tnslsnr LISTENER -inherit
oracle 3189 1 0 08:37 ? 00:00:00 /opt/oracle/product/19c/dbhome_1/bin/tnslsnr LISTCDB2 -inherit
[CDB1:oracle@dbhol:~]$ ps -ef|grep ora_|grep pmon|grep -v grep
oracle 3387 1 0 08:37 ? 00:00:00 ora_pmon_CDB1
oracle 3847 1 0 08:38 ? 00:00:00 ora_pmon_CDB2
[CDB1:oracle@dbhol:~]$ systemctl status oracle-database
● oracle-database.service - The Oracle Database Service
Loaded: loaded (/etc/systemd/system/oracle-database.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-12-13 08:38:44 GMT; 34min ago
Process: 3109 ExecStart=/bin/bash -c /home/oracle/scripts/startall.sh >> /home/oracle/log/startup_shutdown.log 2>&1 (code=exited, status=0/SUCCESS)
Tasks: 123
Memory: 9.3G
CGroup: /system.slice/oracle-database.service
├─3188 /opt/oracle/product/19c/dbhome_1/bin/tnslsnr LISTENER -inherit
├─3387 ora_pmon_CDB1
├─3391 ora_clmn_CDB1
├─3395 ora_psp0_CDB1
├─3401 ora_vktm_CDB1
├─3407 ora_gen0_CDB1
├─3411 ora_mman_CDB1
├─3417 ora_gen1_CDB1
├─3420 ora_diag_CDB1
├─3422 ora_ofsd_CDB1
├─3425 ora_dbrm_CDB1
├─3429 ora_vkrm_CDB1
├─3431 ora_svcb_CDB1
├─3433 ora_pman_CDB1
├─3437 ora_dia0_CDB1
├─3439 ora_dbw0_CDB1
├─3444 ora_lgwr_CDB1
├─3448 ora_ckpt_CDB1
├─3452 ora_lg00_CDB1
├─3454 ora_smon_CDB1
├─3458 ora_lg01_CDB1
├─3460 ora_smco_CDB1
├─3462 ora_reco_CDB1
├─3464 ora_w000_CDB1
├─3466 ora_lreg_CDB1
├─3470 ora_w001_CDB1
├─3472 ora_pxmn_CDB1
├─3478 ora_mmon_CDB1
├─3480 ora_mmnl_CDB1
├─3482 ora_d000_CDB1
├─3484 ora_s000_CDB1
├─3486 ora_tmon_CDB1
├─3495 ora_m000_CDB1
├─3502 ora_tt00_CDB1
├─3504 ora_tt01_CDB1
├─3506 ora_tt02_CDB1
├─3511 ora_w002_CDB1
├─3513 ora_aqpc_CDB1
├─3517 ora_p000_CDB1
├─3519 ora_p001_CDB1
├─3521 ora_p002_CDB1
├─3523 ora_p003_CDB1
├─3525 ora_p004_CDB1
├─3527 ora_p005_CDB1
├─3529 ora_p006_CDB1
├─3531 ora_p007_CDB1
├─3626 ora_w003_CDB1
├─3723 ora_cjq0_CDB1
├─3843 ora_w004_CDB1
├─3847 ora_pmon_CDB2
├─3851 ora_clmn_CDB2
├─3855 ora_psp0_CDB2
├─3872 ora_vktm_CDB2
├─3887 ora_gen0_CDB2
├─3900 ora_mman_CDB2
├─3912 ora_gen1_CDB2
├─3919 ora_diag_CDB2
├─3946 ora_ofsd_CDB2
├─3968 ora_dbrm_CDB2
├─4004 ora_vkrm_CDB2
├─4025 ora_svcb_CDB2
├─4038 ora_pman_CDB2
├─4053 ora_dia0_CDB2
├─4063 ora_dbw0_CDB2
├─4087 ora_lgwr_CDB2
├─4114 ora_ckpt_CDB2
├─4141 ora_lg00_CDB2
├─4156 ora_smon_CDB2
├─4162 ora_qm02_CDB1
├─4181 ora_lg01_CDB2
├─4188 ora_smco_CDB2
├─4193 ora_reco_CDB2
├─4203 ora_w000_CDB2
├─4221 ora_lreg_CDB2
├─4233 ora_q003_CDB1
├─4235 ora_w001_CDB2
├─4243 ora_pxmn_CDB2
├─4275 ora_mmon_CDB2
├─4287 ora_mmnl_CDB2
├─4299 ora_d000_CDB2
├─4304 ora_s000_CDB2
├─4310 ora_tmon_CDB2
├─4452 ora_m000_CDB2
├─4691 ora_m001_CDB1
├─4694 ora_m002_CDB1
├─4717 ora_tt00_CDB2
├─4730 ora_tt01_CDB2
├─4746 ora_tt02_CDB2
├─5255 ora_m003_CDB1
├─5358 ora_w002_CDB2
├─5745 ora_aqpc_CDB2
├─5750 ora_qm02_CDB2
├─5773 ora_p000_CDB2
├─5775 ora_p001_CDB2
├─5777 ora_p002_CDB2
├─5780 ora_p003_CDB2
├─5782 ora_p004_CDB2
├─5784 ora_w003_CDB2
├─5786 ora_p005_CDB2
├─5788 ora_p006_CDB2
├─5790 ora_p007_CDB2
├─5883 ora_w004_CDB2
├─5943 ora_q007_CDB1
├─6175 ora_q00d_CDB2
├─6188 ora_q00j_CDB2
├─6197 ora_cjq0_CDB2
├─6556 ora_cl00_CDB1
├─6559 ora_w005_CDB1
├─6607 ora_m001_CDB2
├─6609 ora_m002_CDB2
├─6611 ora_m003_CDB2
├─6639 ora_m004_CDB2
├─6681 ora_cl00_CDB2
├─9786 ora_w006_CDB1
├─9791 ora_w007_CDB1
├─9892 ora_w005_CDB2
├─9929 ora_w006_CDB2
└─9934 ora_w007_CDB2
[CDB1:oracle@dbhol:~]$ systemctl status oracle-db-listener
● oracle-db-listener.service - The Oracle DB Listeners Service
Loaded: loaded (/etc/systemd/system/oracle-db-listener.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2021-12-13 08:37:54 GMT; 35min ago
Process: 3108 ExecStart=/bin/bash -c /home/oracle/scripts/start-all-listeners.sh (code=exited, status=0/SUCCESS)
Main PID: 3189 (tnslsnr)
Tasks: 2
Memory: 23.3M
CGroup: /system.slice/oracle-db-listener.service
└─3189 /opt/oracle/product/19c/dbhome_1/bin/tnslsnr LISTCDB2 -inherit
从以上输出可知,目前有2个CDB:CDB1和CDB2。两个监听:LISTENER(1521端口) 和 LISTCDB2(1522端口) 。
从以下输出可知。CDB1用的LISTENER,CDB2用的LISTCDB2 :
[CDB2:oracle@dbhol:~]$ lsnrctl status
LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 13-DEC-2021 09:16:23
Copyright (c) 1991, 2021, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=dbhol.livelabs.oraclevcn.com)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias LISTENER
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 13-DEC-2021 08:37:53
Uptime 0 days 0 hr. 38 min. 30 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/dbhol/listener/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbhol.livelabs.oraclevcn.com)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=dbhol.livelabs.oraclevcn.com)(PORT=5501))(Security=(my_wallet_directory=/opt/oracle/admin/CDB1/xdb_wallet))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "CDB1" has 1 instance(s).
Instance "CDB1", status READY, has 1 handler(s) for this service...
Service "CDB1XDB" has 1 instance(s).
Instance "CDB1", status READY, has 1 handler(s) for this service...
Service "cd8d29dc40fd7370e0533800000ab923" has 1 instance(s).
Instance "CDB1", status READY, has 1 handler(s) for this service...
Service "pdb1" has 1 instance(s).
Instance "CDB1", status READY, has 1 handler(s) for this service...
The command completed successfully
[CDB2:oracle@dbhol:~]$ lsnrctl status LISTCDB2
LSNRCTL for Linux: Version 19.0.0.0.0 - Production on 13-DEC-2021 09:19:54
Copyright (c) 1991, 2021, Oracle. All rights reserved.
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=dbhol.livelabs.oraclevcn.com)(PORT=1522)))
STATUS of the LISTENER
------------------------
Alias LISTCDB2
Version TNSLSNR for Linux: Version 19.0.0.0.0 - Production
Start Date 13-DEC-2021 08:37:53
Uptime 0 days 0 hr. 42 min. 1 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/dbhol/listcdb2/alert/log.xml
Listening Endpoints Summary...
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=dbhol.livelabs.oraclevcn.com)(PORT=1522)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=dbhol.livelabs.oraclevcn.com)(PORT=5502))(Security=(my_wallet_directory=/opt/oracle/admin/CDB2/xdb_wallet))(Presentation=HTTP)(Session=RAW))
Services Summary...
Service "CDB2" has 1 instance(s).
Instance "CDB2", status READY, has 1 handler(s) for this service...
Service "CDB2XDB" has 1 instance(s).
Instance "CDB2", status READY, has 1 handler(s) for this service...
Service "cd8d71a2721f1aace0533800000a39bd" has 1 instance(s).
Instance "CDB2", status READY, has 1 handler(s) for this service...
Service "pdb2" has 1 instance(s).
Instance "CDB2", status READY, has 1 handler(s) for this service...
The command completed successfully
如果监听和数据库状态不对,可以用以下脚本重置:
sudo systemctl restart oracle-database
sudo systemctl restart oracle-db-listener
为多租户用例初始化数据库
下载脚本:
cd ~
wget -O labs-novnc.zip https://objectstorage.us-ashburn-1.oraclecloud.com/p/DI8FDu8opoqAK7MoplCLgwUiVsf8uyNNbcLkaytAdJyGSMmgn0Jw14OMtG3vNLI-/n/c4u04/b/labfiles/o/labs-novnc.zip
unzip -qo labs-novnc.zip
rm -f labs-novnc.zip
cd labs/multitenant
chmod +x *.sh
Lab 3: Clone, Plug and Drop
Task 1: Log in and create PDB
cd /home/oracle/labs/multitenant
# 设置ORACLE_SID: CDB1
# 设置ORACLE_HOME: /opt/oracle/product/19c/dbhome_1
. ~/.set-env-db.sh CDB1
sql sys/Ora_DB4U@localhost:1521/cdb1 as sysdba
set sqlformat ANSICONSOLE
以上.set-env-db.sh脚本,从oratab中获取SID,然后设置ORACLE_HOME。oratab位置可能有两处:
if [ -f /etc/oratab ]; then
OTAB=/etc/oratab
elif [ -f /var/opt/oracle/oratab ]; then
OTAB=/var/opt/oracle/oratab
else
echo 'oratab file not found.'
exit
fi
下面这个脚本可以显示当前所连数据库的用户:
select
'DB Name: ' ||Sys_Context('Userenv', 'DB_Name')||
' / CDB?: ' ||case
when Sys_Context('Userenv', 'CDB_Name') is not null then 'YES'
else 'NO'
end||
' / Auth-ID: ' ||Sys_Context('Userenv', 'Authenticated_Identity')||
' / Sessn-User: '||Sys_Context('Userenv', 'Session_User')||
' / Container: ' ||Nvl(Sys_Context('Userenv', 'Con_Name'), 'n/a')
"Who am I?"
from Dual
.
save whoami.sql
以上脚本中,.
命令的作用是把输入存入buffer,但不执行,效果和空行相同。后续save命令将buffer内容存为文件。和buffer相关SQL Plus命令包括list,edit,save,input。
SYS_CONTEXT函数的帮助见这里,对于开发者尤为有用。
看一下执行效果:
SQL> @whoami
Who am I?
DB Name: CDB1 / CDB?: YES / Auth-ID: SYS / Sessn-User: SYS / Container: CDB$ROOT
创建可插拔数据库PDB2:
SQL> show user
USER is "SYS"
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
-- pdb_admin是local admin user
SQL> create pluggable database PDB2 admin user pdb_admin identified by Ora_DB4U;
Pluggable database created.
-- 初始克隆的PDB是mount状态
SQL> alter pluggable database PDB2 open;
Pluggable database altered.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ WRITE NO
SQL> alter session set container = PDB2;
Session altered.
SQL> grant sysdba to pdb_admin;
Grant succeeded.
-- 由pdb$seed克隆而来,因此最初没有users表空间
SQL> create tablespace users datafile size 20M autoextend on next 1M maxsize unlimited segment space management auto;
Tablespace created.
SQL> alter database default tablespace Users;
Database altered.
SQL> grant create table, unlimited tablespace to pdb_admin;
Grant succeeded.
在pdb2中创建测试表:
SQL> connect pdb_admin/Ora_DB4U@localhost:1521/pdb2
Connected.
SQL> create table my_tab(my_col number);
Table created.
SQL> insert into my_tab values (1);
1 row created.
SQL> commit;
Commit complete.
切换到CDB根并显示所有的表空间:
connect sys/Ora_DB4U@localhost:1521/cdb1 as sysdba
with Containers as (
select PDB_ID Con_ID, PDB_Name Con_Name from DBA_PDBs
union
select 1 Con_ID, 'CDB$ROOT' Con_Name from Dual)
select
Con_ID,
Con_Name "Con_Name",
Tablespace_Name "T'space_Name",
File_Name "File_Name"
from CDB_Data_Files inner join Containers using (Con_ID)
union
select
Con_ID,
Con_Name "Con_Name",
Tablespace_Name "T'space_Name",
File_Name "File_Name"
from CDB_Temp_Files inner join Containers using (Con_ID)
order by 1, 3
/
这个SQL首先使用with子句创建了CTE(Common Table Expression),CTE中的union兼顾了PDB和CDB根。然后在之后的SELECT语句中,分别查询了CDB_Data_Files和CDB_Temp_Files,并将结果集合并。然后,inner join是最常见的联接类型。 只会从一个表中返回与另一个表中存在匹配行的行。
输出如下:
CON_ID Con_Name T'space_Name File_Name
1 CDB$ROOT SYSAUX /opt/oracle/oradata/CDB1/sysaux01.dbf
1 CDB$ROOT SYSTEM /opt/oracle/oradata/CDB1/system01.dbf
1 CDB$ROOT TEMP /opt/oracle/oradata/CDB1/temp01.dbf
1 CDB$ROOT UNDOTBS1 /opt/oracle/oradata/CDB1/undotbs01.dbf
1 CDB$ROOT USERS /opt/oracle/oradata/CDB1/users01.dbf
3 PDB1 SYSAUX /opt/oracle/oradata/CDB1/PDB1/sysaux01.dbf
3 PDB1 SYSTEM /opt/oracle/oradata/CDB1/PDB1/system01.dbf
3 PDB1 TEMP /opt/oracle/oradata/CDB1/PDB1/temp01.dbf
3 PDB1 UNDOTBS1 /opt/oracle/oradata/CDB1/PDB1/undotbs01.dbf
3 PDB1 USERS /opt/oracle/oradata/CDB1/PDB1/users01.dbf
4 PDB2 SYSAUX /opt/oracle/oradata/CDB1/D311F4F4FC433460E053C000000ABBB1/datafile/o1_mf_sysaux_jvhw5tmm_.dbf
4 PDB2 SYSTEM /opt/oracle/oradata/CDB1/D311F4F4FC433460E053C000000ABBB1/datafile/o1_mf_system_jvhw5tmb_.dbf
4 PDB2 TEMP /opt/oracle/oradata/CDB1/D311F4F4FC433460E053C000000ABBB1/datafile/o1_mf_temp_jvhw5tmn_.dbf
4 PDB2 UNDOTBS1 /opt/oracle/oradata/CDB1/D311F4F4FC433460E053C000000ABBB1/datafile/o1_mf_undotbs1_jvhw5tmm_.dbf
4 PDB2 USERS /opt/oracle/oradata/CDB1/D311F4F4FC433460E053C000000ABBB1/datafile/o1_mf_users_jvhw7gfy_.dbf
15 rows selected.
Task 2: Clone a PDB
SQL> connect sys/Ora_DB4U@localhost:1521/cdb1 as sysdba
Connected.
-- 普通的克隆要求源PDB是只读的,但实际上此处也可以热克隆
SQL> alter pluggable database PDB2 open read only force;
Pluggable database altered.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ ONLY NO
SQL> create pluggable database PDB3 from PDB2;
Pluggable database created.
SQL> alter pluggable database PDB3 open force;
Pluggable database altered.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ ONLY NO
5 PDB3 READ WRITE NO
SQL> alter pluggable database PDB2 open read write force;
Pluggable database altered.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ WRITE NO
5 PDB3 READ WRITE NO
注意以上代码段中的alter pluggable database open .... force
。如果不指定force,则PDB要求处于mount状态。如果PDB不是mount状态,则报错如下:
ERROR at line 1:
ORA-65019: pluggable database PDB2 already open
以下确认测试表被克隆:
SQL> connect pdb_admin/Ora_DB4U@localhost:1521/pdb2
Connected.
SQL> select * from my_tab;
MY_COL
1
SQL> connect pdb_admin/Ora_DB4U@localhost:1521/pdb3
Connected.
SQL> select * from my_tab;
MY_COL
1
Task 3: Unplug a PDB
本节演示如何从容器数据库 (CDB) 中拔出 PDB 并将 PDB 元数据保存到 XML文件中。
SQL> connect sys/Ora_DB4U@localhost:1521/cdb1 as sysdba
Connected.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ ONLY NO
5 PDB3 READ WRITE NO
-- unplug前必须关闭数据库
SQL> alter pluggable database PDB3 close immediate;
Pluggable database altered.
SQL> alter pluggable database PDB3 unplug into '/opt/oracle/oradata/CDB1/pdb3.xml';
Pluggable database altered.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ ONLY NO
5 PDB3 MOUNTED
-- 将PDB3从CDB中删除,但其数据文件仍在(keep datafiles)
SQL> drop pluggable database PDB3 keep datafiles;
Pluggable database dropped.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
4 PDB2 READ ONLY NO
查看XML文件:
SQL> host cat /opt/oracle/oradata/CDB1/pdb3.xml
<?xml version="1.0" encoding="UTF-8"?>
<PDB>
<xmlversion>1</xmlversion>
<pdbname>PDB3</pdbname>
<cid>5</cid>
<byteorder>1</byteorder>
<vsn>318767104</vsn>
<vsns>
<vsnnum>19.0.0.0.0</vsnnum>
<cdbcompt>19.0.0.0.0</cdbcompt>
<pdbcompt>19.0.0.0.0</pdbcompt>
<vsnlibnum>0.0.0.0.24</vsnlibnum>
<vsnsql>24</vsnsql>
<vsnbsv>8.0.0.0.0</vsnbsv>
</vsns>
<dbid>1895726733</dbid>
<ncdb2pdb>0</ncdb2pdb>
<cdbid>1058798813</cdbid>
<guid>D3125F5FE21B66E1E053C000000AE230</guid>
<uscnbas>4249686</uscnbas>
<uscnwrp>0</uscnwrp>
<undoscn>280</undoscn>
<rdba>4194824</rdba>
<tablespace>
<name>SYSTEM</name>
<type>0</type>
<tsn>0</tsn>
<status>1</status>
<issft>0</issft>
<isnft>0</isnft>
<encts>0</encts以上是关于Oracle LiveLabs实验: Oracle多租户基础的主要内容,如果未能解决你的问题,请参考以下文章
Oracle LiveLabs实验:Oracle RAC Fundamentals
Oracle LiveLabs实验:Install Oracle Database 21c
Oracle LiveLabs实验:DB Security - Oracle Label Security (OLS)
Oracle LiveLabs实验:Oracle Label Security (OLS)