在Oracle 12c中,分CDB 和PDB,他們的啟動和關閉操作整理如下。
對於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 rightsreserved. Connectedto: OracleDatabase 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production Withthe 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 Databaseclosed. Databasedismounted. ORACLEinstance shut down.
我們關閉CDB之前PDB 是沒有關閉的,所以這個操作也會把PDB 關閉掉。
SQL>startup ORACLEinstance started. TotalSystem Global Area 1269366784 bytes FixedSize 2287912 bytes VariableSize 788530904 bytes DatabaseBuffers 469762048 bytes RedoBuffers 8785920 bytes Databasemounted. Databaseopened. 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。
PDB 的操作可以通過SQL*PLUS 來操作,也可以通過ALTER PLUGGABLE DATABASE 命令操作。
因為默認連接的是CDB,所以必須先指定PDB才可以通過sqlplus來啟動和關閉PDB,
具體語法和普通實例一樣:
STARTUPFORCE; STARTUPOPEN READ WRITE [RESTRICT]; STARTUPOPEN READ ONLY [RESTRICT]; STARTUPUPGRADE; SHUTDOWN[IMMEDIATE]; SQL>show con_name CON_NAME ------------------------------ CDB$ROOT SQL>selectcon_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; Sessionaltered. SQL>startup PluggableDatabase 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的信息了。
如果在PDB中可以使用如下語法:
ALTERPLUGGABLE DATABASE OPEN READ WRITE [RESTRICTED] [FORCE]; ALTERPLUGGABLE DATABASE OPEN READ ONLY [RESTRICTED] [FORCE]; ALTERPLUGGABLE DATABASE OPEN UPGRADE [RESTRICTED]; ALTERPLUGGABLE DATABASE CLOSE [IMMEDIATE];
如果是在CDB中,可以使用如下語法:
ALTERPLUGGABLE DATABASE <pdd-name-clause> OPEN READ WRITE [RESTRICTED][FORCE]; ALTERPLUGGABLE DATABASE <pdd-name-clause> OPEN READ ONLY [RESTRICTED] [FORCE]; ALTERPLUGGABLE DATABASE <pdd-name-clause> OPEN UPGRADE [RESTRICTED]; ALTERPLUGGABLE DATABASE <pdd-name-clause> CLOSE [IMMEDIATE];
<pdd-name-clause>表示的是多個PDB,如果有多個,用逗號分開。 也可以使用ALL或者ALL EXCEPT關鍵字來替代。
ALL:表示所有的PDBS。
ALLEXCEPT 表示需要排除的PDBS。
如:
ALTERPLUGGABLE DATABASE pdb1, pdb2 OPEN READ ONLY FORCE; ALTERPLUGGABLE DATABASE pdb1, pdb2 CLOSE IMMEDIATE; ALTERPLUGGABLE DATABASE ALL OPEN; ALTERPLUGGABLE DATABASE ALL CLOSE IMMEDIATE; ALTERPLUGGABLE DATABASE ALL EXCEPT pdb1 OPEN; ALTERPLUGGABLE DATABASE ALL EXCEPT pdb1 CLOSE IMMEDIATE;
示例:
SQL>alter session set container=CDB$ROOT; Sessionaltered. 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 ; Pluggabledatabase 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; Sessionaltered. SQL>ALTER PLUGGABLE DATABASE OPEN; Pluggabledatabase altered. SQL> select con_id, dbid, guid, name , open_modefrom v$pdbs; CON_ID DBID GUID NAME OPEN_MODE -------------------- -------------------------------- --------------- ---------- 3 426143573 F7C209EB1DFC0854E0430A01A8C0B787PDBCNDBA READ WRITE
默認情況下,在CDB 啟動的時候,all 的PDB 都是mount狀態,也沒有默認的機制,在CDB啟動時自動啟動PDB。
但這裡可以通過觸發器來實現PDB的自動open:
CREATEOR REPLACE TRIGGER open_pdbs AFTER STARTUP ON DATABASE BEGIN EXECUTE IMMEDIATE 'ALTERPLUGGABLE DATABASE ALL OPEN'; END open_pdbs; /
示例:
SQL>show con_name CON_NAME ------------------------------ PDBCNDBA SQL>alter session set container=CDB$ROOT; Sessionaltered. SQL>CREATE OR REPLACE TRIGGER open_pdbs 2 AFTER STARTUP ON DATABASE 3 BEGIN 4 EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN'; 5 ENDopen_pdbs; 6 / Triggercreated.