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@dg1 ~]$ sqlplus "/as sysdba"

SQL*Plus: Release 12.2.0.1.0 Production on Tue Jul 25 00:23:09 2017

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


Connected to:
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

SQL> show con_name

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

SQL> col name for a30       
SQL> set lines 200
SQL> select con_id, dbid, guid, name , open_mode from v$pdbs;

    CON_ID     DBID GUID                   NAME                  OPEN_MODE
---------- ---------- -------------------------------- ------------------------------ ----------
     2 1317028608 520C10C9CB093123E055F64A1FAC93B3 PDB$SEED               READ ONLY
     3 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 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 1426063360 bytes
Fixed Size            8792776 bytes
Variable Size          939525432 bytes
Database Buffers      469762048 bytes
Redo Buffers            7983104 bytes
Database mounted.
Database opened.

SQL> select con_id, dbid, guid, name , open_mode from v$pdbs;

    CON_ID     DBID GUID                   NAME                  OPEN_MODE
---------- ---------- -------------------------------- ------------------------------ ----------
     2 1317028608 520C10C9CB093123E055F64A1FAC93B3 PDB$SEED               READ ONLY
     3 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 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];
STARTUPU PGRADE;
SHUTDOWN[IMMEDIATE];
SQL> show con_name

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

SQL> set lines 200
SQL> select con_id, dbid, guid, name , open_mode from v$pdbs;

    CON_ID     DBID GUID                   NAME                  OPEN_MODE
---------- ---------- -------------------------------- ------------------------------ ----------
     2 1317028608 520C10C9CB093123E055F64A1FAC93B3 PDB$SEED               READ ONLY
     3 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 PDBCNDBA               MOUNTED
 注意这里是mount,表示PDB 是关闭的。
SQL> shutdown immediate
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 1426063360 bytes
Fixed Size            8792776 bytes
Variable Size          939525432 bytes
Database Buffers      469762048 bytes
Redo Buffers            7983104 bytes
Database mounted.
Database opened.
--指定PDB 数据库:

SQL> select con_id, dbid, guid, name , open_mode from v$pdbs;

    CON_ID     DBID GUID                   NAME                  OPEN_MODE
---------- ---------- -------------------------------- ------------------------------ ----------
     2 1317028608 520C10C9CB093123E055F64A1FAC93B3 PDB$SEED               READ ONLY
     3 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 PDBCNDBA               MOUNTED

SQL> alter session set container=pdbcndba;

Session altered.

SQL> alter database open;

Database 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 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 PDBCNDBA               READ WRITE
启动成功。 当我们切换到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  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或者ALL EXCEPT关键字来替代。
ALL:表示所有的PDBS。
ALLEXCEPT 表示需要排除的PDBS

如:
ALTER PLUGGABLE DATABASE pdbcndba, pdbcndba2 OPEN READ ONLY FORCE;
ALTER PLUGGABLE DATABASE pdbcndba, pdbcndba2 CLOSE IMMEDIATE;
ALTER PLUGGABLE DATABASE ALL OPEN;
ALTER PLUGGABLE DATABASE ALL CLOSE IMMEDIATE;
ALTER PLUGGABLE DATABASE ALL EXCEPT pdbcndba OPEN;
ALTER PLUGGABLE DATABASE ALL pdbcndba 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 1317028608 520C10C9CB093123E055F64A1FAC93B3 PDB$SEED 		      READ ONLY
	 3 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 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 1317028608 520C10C9CB093123E055F64A1FAC93B3 PDB$SEED 		      READ ONLY
	 3 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 PDBCNDBA 		      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 2565515280 520C3FE7B2713E4CE055F64A1FAC93B3 PDBCNDBA 		      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> show con_name

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

SQL> CREATE OR REPLACE TRIGGER open_pdbs  
AFTER STARTUP ON DATABASE   
BEGIN  
EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN';   
END open_pdbs;
/  2    3    4    5    6  

Trigger created.

SQL> SHUTDOWN IMMEDIATE
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> STARTUP
ORACLE instance started.

Total System Global Area 1426063360 bytes
Fixed Size            8792776 bytes
Variable Size          939525432 bytes
Database Buffers      469762048 bytes
Redo Buffers            7983104 bytes
Database mounted.
Database opened.
SQL> show pdbs

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

你可能感兴趣的:(Oracle,12c)