CDB和PDB的創建、連接、啟動、關閉
阿新 • • 發佈:2019-01-14
exe 應用 water 驗證過 會話 關閉 nta https sql 一、CDB和PDB基本管理
基本概念:
Multitenant Environment:多租戶環境
CDB(Container Database):數據庫容器
PDB(Pluggable Database):可插拔數據庫
二、創建
2.1創建CDB
PDB包含在CDB中,所以,要創建PDB,必須先創建CDB。一臺機器上可以有多個CDB。Oracle軟件安裝完成後,就可以創建CDB
啟動DBCA
C:\app\oracle\product\12.1.0\dbhome_1\BIN\dbca.bat)
2.2創建PDB
C:\app\oracle\product\12.1.0\dbhome_1\BIN\dbca.bat
三、連接
3.1連接CDB
Sqlplus默認連接CDB
sqlplus sys/oracle as sysdba
3.2連接PDB
pdb可以通過在CDB中alter session container
alter session set container=pdb1
也可以直接通過tns方式(如下)登錄
sqlplus sys/oracle@pdb1 as sysdba
其中tnsnames.ora添加
PDB1 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = pdb1) ) )
四、啟動與關閉
4.1啟動關閉CDB
SQL> show con_name CON_NAME ------------------------------ CDB$ROOT SQL> shutdown immediate 數據庫已經關閉。 已經卸載數據庫。 ORACLE 例程已經關閉。 SQL> startup ORACLE 例程已經啟動。 Total System Global Area 5044088832 bytes Fixed Size 2413072 bytes Variable Size 1040190960 bytes Database Buffers 3992977408 bytes Redo Buffers 8507392 bytes 數據庫裝載完畢。 數據庫已經打開。 SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs; CON_ID DBID NAME OPEN_MODE ---------- ---------- ------------------------------ ---------- 4122484437 PDB$SEED READ ONLY 3387525155 PDB1 MOUNTED 3946119498 PDB2 MOUNTED SQL>
從上面的操作中可以看到數據庫啟動的時候所有的PDBs的狀態為MOUNTED
4.2啟動關閉PDB
pdb的管理可以在cdb中進行也可以在pdb中進行,如果是cdb中進行,需要PLUGGABLE關鍵字,如果是在pdb中直接和普通數據庫一樣
SQL> show con_name
CON_NAME
------------------------------
CDB$ROOT
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
4122484437 PDB$SEED READ ONLY
3387525155 PDB1 MOUNTED
3946119498 PDB2 MOUNTED
SQL> alter pluggable database pdb1 open;
插接式數據庫已變更。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
4122484437 PDB$SEED READ ONLY
3387525155 PDB1 READ WRITE
3946119498 PDB2 MOUNTED
SQL> alter pluggable database pdb2 open;
插接式數據庫已變更。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
4122484437 PDB$SEED READ ONLY
3387525155 PDB1 READ WRITE
3946119498 PDB2 READ WRITE
SQL> alter pluggable database pdb1 close;
插接式數據庫已變更。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
4122484437 PDB$SEED READ ONLY
3387525155 PDB1 MOUNTED
3946119498 PDB2 READ WRITE
SQL> alter pluggable database pdb2 close;
插接式數據庫已變更。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
4122484437 PDB$SEED READ ONLY
3387525155 PDB1 MOUNTED
3946119498 PDB2 MOUNTED
SQL> alter pluggable database all open;
插接式數據庫已變更。
SQL> alter pluggable database all close;
插接式數據庫已變更。
SQL> alter session set container=pdb1 ;
會話已更改。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
3387525155 PDB1 MOUNTED
SQL> startup
插接式數據庫已打開。
SQL> shutdown
插接式數據庫已關閉。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
3387525155 PDB1 MOUNTED
SQL>
4.3自動啟動所有PDB
從上面的操作中可以看到數據庫啟動的時候所有的PDBs的狀態為MOUNTED,在實際應用可以考慮增加如下的觸發器使所有的PDBs都處於打開狀態。
create or replace trigger sys.after_startup after startup on database
begin
execute immediate ‘alter pluggable database all open ‘ ;
end after_startup ;
驗證過程
SQL> create or replace trigger sys.after_startup after startup on database
begin
execute immediate ‘alter pluggable database all open ‘ ;
end after_startup ;
/
觸發器已創建
SQL> shutdown
數據庫已經關閉。
已經卸載數據庫。
ORACLE 例程已經關閉。
SQL> startup
ORACLE 例程已經啟動。
Total System Global Area 5044088832 bytes
Fixed Size 2413072 bytes
Variable Size 1040190960 bytes
Database Buffers 3992977408 bytes
Redo Buffers 8507392 bytes
數據庫裝載完畢。
數據庫已經打開。
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;
CON_ID DBID NAME OPEN_MODE
---------- ---------- ------------------------------ ----------
4122484437 PDB$SEED READ ONLY
3387525155 PDB1 READ WRITE
3946119498 PDB2 READ WRITE
SQL>
五、常用SQL
--查看ORACLE 12C版本
select * from v$version;
--查看容器名稱和ID
show con_name ;
--查看狀態
select name ,open_mode from v$pdbs ;
--切換container
alter session set container=pdb1
CDB和PDB的創建、連接、啟動、關閉