代码改变世界

番外:可刷新PDB的管理操作(如何切换PDB Switching Over)

2019-09-01 08:31  askscuti  阅读(1101)  评论(0编辑  收藏  举报

基于版本:19c (12.2.0.3) AskScuti

主题:可刷新PDB如何进行切换操作

内容说明:本篇延续如何克隆可刷新的PDB(Refreshable PDB)一文,进行切换实验。

具体请参考《克隆可刷新的PDB

 

内容总览

1. 环境概述

2. 如何刷新PDB

  2.1 连接要刷新PDB所在的根容器

  2.2 关闭要刷新的PDB

  2.3 执行刷新

  2.4 只读模式打开可刷新的PDB(或保留关闭状态) 

3. 如何切换可刷新PDB

  3.1 检查目标库环境

  3.2 目标库创建用户并授权

  3.3 源库编辑TNS

  3.4 源库创建DBLink

  3.5 源PDB与目标PDB进行切换

4. ORA-12754 的说明

 

1. 环境概述

具体请参考:如何克隆可刷新的PDB(Refreshable PDB)一文。

 

2. 如何刷新PDB

要刷新一个PDB,前提:该PDB必须是以包含REFRESH MODE MANUAL 或者 REFRESH MODE EVERY minutes 子句克隆出来的PDB。

2.1 连接要刷新PDB所在的根容器

SQL> show pdbs

    CON_ID CON_NAME  OPEN MODE  RESTRICTED
---------- --------- ---------- ----------
     2 PDB$SEED      READ ONLY  NO
     3 PDB1          MOUNTED
     4 PDB_REF       READ ONLY  NO
     5 PDB_REFAUTO   MOUNTED

2.2 关闭要刷新的PDB

SQL> alter pluggable database pdb_ref close immediate;

Pluggable database altered.

2.3 执行刷新

SQL> alter pluggable database pdb_ref refresh;

Pluggable database altered.

2.4 只读模式打开可刷新的PDB(或保留关闭状态) 

SQL> alter pluggable database pdb_ref open read only;

Pluggable database altered.

 

3. 如何切换可刷新PDB

可以切换源PDB可刷新PDB的身份角色。

需要注意的是

  • 切换命令必须在源PDB中执行(ALTER PLUGGABLE DATABASE ... SWITCHOVER);
  • 源库和可刷新库不在同一个CDB中时,两个CDB中DBLink指定的用户和密码必须完全一致;
  • DBLink中的字符串必须指向PDB所在的根容器中。

3.1 检查目标库环境

[oracle@erp ~]$ rlwrap sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Aug 31 21:10:03 2019
Version 19.3.0.0.0

Copyright (c) 1982, 2019, Oracle.  All rights reserved.

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.3.0.0.0

SQL> show pdbs

    CON_ID CON_NAME  OPEN MODE  RESTRICTED
---------- --------- ---------- ----------
     2 PDB$SEED      READ ONLY  NO
     3 PDB1          MOUNTED
     4 PDB_REF       READ ONLY  NO
     5 PDB_REFAUTO   MOUNTED

3.2 目标库创建用户并授权

注意和源库一致

SQL> create user c##u1 identified by oracle;

User created.

SQL> grant create session,create pluggable database,sysoper to c##u1 container=all;

Grant succeeded.

3.3 源库编辑TNS

CDB1 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.12)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = CDB1)
    )
  )

3.4 源库创建DBLink

SQL> create database link cdb1_dblink connect to c##u1 identified by oracle using 'CDB1';

Database link created.

SQL> select * from tab@cdb1_dblink;

no rows selected

3.5 源PDB与目标PDB进行切换

切换前,看下源PDB的状态

SQL> show pdbs

    CON_ID CON_NAME  OPEN MODE  RESTRICTED
---------- --------- ---------- ----------
     3 ERP1          READ WRITE NO   <--- STATUS:READ WRITE

切换前,看下目标PDB的状态

SQL> show pdbs

    CON_ID CON_NAME  OPEN MODE  RESTRICTED
---------- --------- ---------- ----------
     2 PDB$SEED      READ ONLY  NO
     3 PDB1          MOUNTED
     4 PDB_REF       READ ONLY  NO    <--- STATUS:READ ONLY 只读
     5 PDB_REFAUTO   MOUNTED

源数据库ERP1中执行切换命令

SQL> alter session set container=erp1;

Session altered.

SQL> show pdbs

    CON_ID CON_NAME  OPEN MODE  RESTRICTED
---------- --------- ---------- ----------
     3 ERP1          READ WRITE NO

SQL
> alter pluggable database refresh mode manual from pdb_ref@cdb1_dblink switchover; Pluggable database altered.

切换后,看下源PDB状态

SQL> show pdbs

    CON_ID CON_NAME   OPEN MODE  RESTRICTED
---------- ---------- ---------- ----------
     3 ERP1           MOUNTED   <--- 注意:由切换前的读写状态,变为了关闭状态(源库ERP1变为可刷新PDB)

切换后,看下目标PDB状态

SQL> show pdbs

    CON_ID CON_NAME   OPEN MODE  RESTRICTED
---------- ---------- ---------- ----------
     2 PDB$SEED       READ ONLY  NO
     3 PDB1           MOUNTED
     4 PDB_REF        READ WRITE NO  <--- 注意:由切换前的只读状态,变为了读写状态(目标PDB变为源PDB)
     5 PDB_REFAUTO    MOUNTED

最后以只读模式打开可刷新PDB - ERP1

SQL> alter pluggable database erp1 open read only;

Pluggable database altered.

SQL> show pdbs

    CON_ID CON_NAME   OPEN MODE  RESTRICTED
---------- ---------- ---------- ----------
     3 ERP1           READ ONLY  NO

 

4. ORA-12754 的说明

如果环境是非Oracle Cloud使用,在执行 ALTER PLUGGABLE DATABASE refresh_mode FROM clonepdb@dblink SWITCHOVER; 命令时,将提示错误:

ERROR at line 1:
ORA-12754: Feature PDB REFRESH SWITCHOVER is disabled due to missing capability

因为功能受到限制,作为学习测试使用,您可以自行通过某些方法进行解决,但请勿侵犯Oracle软件版权。