Oracle 19c 中CDB和PDB的常见操作介绍

184 篇文章 18 订阅

----使用 sqlplus / as sysdba 登录连接到 CDB

[oracle@ocpstudy ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sun Mar 17 15:35:55 2024
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

----查看有哪些CDB

SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   MOUNTED
         4 PDB 1                                 MOUNTED

----启动 PDB

SQL> alter pluggable database pdb open;

Pluggable database altered.

----查看容器当前状态

SQL> col name format A10
SQL> select name,open_mode from v$pdbs;

NAME       OPEN_MODE
---------- ----------
PDB$SEED   READ ONLY
PDB               READ WRITE
PDB 1            MOUNTED

----关闭 PDB

SYS@orcl(CDB$ROOT)> alter pluggable database pdb close;

Pluggable database altered.

----启动或者关闭 all pdbs

SQL> alter pluggable database all open;
SQL> alter pluggable database all close;
或
SQL> alter pluggable database all close immediate;

----排除某些 pdbs 不启动,启动所有未排除的pdbs

SQL>alter pluggable database all except pdb1 open;
SQL>alter pluggable database all except pdb1 close immediate;

----以只读方式打开PDBS

SQL>alter pluggable database pdb,pdb1 open read only force;
SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   READ ONLY  NO
         4 PDB 1                                READ ONLY  NO

----以受限模式打开PDB

SQL> alter pluggable database all close;
SQL> alter pluggable database pdb1 open restricted;
SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY    NO
         3 PDB                                   READ ONLY    NO
         4 PDB 1                                READ WRITE  YES

----切换容器

--查看当前所在容器
SQL> show con_name

CON_NAME
------------------------------
CDB$ROOT

--切换至容器pdb
SQL> alter session set container=pdb;

Session altered.

--查看当前所在容器
SQL> show con_name

CON_NAME
------------------------------
PDB
SQL> show pdbs

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

--切换到根容器
SQL> alter session set container=cdb$root;
或
SQL> conn / as sysdba

SQL> show con_name

CON_NAME
------------------------------
CDB$ROOT

–关闭pdb
当已经在pdb里面时,可通过执行shutdown和startup命令关闭和启动容器

SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         3 PDB                            READ WRITE NO
 
 SQL> shutdown
 SQL> startup

默认情况下使用 sqlplus / as sysdba 登录连接的是 CDB。在 CDB 启动之后,PDB 是自动启动到 mount 状态,而不是 OPEN状态。

SQL> conn / as sysdba
SQL> show con_name

CON_NAME
------------------------------
CDB$ROOT
SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   READ WRITE NO
         4 PDB                                   READ WRITE NO
SQL> shutdown immediate
SQL> startup
SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   MOUNTED
         4 PDB 1                                MOUNTED

如果想在CDB启动的时候自动启动PDBS,该怎么实现呢?
方法一:在pdb open 状态下保存状态

SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   MOUNTED
         4 PDB 1                                MOUNTED
SQL> alter pluggable database all open;

Pluggable database altered.

SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   READ WRITE NO
         4 PDB                                   READ WRITE NO
SQL> alter pluggable database all save state;

Pluggable database altered.
SQL> shutdown immediate
SQL> startup
SQL> show pdbs

    CON_ID CON_NAME              OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB                                   READ WRITE NO
         4 PDB                                   READ WRITE NO

方法二:设置触发器

create trigger open_all_pdbs
after startup on database
begin
execute immediate 'alter pluggable database all open';
end open_all_pdbs;
/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值