Oracle12c中引入的多宿主选项(multitenant option)允许一个容器数据库容纳多个独立的可插拔数据库(PDB)。本文将说明如何启动和关闭容器数据库(CDB)和可插拔数据库(PDB)。
1. 容器数据库(CDB)
启动和关闭容器数据库和以往一样。当作为授权用户连接到CDB时,SQL*Plus中可以使用STARTUP和SHUTDOWN命令。具体语法如下:
STARTUP [NOMOUNT | MOUNT | RESTRICT | UPGRADE| FORCE | READ ONLY]
SHUTDOWN [IMMEDIATE | ABORT]
2. 可插拔数据库(PDB)
可插拔数据库能用SQL*Plus命令或 ALTER PLUGGABLEDATABASE命令启动和关闭。
3. SQL*Plus命令
当作为授权用户连接至可插拔数据库时,如下命令可用来启动和关闭数据库:
STARTUP FORCE;
STARTUP OPEN READ WRITE [RESTRICT];
STARTUP OPEN READ ONLY [RESTRICT];
STARTUP UPGRADE;
SHUTDOWN [IMMEDIATE];
示例如下:
STARTUP FORCE;
SHUTDOWN IMMEDIATE;
STARTUP OPEN READ WRITE RESTRICT;
SHUTDOWN;
STARTUP;
SHUTDOWN IMMEDIATE;
4. ALTER PLUGGABLE DATABASE
ALTER PLUGGABLE DATABASE命令能在CDB或PDB中使用。
当作为授权用户连接至PDB时,如下命令可用来打开和关闭目前的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];
示例如下。
ALTER PLUGGABLE DATABASE OPEN READ ONLYFORCE;
ALTER PLUGGABLE DATABASE CLOSE IMMEDIATE;
ALTER PLUGGABLE DATABASE OPEN READ WRITE;
ALTER PLUGGABLE DATABASE CLOSE IMMEDIATE;
当作为授权用户连接至CDB时,如下命令可用来打开和关闭一个或多个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];
子句可以是如下值:
一个或多个以逗号分隔的PDB名。
ALL关键字指所有PDB。
ALL EXCEPT关键字,后面跟一个或多个逗号分隔的PDB,指一个PDB子集。
示例如下:
ALTER PLUGGABLE DATABASE pdb1, pdb2 OPEN READONLY FORCE;
ALTER PLUGGABLE DATABASE pdb1, pdb2 CLOSEIMMEDIATE;
ALTER PLUGGABLE DATABASE ALL OPEN;
ALTER PLUGGABLE DATABASE ALL CLOSE IMMEDIATE;
ALTER PLUGGABLE DATABASE ALL EXCEPT pdb1OPEN;
ALTER PLUGGABLE DATABASE ALL EXCEPT pdb1CLOSE IMMEDIATE;
5. 可插拔数据库(PDB)自动启动
Oracle12.1.0.2补丁集已引入了保持PDB启动状态的能力,因此,不用再通过下面的触发器方式来实现自动打开PDB。
Oracle12.1.0.2之前,CDB启动后,所有的PDB都处于加载状态。CDB启动时,没有默认机制自动启动PDB。只能通过在CDB上建立一个系统触发器来自动启动部分或所有PDB。
CREATE OR REPLACE TRIGGER open_pdbs
AFTERSTARTUP ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'ALTER PLUGGABLEDATABASE ALL OPEN';
END open_pdbs;
/
如果你不想启动所有的PDB,可以定制修改该触发器。
6. 保持PDB处于启动状态(12.1.0.2之后)
Oracle12.1.0.2补丁集可以让PDB在CDB重启时保持启动状态。这可以通过ALTERPLUGGABLE DATABASE命令实现。
我们可以观察CDB重启的整个过程。注意,重启前,PDB都处于可读写模式,但重启后却处于加载模式。
SELECT name, open_mode FROM v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READ ONLY
PDB1 READWRITE
PDB2 READWRITE
SQL>
SHUTDOWN IMMEDIATE;
STARTUP;
SELECT name, open_mode FROM v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READONLY
PDB1 MOUNTED
PDB2 MOUNTED
SQL>
接着,我们打开两个PDBS,但只保存PDB1的状态。
ALTER PLUGGABLE DATABASE pdb1 OPEN;
ALTER PLUGGABLE DATABASE pdb2 OPEN;
ALTER PLUGGABLE DATABASE pdb1 SAVE STATE;
视图DBA_PDB_SAVED_STATES显示容器保存状态的信息。
COLUMN con_name FORMAT A20
COLUMN instance_name FORMAT A20
SELECT con_name, instance_name, state FROMdba_pdb_saved_states;
CON_NAME INSTANCE_NAME STATE
-------------------- ----------------------------------
PDB1 cdb1 OPEN
SQL>
重启CDB将会出现一个不同结果。
SELECT name, open_mode FROM v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READONLY
PDB1 READWRITE
PDB2 READWRITE
SQL>
SHUTDOWN IMMEDIATE;
STARTUP;
SELECT name, open_mode FROM v$pdbs;
NAME OPEN_MODE
------------------------------ ----------
PDB$SEED READONLY
PDB1 READWRITE
PDB2 MOUNTED
SQL>
被保存的状态可以通过如下命令被废弃。
ALTER PLUGGABLE DATABASE pdb1 DISCARD STATE;
COLUMN con_name FORMAT A20
COLUMN instance_name FORMAT A20
SELECT con_name, instance_name, state FROMdba_pdb_saved_states;
no rows selected
SQL>
7. 注意: