原文链接:https://blog.dbi-services.com/managing-refreshable-clone-pluggable-databases-with-oracle-21c/
原文作者:By Mouhamadou Diaw
可刷新的克隆 PDB 是一种刷新单个 PDB 的方法,而不是像在 Data Guard 环境中那样刷新容器中的所有 PDB。它包括对源 PDB 进行克隆,并使用自上次应用重做日志以来累积的重做更新克隆 PDB。
在这个博客中,我做了一些关于可刷新可插拔数据库特性的测试。
我正在使用 Oracle 21c 进行测试,但此功能自 Oracle 12.2 以来就存在。
我在下面使用的配置
一个 Oracle 21c 源 CDB:带有源可插入数据库 PDB1的 DB21
一个 Oracle 21c 目标 CDB:TEST21,它将包含 PDB1 的可刷新克隆。克隆将命名为 PDB1FRES
请注意,可刷新克隆可以在同一个容器中创建。
第一步是在源CDB DB21中创建一个用于数据库链接的用户
SQL> create user c##clone_user identified by rootroot2016 temporary tablespace temp container=ALL;
User created.
SQL>
SQL> grant create session, create pluggable database, sysoper to c##clone_user container=ALL ;
Grant succeeded.
SQL>
在目标CDB TEST21中,让我们创建一个到源CDB的database link。 我们将使用用户c##克隆
SQL> create database link clonesource connect to c##clone_user identified by rootroot2016 using 'DB21';
Database link created.
SQL>
SQL> select * from dual@clonesource;
D
-
X
SQL>
现在,我们可以在数据库TEST21中创建PDB1的可刷新克隆PDB1FRES。
首先,我们将创建一个手动可刷新克隆
SQL> create pluggable database PDB1FRES from PDB1@clonesource refresh mode manual;
Pluggable database created.
SQL>
创建后,新克隆的database是mounted状态
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES MOUNTED
SQL>
我们可以看到刷新模式
SQL> select PDB_NAME,REFRESH_MODE,REFRESH_INTERVAL,LAST_REFRESH_SCN from dba_pdbs where PDB_NAME='PDB1FRES';
PDB_NAME REFRES REFRESH_INTERVAL LAST_REFRESH_SCN
--------------- ------ ---------------- ----------------
PDB1FRES MANUAL 39266271
SQL>
好了,现在让我们对PDB1做一些更改,看看如何在PDB1FRES上传这些更改
SQL> show con_name
CON_NAME
------------------------------
PDB1
SQL> create table test(id number);
Table created.
SQL> insert into test values (1);
1 row created.
SQL> commit;
Commit complete.
SQL>
必须关闭(mounted)PDB1FRES,以便对PDB1中的更改进行刷新。 由于在创建过程中使用了子句REFRESH MANUAL,所以我们必须手动执行刷新
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL> alter pluggable database PDB1FRES refresh;
Pluggable database altered.
SQL>
现在,我们以只读模式打开PDB1FRES,以验证刷新
SQL> alter pluggable database PDB1FRES open read only;
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES MOUNTED
SQL> alter pluggable database PDB1FRES open read only;
Pluggable database altered.
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES READ ONLY NO
SQL> alter session set container=PDB1FRES;
Session altered.
SQL> select * from test;
ID
----------
1
SQL>
SQL> alter pluggable database PDB1FRES close immediate;
Pluggable database altered.
可以看到,手动刷新工作正常。
我们可以把手动刷新模式改成自动刷新模式吗?
让我们试试看.
SQL> alter pluggable database PDB1FRES refresh mode every 4 minutes;
Pluggable database altered.
SQL> select PDB_NAME,REFRESH_MODE,REFRESH_INTERVAL,LAST_REFRESH_SCN from dba_pdbs where PDB_NAME='PDB1FRES';
PDB_NAME REFRES REFRESH_INTERVAL LAST_REFRESH_SCN
--------------- ------ ---------------- ----------------
PDB1FRES AUTO 4 39272240
SQL>
现在让我们再次对PDB1进行一些更改
SQL> insert into test values (10);
1 row created.
SQL> insert into test values (20);
1 row created.
SQL> commit;
Commit complete.
SQL>
4分钟后,我们可以看到PDB1FRES上最后一个LAST_REFRESH_SCN发生了更改
SQL> select PDB_NAME,REFRESH_MODE,REFRESH_INTERVAL,LAST_REFRESH_SCN from dba_pdbs where PDB_NAME='PDB1FRES';
PDB_NAME REFRES REFRESH_INTERVAL LAST_REFRESH_SCN
--------------- ------ ---------------- ----------------
PDB1FRES AUTO 4 39272403
SQL>
让我们以只读模式打开PDB1FRES,并验证是否复制了最新的更改
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES READ ONLY NO
SQL> alter session set container=PDB1FRES ;
Session altered.
SQL> select * from test;
ID
----------
1
10
20
SQL>
注意,PDB克隆只有mounted,自动刷新才会成功。 还要注意,即使配置了自动刷新,也可以执行手动刷新。
另一个问题可能是,我们是否可以以读写模式打开PDB1FRES。
让我们试试看
SQL> alter pluggable database PDB1FRES open read write;
Pluggable database altered.
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES READ ONLY NO
SQL>
什么? open read write命令返回SUCCESS,但数据库在只读模式下是真正的openend。
要以读写模式打开数据库,必须将刷新模式设置为none。
SQL> alter pluggable database PDB1FRES refresh mode none;
alter pluggable database PDB1FRES refresh mode none
*
ERROR at line 1:
ORA-65025: Pluggable database PDB1FRES is not closed on all instances.
SQL> alter pluggable database PDB1FRES close immediate;
Pluggable database altered.
SQL> alter pluggable database PDB1FRES refresh mode none;
Pluggable database altered.
SQL> col pdb_name for a15
SQL> select PDB_NAME,REFRESH_MODE,REFRESH_INTERVAL,LAST_REFRESH_SCN from dba_pdbs where PDB_NAME='PDB1FRES';
PDB_NAME REFRES REFRESH_INTERVAL LAST_REFRESH_SCN
--------------- ------ ---------------- ----------------
PDB1FRES NONE 39272683
SQL> alter pluggable database PDB1FRES open read write;
Pluggable database altered.
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES READ WRITE NO
SQL>
现在PDB1FRES已以读写模式打开,让我们关闭它,并尝试以可刷新克隆的方式再次转换它
SQL> alter pluggable database PDB1FRES close immediate;
Pluggable database altered.
SQL> show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1FRES MOUNTED
SQL> alter pluggable database PDB1FRES refresh mode manual;
alter pluggable database PDB1FRES refresh mode manual
*
ERROR at line 1:
ORA-65261: pluggable database PDB1FRES not enabled for refresh
SQL>
无法将打开的 R/W PDB 转换回可刷新的 PDB。文档中明确指定了
您不能将普通 PDB 更改为可刷新的克隆 PDB。可刷新克隆 PDB 转换为普通 PDB 后,您无法将其改回可刷新克隆 PDB。
结论
可刷新 PDB 的一种用法是克隆可用作 PDB 级别快照的黄金主控。这些快照可用于开发人员的克隆环境。