最近看到好多人都在尝试oracle中的12C新特性-容器数据库,今年3月orcle退出了Release2版本,可以算是一个稳定版本了。下午着手尝试了一下,还是蛮不错得
1.前言
CDB与PDB是Oracle 12C引入的新特性,在ORACLE 12C数据库引入的多租用户环境(Multitenant Environment)中,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB)。CDB全称为ContainerDatabase,中文翻译为数据库容器,PDB全称为Pluggable Database,即可插拔数据库。在ORACLE 12C之前,实例与数据库是一对一或多对一关系(RAC):即一个实例只能与一个数据库相关联,数据库可以被多个实例所加载。而实例与数据库不可能是一对多的关系。当进入ORACLE 12C后,实例与数据库可以是一对多的关系。
关于CDB与PDB的关系图
其实大家如果对SQL SERVER比较熟悉的话,这种CDB与PDB是不是感觉和SQL SERVER的单实例多数据库架构是一回事呢。像PDB$SEED可以看成是master、msdb等系统数据库,PDBS可以看成用户创建的数据库。而可插拔的概念与SQL SERVER中的用户数据库的分离、附加其实就是那么一回事。
二、CDB组件(Components of a CDB)
一个CDB数据库容器包含了下面一些组件:
-ROOT组件
ROOT又叫CDB$ROOT, 存储着ORACLE提供的元数据和Common User,元数据的一个例子是ORACLE提供的PL/SQL包的源代码,Common User 是指在每个容器中都存在的用户。
-SEED组件
Seed又叫PDB$SEED,这个是你创建PDBS数据库的模板,你不能在Seed中添加或修改一个对象。一个CDB中有且只能有一个Seed. 这个感念,个人感觉非常类似SQL SERVER中的model数据库。
-PDBS
CDB中可以有一个或多个PDBS,PDBS向后兼容,可以像以前在数据库中那样操作PDBS,这里指大多数常规操作。
这些组件中的每一个都可以被称为一个容器。因此,ROOT(根)是一个容器,Seed(种子)是一个容器,每个PDB是一个容器。每个容器在CDB中都有一个独一无二的的ID和名称。
三、具体操作
1.查看数据库是否为CDB
sqlplus登录数据库
[[email protected] ~]$ sqlplus / as sysdba SQL*Plus: Release 12.2.0.1.0 Production on 星期三 6月 14 15:37:13 2017 Copyright (c) 1982, 2016, Oracle. All rights reserved. 连接到: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production select name,cdb,open_mode,con_id from v$database; [email protected]>select name,cdb,open_mode,con_id from v$database; NAME CDB OPEN_MODE CON_ID --------- --- -------------------- ---------- ORCL YES READ WRITE 0
2.查看当前容器
show con_name
[email protected]>show con_name; CON_NAME ------------------------------ CDB$ROOT
3.创建一个新PDB
create pluggable database pdb1 admin user pdb1 identified by pdb1 file_name_convert=(‘/u01/app/oracle/oradata/orcl/pdbseed/‘,‘/u01/app/oracle/oradata/orcl/pdb1/‘);
4.查看所创建的PDB;
show pdbs;
[email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 ORCLPDB MOUNTED 4 PDB1 MOUNTED 5 PDB2 MOUNTED
5.启动一个创建好的PDB
alter pluggable database pdb1 open;
再次查看
[email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 ORCLPDB MOUNTED 4 PDB1 READ WRITE NO 5 PDB2 READ WRITE NO
6.关闭PDB
[email protected]>alter pluggable database pdb2 close; 插接式数据库已变更。 [email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 ORCLPDB MOUNTED 4 PDB1 READ WRITE NO 5 PDB2 MOUNTED
7.也可以通过sqlplus使用传统的startup和shutdown命令来启动和关闭PDB
[email protected]>alter session set container=pdb2; 会话已更改。 [email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 5 PDB2 MOUNTED [email protected]>startup; 插接式数据库已打开。 [email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 5 PDB2 READ WRITE NO [email protected]>shutdown immediate; 插接式数据库已关闭。 [email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 5 PDB2 MOUNTED [email protected]>alter session set container=CDB$ROOT;
会话已更改。
8.配置监听文件
监听文件目录在$ORACLE_HOME/network/admin
[[email protected] admin]$ cat listener.ora # listener.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/listener.ora # Generated by Oracle configuration tools. SID_LIST_LISTENER = (SID_LIST = (SID_DESC= (GLOBAL_DBNAME = ORCL) (SID_NAME = ORCL) ) (SID_DESC= (GLOBAL_DBNAME = PDB1) (SID_NAME = PDB1) ) ) LISTENER = (DESCRIPTION_LIST = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.221.165)(PORT = 1521)) (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521)) ) ) [[email protected] admin]$ cat tnsnames.ora # tnsnames.ora Network Configuration File: /u01/app/oracle/product/11.2.0/db_1/network/admin/tnsnames.ora # Generated by Oracle configuration tools. LISTENER_ORCL = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.221.165)(PORT = 1521)) ORCL = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.221.165)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = orcl) ) ) PDB1 = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.221.165)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = PDB1) ) )
测试登录
[[email protected] admin]$ tnsping pdb1 TNS Ping Utility for Linux: Version 12.2.0.1.0 - Production on 14-6月 -2017 16:02:05 Copyright (c) 1997, 2016, Oracle. All rights reserved. 已使用的参数文件: /u01/app/oracle/product/11.2.0/db_1/network/admin/sqlnet.ora 已使用 TNSNAMES 适配器来解析别名 尝试连接 (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.221.165)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = PDB1))) OK (10 毫秒) [[email protected] admin]$ sqlplus pdb1/[email protected] SQL*Plus: Release 12.2.0.1.0 Production on 星期三 6月 14 16:02:28 2017 Copyright (c) 1982, 2016, Oracle. All rights reserved. 上次成功登录时间: 星期三 6月 14 2017 15:51:38 +08:00 连接到: Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production [email protected]> select name from v$datafile where con_id=4 ; NAME -------------------------------------------------------------------------------- /u01/app/oracle/oradata/orcl/pdb1/system01.dbf /u01/app/oracle/oradata/orcl/pdb1/sysaux01.dbf /u01/app/oracle/oradata/orcl/pdb1/undotbs01.dbf [email protected]>
9.oracle12C中再启动数据库的时候,PDB并不会随着CDB而启动。不过我们可以通过创建一个触发器让PDB能够随CDB启动。如下:
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; 触发器已创建 SQL> SHUTDOWN IMMEDIATE 数据库已经关闭。 已经卸载数据库。 ORACLE 例程已经关闭。 SQL> STARTUP ORACLE 例程已经启动。 Total System Global Area 754974720 bytes Fixed Size 2928968 bytes Variable Size 524291768 bytes Database Buffers 222298112 bytes Redo Buffers 5455872 bytes 数据库装载完毕。 数据库已经打开。 [email protected]>show pdbs; CON_ID CON_NAME OPEN MODE RESTRICTED ---------- ------------------------------ ---------- ---------- 2 PDB$SEED READ ONLY NO 3 ORCLPDB READ WRITE NO 4 PDB1 READ WRITE NO 5 PDB2 READ WRITE NO