在Oracle 12c/19c多租户环境中,默认情况下,使用startup命令启动数据库实例后,你会发现PDB数据库的状态为MOUNT状态,PDB不会随着CDB启动而启动。如下例子所示:
SQL> startup
ORACLE instance started.
Total System Global Area 2432695872 bytes
Fixed Size 9137728 bytes
Variable Size 620756992 bytes
Database Buffers 1795162112 bytes
Redo Buffers 7639040 bytes
Database mounted.
Database opened.
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PSUAT MOUNTED
SQL>
Oracle 12.1.0.2之前,CDB启动后,所有的PDB都处于加载状态(MOUNTED)。CDB启动时,没有默认机制自动启动PDB。只能通过在CDB上建立一个系统触发器来自动启动部分或所有PDB,如下所示:
CREATE OR REPLACE TRIGGER open_pdbs
AFTER STARTUP ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN';
END open_pdbs;
/
Oracle12.1.0.2补丁集已引入了保持PDB启动状态的能力,因此,不用再通过上面的触发器方式来实现自动打开PDB。可以通过下面方式让PDB数据库自动启动。
SQL>ALTER PLUGGABLE DATABASE PTEST1 OPEN;
SQL>ALTER PLUGGABLE DATABASE PTEST1 SAVE STATE;
SQL>SHUTDOWN IMMEDIATE;
SQL>STARTUP
SQL>SHOW PDBS;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PTEST2 MOUNTED
4 PTEST1 READ WRITE
如上所示,使用命令保存了PDB的STATE后,下次使用命令startup重启实例,对应的PDB就会启动到相应状态(READ WRITE)
--查看PDB保存的状态
COLUMN CON_NAME FORMAT A20
COLUMN INSTANCE_NAME FORMAT A20
SELECT CON_NAME, INSTANCE_NAME, STATE FROM DBA_PDB_SAVED_STATES;
--被保存的状态可以通过如下命令被废弃。
ALTER PLUGGABLE DATABASE T1PDB DISCARD STATE;
更多详细信息,可以参考官方文档How to Preserve Open Mode of PDBs When the CDB Restarts ( 文档 ID 1933511.1) 。