CDB、PDB概念介绍
CDB与PDB是Oracle 12C引入的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。CDB全称为Container Database,中文翻译为数据库容器,PDB全称为Pluggable Database,即可插拔数据库。在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。而实例与数据库不可能是一对多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。下面为CDB与PDB的关系图:
一个CDB容器数据库通常包含CDB$ROOT、PDB$SEED组件:
a. CDB$ROOT存储着ORACLE提供的元数据和Common User,Common User 是指在每个容器中都存在的用户,查看:
show con_name;
b.PDB$SEED是创建PDB数据库的模板。一个CDB中有且只能有一个Seed,查看:
show pdbs
c.PDB可插拔数据库,CDB中可以有一个或多个PDB,查看:
show pdbs
创建CDB容器数据库
dbca在创建数据库的时候,可以选择是创建容器数据库还是传统的数据库,如下图选择为创建CDB:
创建pdb:
a.命令create pluggable database创建:
create pluggable database app1 admin user pdba identified by pdba123;
指定路径创建pdb:
mkdir -p /u01/app/oracle/oradata/prod/data
create pluggable database app1 admin user pdba identified by pdba123 FILE_NAME_CONVERT=('/u01/app/oracle/oradata/prod/pdbseed','/u01/app/oracle/oradata/prod/data');
b.dbca创建pdb:
确认:
启动pdb:
alter pluggable database APP1 open;
c. 克隆pdb数据库:
alter system set db_create_file_dest='/u01/app/oracle/oradata/prod/';
create pluggable database app3 from app2;
删除pdb
先关闭对应的pdb,然后删除pdb:
alter pluggable database app3 close immediate;
drop pluggable database app3 including datafiles;
只是删除库,不删除文件:
drop pluggable database app3 keep datafiles;
alter pluggable database app3 unplug into '/home/oracle/app3.xml';
drop pluggable database app3 keep datafiles;
show pdbs
create pluggable database app5 using '/home/oracle/app3.xml' nocopy;
切换容器
切换到app1 pdb下面:
alter session set container=app1;
alter session set container=CDB$ROOT;
查看当前属于哪个容器:
show con_name
select sys_context('USERENV','CON_NAME') from dual;
启动、关闭pdb
a. 指定pdb进行关闭和启动:
alter pluggable database app1 open;
startup pluggable database app1;
启动到只读模式,新建的pdb必须启动一次后才可以设置为read only,否则报
ERROR at line 1:
ORA-65085: cannot open pluggable database in read-only mode:
可以从dba_pdbs视图进行确认,对于NEW的pdb,需要先open:
SQL> col pdb_name for a10
SQL> select pdb_name,status from dba_pdbs;
PDB_NAME STATUS
---------- ----------
APP1 NORMAL
PDB$SEED NORMAL
APP2 NORMAL
APP3 NORMAL
APP4 NEW
新建一个app3 数据库,然后执行启动到read only:
create pluggable database app3 from app2;
alter pluggable database app3 open read only;
alter pluggable database app3 open read only;
show pdbs;
alter pluggable database app1 close immediate;
b.切换到对应的pdb进行关闭启动:
alter session set container= app1;
startup;
alter session set container= app1;
shutdown immediate;
c.集中操作pdb:
开启所有pdb:
alter pluggable database all open;
关闭所有pdb:
alter pluggable database all close immediate;
pdb自动启动:
oracle 12.1和oracle12.2版本,默认情况下PDB不会随着CDB启动而启动,oracle 12.1只能通过触发器实现:
CREATE TRIGGER open_all_pdbs
AFTER STARTUP ON DATABASE
BEGIN
EXECUTE IMMEDIATE 'ALTER PLUGGABLE DATABASE ALL OPEN';
END ;
/
删除触发器:
drop trigger open_all_pdbs;
oracle12.2之后可以使用save state来保存pdb数据库的当前状态。
show pdbs;
alter pluggable database app1 save state;
startup force;
show pdbs;
查看state状态:
col con_name for a10
select con_name, state from dba_pdb_saved_states;
删除discard state状态:
alter pluggable database app1 discard state;
查看pdb信息
查看所有pdb:
show pdbs
col name for a20
select con_id,dbid,name,open_mode from v$pdbs;
select name,cause ,type,message,status from pdb_plug_in_violations;