Oracle12c CDB和PDB数据库的启动与关闭说明
Oracle 12c中,增加了可插接数据库的概念,即PDB,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。
CDB全称为Container Database,中文翻译为数据库容器,PDB全称为Pluggable Database,即可插拔数据库。
在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。
而实例与数据库不可能是一对多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。
下面是官方文档关于CDB与PDB的关系图。
1 Container Database (CDB) 对于CDB,启动和关闭与之前传统的方式一样,具体语法如下: STARTUP[NOMOUNT | MOUNT | RESTRICT | UPGRADE | FORCE | READ ONLY] SHUTDOWN[IMMEDIATE | ABORT] 要注意,在12c数据库创建完成后,默认情况下使用sqlplus / as sysdba 登录连接的是CDB。 [oracle@Ora12c~]$ sqlplus / as sysdba SQL*Plus:Release 12.1.0.1.0 Production on Thu Apr 24 17:50:34 2014 Copyright(c) 1982, 2013, Oracle. All rights reserved. Connected to: Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options SQL>show con_name CON_NAME ------------------------------ CDB$ROOT SQL> SQL>select con_id, dbid, guid, name , open_mode from v$pdbs; CON_ID DBID GUID NAME OPEN_MODE ---------- ------------------------------------------ ------------------------------ ---------- 2 4088301206 F7C1E3C96BBF0585E0430A01A8C05459PDB$SEED READ ONLY 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA READ WRITE 我们现在连接的是CDB,即root container。 我们关闭CDB: SQL>shutdown immediate Database closed. Database dismounted. ORACLE instance shut down. 我们关闭CDB之前PDB 是没有关闭的,所以这个操作也会把PDB 关闭掉。 SQL>startup ORACLE instance started. Total System Global Area 1269366784 bytes Fixed Size 2287912 bytes Variable Size 788530904 bytes Database Buffers 469762048 bytes Redo Buffers 8785920 bytes Database mounted. Database opened. SQL> select con_id, dbid, guid, name , open_modefrom v$pdbs; CON_ID DBID GUID NAME OPEN_MODE ---------- ------------------------------------------ ------------------------------ ---------- 2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED READ ONLY 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA MOUNTED 注意这里的PDB,在CDB 启动之,PDB 是自动启动到mount状态,而不是OPEN。 所以我们还需要手工去open它,当然,也可以通过在CDB中配置触发器来自动open。 2 Pluggable Database (PDB) PDB 的操作可以通过SQL*PLUS 来操作,也可以通过ALTER PLUGGABLE DATABASE 命令操作。 2.1 使用SQL*Plus 命令 因为默认连接的是CDB,所以必须先指定PDB才可以通过sqlplus来启动和关闭PDB, 具体语法和普通实例一样: STARTUP FORCE; STARTUP OPEN READ WRITE [RESTRICT]; STARTUP OPEN READ ONLY [RESTRICT]; STARTUP UPGRADE; SHUTDOWN[IMMEDIATE]; SQL>show con_name CON_NAME ------------------------------ CDB$ROOT SQL>select con_id, dbid, guid, name , open_mode from v$pdbs; CON_ID DBID GUID NAME OPEN_MODE -------------------- -------------------------------- --------------- ---------- 2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED READ ONLY 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA MOUNTED 注意这里是mount,表示PDB 是关闭的。 --指定PDB 数据库: SQL>alter session set container=pdbcndba; Session altered. SQL>startup Pluggable Database opened. SQL>select con_id, dbid, guid, name , open_mode from v$pdbs; CON_ID DBID GUID NAME OPEN_MODE -------------------- -------------------------------- --------------- ---------- 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA READ WRITE SQL> 启动成功。 当我们切换到PDB之后,就看不到seed PDB的信息了。 2.2 使用ALTER PLUGGABLE DATABASE命令 如果在PDB中可以使用如下语法: ALTER PLUGGABLE DATABASE OPEN READ WRITE [RESTRICTED] [FORCE]; ALTER PLUGGABLE DATABASE OPEN READ ONLY [RESTRICTED] [FORCE]; ALTER PLUGGABLE DATABASE OPEN UPGRADE [RESTRICTED]; ALTER PLUGGABLE DATABASE CLOSE [IMMEDIATE]; 如果是在CDB中,可以使用如下语法: ALTER PLUGGABLE DATABASE <pdd-name-clause> OPEN READ WRITE [RESTRICTED][FORCE]; ALTER PLUGGABLE DATABASE <pdd-name-clause> OPEN READ ONLY [RESTRICTED] [FORCE]; ALTER PLUGGABLE DATABASE <pdd-name-clause> OPEN UPGRADE [RESTRICTED]; ALTER PLUGGABLE DATABASE <pdd-name-clause> CLOSE [IMMEDIATE]; <pdd-name-clause>表示的是多个PDB,如果有多个,用逗号分开。 也可以使用ALL或者ALL EXCEPT关键字来替代。 ALL:表示所有的PDBS。 ALL EXCEPT 表示需要排除的PDBS。 如: ALTER PLUGGABLE DATABASE pdb1, pdb2 OPEN READ ONLY FORCE; ALTER PLUGGABLE DATABASE pdb1, pdb2 CLOSE IMMEDIATE; ALTER PLUGGABLE DATABASE ALL OPEN; ALTER PLUGGABLE DATABASE ALL CLOSE IMMEDIATE; ALTER PLUGGABLE DATABASE ALL EXCEPT pdb1 OPEN; ALTER PLUGGABLE DATABASE ALL EXCEPT pdb1 CLOSE IMMEDIATE; 示例: SQL>alter session set container=CDB$ROOT; Session altered. SQL>select con_id, dbid, guid, name , open_mode from v$pdbs; CON_ID DBID GUID NAME OPEN_MODE -------------------- -------------------------------- --------------- ---------- 2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED READ ONLY 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787 PDBCNDBA READ WRITE SQL>ALTER PLUGGABLE DATABASE ALL CLOSE ; Pluggable database altered. SQL>select con_id, dbid, guid, name , open_mode from v$pdbs; CON_ID DBID GUID NAME OPEN_MODE -------------------- -------------------------------- --------------- ---------- 2 4088301206F7C1E3C96BBF0585E0430A01A8C05459 PDB$SEED READ ONLY 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787PDBCNDBA MOUNTED SQL>alter session set container=pdbcndba; Session altered. SQL>ALTER PLUGGABLE DATABASE OPEN; Pluggable database altered. SQL> select con_id, dbid, guid, name , open_mode from v$pdbs; CON_ID DBID GUID NAME OPEN_MODE -------------------- -------------------------------- --------------- ---------- 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787PDBCNDBA READ WRITE 3 设置Pluggable Database (PDB) 的自动startup 默认情况下,在CDB 启动的时候,all 的PDB 都是mount状态,也没有默认的机制,在CDB启动时自动启动PDB。 但这里可以通过触发器来实现PDB的自动open: CREATE OR REPLACE TRIGGER open_pdbs AFTER STARTUP ON DATABASE BEGIN EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN'; END open_pdbs; / 示例: SQL>show con_name CON_NAME ------------------------------ PDBCNDBA SQL>alter session set container=CDB$ROOT; Session altered. SQL>CREATE OR REPLACE TRIGGER open_pdbs 2 AFTER STARTUP ON DATABASE 3 BEGIN 4 EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN'; 5 END open_pdbs; 6 / Triggercreated.
All for u