Oracle 12C引入了CDB与PDB的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。
在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。而实例与数据库不可能是一对多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。
tnsnames.ora设置
############################tnsnames.ora######################
#cdb
orcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521)) )
(CONNECT_DATA =
(SERVICE_NAME = orcl) #cdb的db_name
)
)
#pdb
pdborcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = pdborcl) #pdb的db_name
)
)
启动、关闭命令
show con_name --用show查看当前属于哪个容器
select name,open_mode from v$pdbs;
alter session set container=CDB$ROOT;
alter session set container=PDBORCL;
alter pluggable database PDBNAME open ;
alter pluggable database PDBNAME close immediate ;
ALTER PLUGGABLE DATABASE ALL OPEN;
ALTER PLUGGABLE DATABASE ALL CLOSE IMMEDIATE;
col name format a20
col pdb format a10
select name,pdb from cdb services order by con_id;
select name,pdb from v$services order by con_id;
pdb自动启动
默认情况下,在CDB 启动的时候,all 的PDB 都是mount状态。通过触发器来实现pdbs随cdb自动启动,但这里可以通过触发器来实现PDB的自动open
方式一:
sqlplus / as sysdba
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;
/
方式二:
alter pluggable database itps save state;
需要注意save state需要在pdb open情况下执行才能够生效
SQL> select con_name, state from dba_pdb_saved_states;
CON_NAME
--------------------------------------------------------------------------------
STATE
--------------
itps
OPEN
创建服务
本例通过db_unique_name mycdb 为CDB中的PDB salespdb添加sale服务。
srvctl add service -db mycdb -service sale -pdb salespdb
srvctl add service - db racdb - service sh - pdb pdb1 - preferred racdb1 - available racdb2
srvctl start service - db racdb - service sh
创建表空间、用户
-第1步:创建临时表空间
create temporary tablespace TBS_TEMP
tempfile 'd:\oracle\product\oradata\TBS_TEMP.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
--第2步:创建数据表空间
create tablespace TBS_DATA
logging
datafile 'd:\oracle\product\oradata\TBS_DATA.dbf'
size 50m
autoextend on
next 50m maxsize 20480m
extent management local;
--第3步:创建用户并指定表空间
create user test identified by test
default tablespace TBS_DATA
temporary tablespace TBS_TEMP;
--第4步:给用户授予权限
grant connect,resource,dba to test;
--删除用户
drop user test cascade;
--删除表空间(含物理位置)
drop tablespace TBS_DATA including contents and datafiles;