1、Refresh MODE語句數據庫
在使用Create Pluggable Database建立PDB時指定Refresh MODE子句用來控制是否開啓刷新機制。c#
該子句目前只在」Create Pluggable Database… From」中支持,能夠使用此語句指定以下選項:session
指定Refresh MODE NONE,這是默認方式,在建立PDB時不開啓刷機機制。oracle
指定Refresh MODE MANUAL子句,建立手動刷新的PDB。app
指定Refresh MODE EVERY number_of_minutes MINUTES子句,建立以週期性刷新的PDB。ide
另外值得一提的是Oracle 18c對Refreshable PDB進行了進一步的加強,加強爲Refreshable PDB Switchover(可刷新PDB的切換),在12.2.1中源PDB只能做爲源端PDB,而在18c中可對源PDB和目標PDB進行角色轉換,該功能相似Active Dataguard的主備切換,只不過Oracle在PDB Refresh中再次實現了該功能。測試
目前Refreshable PDB只能在只讀模式下打開,當Refreshable PDB進行刷新時,Refreshable PDB必須處於關閉狀態,只讀模式打開時會中止刷新,當再次關閉Refreshable PDB時繼續上次的刷新,也能夠將Refreshable PDB更改成Refresh MODE NONE模式,更改以後你沒法在將PDB改成Refreshable PDB。spa
另外也能夠使用Alter Pluggable Database命令將Refreshable PDB從手動刷新更改成自動刷新,也能夠從自動刷新更改成手動刷新,一樣經過語句Alter Pluggable Database +Refresh MODE更改成非Refreshable PDB,可是非Refreshable PDB不能夠被更改成Refreshable PDB。日誌
同時須要注意的是,Source PDB和Refreshable PDB能夠屬於同一個CDB, 也能夠屬於不一樣的 CDB,可是 在建立Refreshable PDB時必須指定DBlink,DBlink指向本身所在的CDB,不然語法沒法經過。orm
Refreshable PDB的應用場景能夠用在開發和測試環境的搭建過程當中, 由於數據是採用增量同步的方式,這樣就減小了對源數據庫的影響,也能夠做爲online備份庫。
Clone源端的PDB前期注意事項:
若是PDB被Clone的到的CDB的字符集不是AL32UTF8,那麼源與目標字符集必須兼容。
源端與目標端的字節順序必須相同。
鏈接的用戶在CDB中必須擁有’Create Pluggable Database’的權限。
源PDB不能夠是關閉狀態。
源端PDB必須爲歸檔模式。
源端PDB必須是local undo模式。
在Refreshable PDB建立後,若是源端須要建立相應表空間,那麼目標端須要配置PDB_FILE_NAME_CONVERT參數或使用OMF目錄管理方式(OMF級別高於PDB_FILE_NAME_CONVERT),不然會致使目標端PDB刷新失敗。
目標端採用OMF目錄管理方式後,Create Pluggable Database子句中不能指定file_name_convert作顯示目錄轉換,不然報ORA-01276錯誤。
在使用刷新目標PDB與源PDB同步數據時,是經過dblink從源PDB訪問redo,但有些時候,當須要更新刷新副本時,源PDB或源PDB所屬的CDB可能沒法訪問,在這種狀況下,設置REMOTE_RECOVERY_FILE_DEST參數,將嘗試今後參數指定的目錄中讀取歸檔日誌文件。
刷新與切換命令均需在目標PDB中執行。
2、PDB Refreshcesh
以下將源端PDB:woqupdb經過refresh方式克隆到本地,且命名爲qdatapdb。
(1)建立目標端到源端庫的DBLINK:orcl12c
sys. ora12c>CREATE DATABASE LINK orcl12c CONNECT TO system IDENTIFIEDBY oracle USING 'orcl12c';
Database link created.
Elapsed: 00:00:00.15
sys. ora12c>
克隆源端WOQUPDB
sys. orcl12c>show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
4WOQUPDB READ WRITENO
sys. orcl12c>
(2)目標端配置OMF目錄管理方式
sys. ora12c>alter system set db_create_file_dest='/u01/app/oracle/oradata';
System altered.
Elapsed: 00:00:00.04
sys. ora12c>
(3)在目標端使用CREATE PLUGGABLE DATABASE + REFRESH MODE MANUAL語句建立qdatapdb
sys. ora12c>CREATE PLUGGABLE DATABASE qdatapdb FROM woqupdb@orcl12c REFRESH MODE MANUAL;
Pluggable database created.
Elapsed: 00:00:14.20
sys. ora12c>
(4)檢查克隆完成的QDATAPDB
sys. ora12c>show pdbs
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
4 QDATAPDB MOUNTED
sys. ora12c>select pdb_id, pdb_name, status, refresh_mode,refresh_interval from cdb_pdbs;
PDB_ID PDB_NAME STATUS REFRES REFRESH_INTERVAL
---------- ------------------------------ ---------- ----------------------
2 PDB$SEED NORMAL NONE
4 QDATAPDB REFRESHINGMANUAL
Elapsed: 00:00:00.07
sys. ora12c>
經過視圖咱們能夠看到狀態爲REFRESHING,刷新模式爲MANUAL手工刷新。
(5)在源PDB建立表空間tbs1,及測試表t
sys. orcl12c>createtablespace tbs1 datafile size 100M;
Tablespace created.
Elapsed: 00:00:00.30
sys. orcl12c>create user yyh identified by yyh;
User created.
Elapsed: 00:00:00.09
sys. orcl12c>drop user yyh ;
User dropped.
Elapsed: 00:00:01.34
sys. orcl12c>create user yyh default tablespace tbs1 identified by yyh;
User created.
Elapsed: 00:00:00.05
sys. orcl12c>grant dba to yyh;
Grant succeeded.
Elapsed: 00:00:00.04
sys. orcl12c>conn yyh/yyh@woqupdb
Connected.
yyh. woqupdb>createtable t as select * from dba_objects;
Table created.
Elapsed: 00:00:02.18
yyh. woqupdb>selectcount(*) from t;
COUNT(*)
----------
72714
Elapsed: 00:00:00.03
yyh. woqupdb>
(6)在目標端刷新QDatapdb
sys. ora12c>alterpluggable database qdatapdb refresh;
alter pluggable database qdatapdb refresh
*
ERROR at line 1:
ORA-65118: operation affecting a pluggable database cannot be performedfrom another pluggable database
Elapsed: 00:00:00.04
sys. ora12c>conn sys/oracle@qdatapdb as sysdba
Connected.
21:55:00 sys. qdatapdb>set time off
sys. qdatapdb>alterpluggable database qdatapdb refresh;
Pluggable database altered.
Elapsed: 00:00:01.84
sys. qdatapdb>
日誌信息:
2018-03-10T21:55:06.187494-05:00
QDATAPDB(4):alter pluggable database qdatapdb refresh
2018-03-10T21:55:06.975492-05:00
Applying media recovery for pdb-4099 from SCN 2800296 to SCN 2802347
Remote log information: count-1
thr-1, seq-5, logfile-/u01/app/oracle/fast_recovery_area/orcl12c/ORCL12C/foreign_archivelog/WOQUPDB/2018_03_10/o1_mf_1_5_1212300924_.arc,los-2791634, nxs-18446744073709551615
QDATAPDB(4):Media Recovery Start
2018-03-10T21:55:06.977285-05:00
QDATAPDB(4):Serial Media Recovery started
2018-03-10T21:55:07.061801-05:00
QDATAPDB(4):Media Recovery Log/u01/app/oracle/fast_recovery_area/orcl12c/ORCL12C/foreign_archivelog/WOQUPDB/2018_03_10/o1_mf_1_5_1212300924_.arc
2018-03-10T21:55:07.503582-05:00
QDATAPDB(4):Successfully added datafile 51 to media recovery
QDATAPDB(4):Datafile #51:'/u01/app/oracle/oradata/ORA12C/671B0AC55A3F2870E0535138A8C0026E/datafile/o1_mf_tbs1_fb96ncby_.dbf'
2018-03-10T21:55:07.998822-05:00
QDATAPDB(4):Incomplete Recovery applied until change 2802347 time 03/10/201821:55:06
2018-03-10T21:55:08.006177-05:00
QDATAPDB(4):Media Recovery Complete (ora12c)
QDATAPDB(4):Completed: alter pluggable database qdatapdb refresh
(7)目標端打開QDatapdb, 檢查刷新完成後的數據
sys. qdatapdb>alter database open read only;
Database altered.
Elapsed: 00:00:01.54
sys. qdatapdb>selectcount(*) from yyh.t;
COUNT(*)
----------
72714
Elapsed: 00:00:00.04
sys. qdatapdb>select t.name tablespace_name, d.name from v$tablespacet, v$datafile d where d.ts#=t.ts#;
TABLESPACE_NAME NAME
------------------------------------------------------------------------------------
SYSTEM /u01/app/oracle/oradata/ORA12C/671B/datafile/o1_mf_system_fbjv_.dbf
SYSAUX /u01/app/oracle/oradata/ORA12C/671B/datafile/o1_mf_sysaux_fbk6_.dbf
UNDOTBS1 /u01/app/oracle/oradata/ORA12C/671B/datafile/o1_mf_undotbs1_fbhk7_.dbf
TBS1 /u01/app/oracle/oradata/ORA12C/671B/datafile/o1_mf_tbs1_fbcby_.dbf
Elapsed: 00:00:00.01
sys. qdatapdb>
這裏已經刷新完畢,數據同步到本地目標qdatapdb中。
(8)目標庫修改成自動刷新模式,每分鐘刷新一次
sys. qdatapdb>ALTER PLUGGABLE DATABASE qdatapdb REFRESH MODE EVERY 1 MINUTES;
Pluggable database altered.
Elapsed: 00:00:00.09
sys. qdatapdb>select pdb_id, pdb_name, status, refresh_mode,refresh_interval from cdb_pdbs;
PDB_ID PDB_NAME STATUS REFRES REFRESH_INTERVAL
---------- ------------------------------ ---------- ----------------------
4 QDATAPDB REFRESHING AUTO 1
Elapsed: 00:00:00.05
sys. qdatapdb>
經過視圖咱們能夠看到刷新模式已經被更改成AUTO,間隔時間爲1分鐘。
(9)源PDB刪除測試表
yyh. woqupdb>droptable t;
Table dropped.
Elapsed: 00:00:00.31
yyh. woqupdb>
日誌:
2018-03-10T22:01:49.537240-05:00
QDATAPDB(4):ALTER PLUGGABLE DATABASE pdb1 REFRESH MODE EVERY 1 MINUTES
QDATAPDB(4):ORA-65118 signalled during: ALTER PLUGGABLE DATABASE pdb1REFRESH MODE EVERY 1 MINUTES...
2018-03-10T22:02:21.179776-05:00
QDATAPDB(4):ALTER PLUGGABLE DATABASE qdatapdb REFRESH MODE EVERY 1 MINUTES
QDATAPDB(4):Completed: ALTER PLUGGABLE DATABASE qdatapdb REFRESH MODEEVERY 1 MINUTES
2018-03-10T22:02:21.357524-05:00
QDATAPDB(4):alter pluggable database refresh
QDATAPDB(4):Completed: alter pluggable database refresh
2018-03-10T22:03:21.502428-05:00
QDATAPDB(4):alter pluggable database refresh
QDATAPDB(4):Completed: alter pluggable database refresh
2018-03-10T22:04:21.680468-05:00
QDATAPDB(4):alter pluggable database refresh
QDATAPDB(4):Completed: alter pluggable database refresh
經過日誌能夠觀察到目標端每分鐘自動刷新一次。
(10)檢查目標端數據同步狀況
sys. qdatapdb>selectcount(*) from yyh.t;
COUNT(*)
----------
72714
Elapsed: 00:00:00.03
sys. qdatapdb>select open_mode from v$database;
OPEN_MODE
--------------------
READ ONLY
Elapsed: 00:00:00.03
sys. qdatapdb>shutdown immediate
Pluggable Database closed.
sys. qdatapdb>
因目標庫在OPEN狀態下,雖然日誌中顯示每分鐘刷新,可是數據實際並未正真同步。
(11)1分鐘後再次檢查數據同步狀況
sys. qdatapdb>alter database open read only;
Database altered.
Elapsed: 00:00:01.89
sys. qdatapdb>selectcount(*) from yyh.t;
select count(*) from yyh.t
*
ERROR at line 1:
ORA-00942: table or view does not exist
Elapsed: 00:00:00.01
sys. qdatapdb>
數據已經同步完成。
(12)轉換爲目標PDB:QDataPDB爲非none PDB
sys. ora12c>select pdb_id, pdb_name, status, refresh_mode,refresh_interval from cdb_pdbs;
PDB_ID PDB_NAME STATUS REFRES REFRESH_INTERVAL
---------- ------------------------------ ---------- ----------------------
2 PDB$SEED NORMAL NONE
4 QDATAPDB REFRESHINGAUTO 1
Elapsed: 00:00:00.03
sys. ora12c>alter pluggable database qdatapdb close;
Pluggable database altered.
Elapsed: 00:00:00.19
sys. ora12c>alter session set container=qdatapdb;
Session altered.
Elapsed: 00:00:00.00
sys. qdatapdb>ALTERPLUGGABLE DATABASE qdatapdb REFRESH MODE NONE;
Pluggable database altered.
Elapsed: 00:00:04.01
sys. ora12c>ALTERPLUGGABLE DATABASE qdatapdb REFRESH MODE manual;
ALTER PLUGGABLE DATABASE qdatapdb REFRESH MODE manual
*
ERROR at line 1:
ORA-65261: pluggable database QDATAPDB not enabled for refresh
Elapsed: 00:00:00.00
sys. ora12c>
Note:禁用PDB Refresh功能後,就不能再轉換爲Refresh PDB。
(13)檢查QDataPDB狀態
sys. ora12c>conn /as sysdba
Connected.
sys. ora12c>select pdb_id, pdb_name, status, refresh_mode, refresh_intervalfrom cdb_pdbs;
PDB_ID PDB_NAME STATUS REFRES REFRESH_INTERVAL
---------- ------------------------------ ---------- ----------------------
2 PDB$SEED NORMAL NONE
4 QDATAPDB NEW NONE
Elapsed: 00:00:00.00
sys. ora12c>alterpluggable database qdatapdb open;
Pluggable database altered.
Elapsed: 00:00:05.02
sys. ora12c>selectpdb_id, pdb_name, status, refresh_mode, refresh_interval from cdb_pdbs;
PDB_ID PDB_NAME STATUS REFRES REFRESH_INTERVAL
---------- ------------------------------ ---------- ----------------------
2 PDB$SEED NORMAL NONE
4 QDATAPDB NORMAL NONE
Elapsed: 00:00:00.00
sys. ora12c>
QDataPDB的狀態已經由自動刷新轉換成爲了普通的PDB。
在 18c 中支持將經過 dblink 建立的可刷新 PDB 進行源 PDB 和克隆 PDB 進行 switchover ,源 PDB 將成爲克隆 PDB ,原來的克隆 PDB 將成爲源 PDB 。
<span style="color:#000000">SQL> show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 LEIPDB READ WRITE NO </span>
<span style="color:#000000">DROP USER c##lei CASCADE; CREATE USER c##lei IDENTIFIED BY oracle; GRANT CREATE SESSION, RESOURCE, CREATE ANY TABLE, UNLIMITED TABLESPACE TO c##lei CONTAINER=ALL; GRANT CREATE PLUGGABLE DATABASE TO c##lei CONTAINER=ALL; GRANT SYSOPER TO c##lei CONTAINER=ALL; </span>
<span style="color:#000000">ALTER SESSION SET CONTAINER = leipdb; CREATE TABLE cndba(id NUMBER); INSERT INTO cndba VALUES(1); COMMIT; SELECT * FROM cndba; N1 ---------- 1 </span>
<span style="color:#000000">DROP USER c##lei CASCADE; CREATE USER c##lei IDENTIFIED BY oracle; GRANT CREATE SESSION, RESOURCE, CREATE ANY TABLE, UNLIMITED TABLESPACE TO c##lei CONTAINER=ALL; GRANT CREATE PLUGGABLE DATABASE TO c##lei CONTAINER=ALL; GRANT SYSOPER TO c##lei CONTAINER=ALL; </span>
配置TNS
<span style="color:#000000">LEICDB = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.179)(PORT = 1521)) ) (CONNECT_DATA = (SERVICE_NAME = lei) ) ) </span>
建立dblink
<span style="color:#000000">CREATE DATABASE LINK leicdb_dblink CONNECT TO c##lei IDENTIFIED BY oracle USING 'leicdb';1 </span>
驗證DBLINK是否正常
<span style="color:#000000">SQL> select COUNT(*) from v$version@leicdb_dblink; COUNT(*) ---------- 1 </span>
<span style="color:#000000">CREATE PLUGGABLE DATABASE leicdb_pdb FROM leipdb@leicdb_dblink CREATE_FILE_DEST='/u01/app/oracle/oradata/' REFRESH MODE MANUAL; </span>
<span style="color:#000000">SQL> INSERT INTO cndba VALUES(2); SQL> select * from cndba; ID ---------- 1 2 SQL> commit; </span>
刷新PDB
<span style="color:#000000">ALTER SESSION SET CONTAINER = leicdb_pdb; ALTER PLUGGABLE DATABASE REFRESH; </span>
查看新數據是否同步過來
<span style="color:#000000">ALTER PLUGGABLE DATABASE leicdb_pdb OPEN READ ONLY; ALTER SESSION SET CONTAINER = leicdb_pdb; select * from cndba; ID ---------- 1 </span>
<span style="color:#000000">LEICDB2 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.178)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = lei) ) ) </span>
建立dblink
<span style="color:#000000">CREATE DATABASE LINK leicdb2_dblink CONNECT TO c##lei IDENTIFIED BY oracle USING 'leicdb2'; </span>
驗證
<span style="color:#000000">select COUNT(*) from v$version@leicdb2_dblink; COUNT(*) ---------- 1 </span>
<span style="color:#000000">ALTER SESSION SET CONTAINER = leipdb; ALTER PLUGGABLE DATABASE REFRESH MODE MANUAL FROM leicdb_pdb@leicdb2_dblink SWITCHOVER; </span>
至此就完成了可刷新PDB的在線switchover。