一、多租户框架
多租户技术(英语:multi-tenancy technology)或称多重租赁技术,是一种软件架构技术,它是在探讨与实现如何于多用户的环境下共用相同的系统或程序组件,并且仍可确保各用户间数据的隔离性。
1.1CDB与PDB
多租户技术(英语:multi-tenancy technology)或称多重租赁技术,是一种软件架构技术,它是在探讨与实现如何于多用户的环境下共用相同的系统或程序组件,并且仍可确保各用户间数据的隔离性。
Oracle 12c Multitenant Architecture多租户框架最重要的2个概念是容器数据库(CDB,multitenant container database)和可热插拔数据库(pluggable databases,PDB)。可以把CDB想象成一个大的容器,这个大的容器在物理上是一个整体,在这个大的容器中还有一些小的容器PDB。容器的目的是将Oracle固有元数据和用户数据(包括用户元数据)隔离。
The multitenant architecture enables an Oracle database to function as a multitenant container database (CDB) that includes zero, one, or many customer-created pluggable databases (PDBs). A PDB is a portable collection of schemas, schema objects, and nonschema objects that appears to an Oracle Net client as a non-CDB.
CDB(容器数据库) :
其实就是我们以前的数据库,只是它被分成了几部分,每一部分(即PDB)都能作为一个整体(数据库)对外独立提供服务,就像多个以前传统的非容器数据库。
PDB(可插拔式数据库):
一系列Schema的集合,从用户和应用看来是一个逻辑上独立的数据库。但是在物理角度上,实例和所有数据库文件都是属于容器数据库(CDB)的。
Container Database (CDB) :
On the surface this seems very similar to a conventional Oracle database, as it contains most of the working parts you will be already familiar with (controlfiles, datafiles, undo, tempfiles, redo logs etc.). It also houses the data dictionary for those objects that are owned by the root container and those that are visible to all PDBs.
Pluggable Database (PDB) :
Since the CDB contains most of the working parts for the database, the PDB only needs to contain information specific to itself. It does not need to worry about controlfiles, redo logs and undo etc. Instead it is just made up of datafiles and tempfiles to handle it's own objects. This includes it's own data dictionary, containing information about only those objects that are specific to the PDB.
1.2和传统非容器数据库的对比
使用
CREATE DATABASE ... ENABLE PLUGGABLE DATABASE
可以创建一个新的CDB
. 如果不带 ENABLE PLUGGABLE DATABASE
, 则新创建的数据库为非容器数据库(non-CDB ),不能包含PDB。我们将容器数据库和传统的非容器数据库放在同一个server上比对,很容易概括出Oracle公司即将推出的Oracle 12c容器数据库和可插拔式数据库的基本架构。
从文件角度:
图示PDBA、PDBB、PDBC、CDB1和NDB。其中PDBA、PDBB、PDBC均属于容器数据库CDB1的可插拔式数据库,NDB则为传统的非容器数据库。所以也可以这样描述:只有两个数据库CDB1和NDB保存在存储设备上。
从实例角度:
在图示服务器节点上,运行着两个实例,对应的数据库分别是CDB1和NDB。可以清楚地看到只有容器数据库和非容器数据库才有对应的实例,可插拔式数据库PDBA、PDBB和PDBC共用容器数据库的实例,并没有自身对应的实例。
从服务角度:
传统的非容器数据库可以通过实例名或服务名链接,但是可插拔式数据库只能通过服务名链接。至于容器数据库,就像一个非容器数据库一样,同样可以通过实例名或服务名链接。
二、CDB中的container
2.1三种容器类型
一个容器数据库CDB含有3种类型的容器(如下图所示)
1、 ROOT容器,
CDB$ROOT
,有且只有一个。包含元数据信息和Common User(在各个容器内都有效的用户,比如sys),
2、 SEED容器, PDB$SEED
,有且只有一个。它就是一个模板,新的PDB可以基于这个模板进行创建
3、 PDB容器, 0~n个,也是最重要的,包含用户数据,我们的创建的表就放在这里。
Exactly one root
The root stores Oracle-supplied metadata and common users. An example of metadata is the source code for Oracle-supplied PL/SQL packages (see "Data Dictionary Architecture in a CDB"). A common user is a database user known in every container (see "Common Users in a CDB"). The root container is named
CDB$ROOT
.Exactly one seed PDB
The seed PDB is a system-supplied template that the CDB can use to create new PDBs. The seed PDB is named
PDB$SEED
. You cannot add or modify objects inPDB$SEED
.Since the bulk of the working parts are already present in the root container, creating a new PDB is a comparatively quick and simple task. When creating a completely new PDP, the PDB is created as a copy of a seed PDB, so it only takes as long as the files take to copy.Zero or more user-created PDBs
A PDB is a user-created entity that contains the data and code required for a specific set of features. For example, a PDB can support a specific application, such as a human resources or sales application. No PDBs exist at creation of the CDB. You add PDBs based on your business requirements.
2.2三种容器类型的进一步解释:
Seed是生成PDB的模板。可以通过直接复制Seed快速创建PDB
每个容器里都包含一个SYSTEM表空间保存数据字典信息:
1. 在Root容器中的SYSTEM数据字典里只保存Oracle固有元数据(只针对Oracle提供的对象,比如DBMS_%的包等等)。
2. 在每个可插拔式数据库(容器)中的SYSTEM数据字典只保存用户在可插拔式数据库创建的对象的元数据。
如图所示,EMP和DEPT表的数据字典信息只保存在可插拔式数据库的OBJ$和TAB$里,Root容器中的OBJ$和TAB$里并没有EMP和DEPT表的相关信息。不管一个容器数据库里有多少个可插拔式数据库,物理上的数据库只有一个,但是它的SYSTEM表空间确有许多个,数量等于可插拔式数据库的数量加一(Root容器的SYSTEM表空间)。
如上图,逻辑层,包含一个Root,一个Seed,两个PDB.不同的应用使用不同的PDB.不同的PDB 管理员管理各自的PDB.而整个CDB的Common User(比如sys)可以管理整个CDB包含的所有的Container。而在物理层,只对应一个数据库实例。
三、多租户(Multitenant)的实现对比
3.1.独立数据库
这是第一种方案,即一个租户一个数据库,这种方案的用户数据隔离级别最高,安全性最好,但成本也高。
优点:
为不同的租户提供独立的数据库,有助于简化数据模型的扩展设计,满足不同租户的独特需求;如果出现故障,恢复数据比较简单。
缺点:
一个客户、一套数据、一套部署。增大了数据库的安装数量,随之带来维护成本和购置成本的增加。如果面对的是银行、医院等需要非常高数据隔离级别的租户,可以选择这种模式,否则这种方案一般来说是无法承受的。
3.2. 共享数据库、独立 Schema
这是第二种方案,即多个或所有租户共享Database,但一个Tenant一个Schema。(下图的上半部分)
优点:
为安全性要求较高的租户提供了一定程度的逻辑数据隔离,并不是完全隔离;每个数据库可以支持更多的租户数量。
缺点:
如果出现故障,数据恢复比较困难,因为恢复数据库将牵扯到其他租户的数据;如果需要跨租户统计数据,存在一定困难。管理、安全性、数据迁移都有困难。
因此就发生了如下的变化
3.3不同的PDB
好处:
- 整合多个小系统的数据库
- 统一管理,物理上共用REDO和UNDO;
- 节省成本
- 数据便捷移动
- 易于迁移
- 易于应用测试
- 便于物理数据库的管理和监控
- PDB级别的数据恢复、隔离
- 减少数据库管理任务
- Oracle Database Resource Manager
- 统一备份、统一升级
- 职权分离
- 最适合大的数据中心项目
- Cost reduction
By consolidating hardware and sharing database memory and files, you reduce costs for hardware, storage, availability, and labor. For example, 100 PDBs on a single server share one database instance and one set of database files, thereby requiring less hardware and fewer personnel.
- Easier and more rapid movement of data and code
By design, you can quickly plug a PDB into a CDB, unplug the PDB from the CDB, and then plug this PDB into a different CDB. The implementation technique for plugging and unplugging is similar to the transportable tablespace technique.
- Easier management and monitoring of the physical database
The CDB administrator can attend to one physical database (one set of files and one set of database instances) rather than split attention among dozens or hundreds of non-CDBs. Backup strategies and disaster recovery are simplified.
- Separation of data and code
Although consolidated into a single physical database, PDBs mimic the behavior of non-CDBs. For example, if user error loses critical data, a PDB administrator can use Oracle Flashback or point-in-time recovery to retrieve the lost data without affecting other PDBs.
- Secure separation of administrative duties
A user account is common, which means that it can connect to any container on which it has privileges, or local, which means that it is restricted to a specific PDB. A CDB administrator can use a common user account to manage the CDB. A PDB administrator uses a local account to manage an individual PDB. Because a privilege is contained within the container in which it is granted, a local user on one PDB does not have privileges on other PDBs within the same CDB.
- Ease of performance tuning
It is easier to collect performance metrics for a single database than for multiple databases. It is easier to size one SGA than 100 SGAs.
- Support for Oracle Database Resource Manager
In a multitenant environment, one concern is contention for system resources among the PDBs running on the same computer. Another concern is limiting resource usage for more consistent, predictable performance. To address such resource contention, usage, and monitoring issues, you can use Oracle Database Resource Manager (see "Database Resource Manager").
- Fewer database patches and upgrades
It is easier to apply a patch to one database than to 100 databases, and to upgrade one database than to upgrade 100 databases.
The multitenant architecture has benefits beyond database consolidation. These benefits derive from storing the data and data dictionary metadata specific to a PDB in the PDB itself rather than storing all dictionary metadata in one place. By storing its own dictionary metadata, a PDB becomes easier to manage as a distinct unit, even when only one PDB resides in a CDB.
Benefits of data dictionary separation include the following:
- Easier migration of data and code
For example, instead of upgrading a CDB from one database release to another, you can unplug a PDB from the existing CDB, and then plug it into a newly created CDB from a higher release.
- Easier testing of applications
You can develop an application on a test PDB and, when it is ready for deployment, plug this PDB into the production CDB.
四、举例
图片上展示的是一个容器数据库,其内包含4个容器:Root容器和三个可插拔式数据库。不同应用App1、App2和App3使用不同的不同PDB,每个可插拔式数据库为特定应用提供数据。通过将非容器数据库作为可插拔式数据库“插入”容器数据库,很容易实现数据集中,实现对多个数据库的合一管理,从而提高服务器的资源利用效率。三个应用所使用的数据库它们既可以被三个不同的DBA管理也能够由一个容器数据库DBA统一管理,即用户SYS。用户SYS在这种架构中是典型的“通用”用户,SYS可以登录在全部4个容器上,并且具备SYSDBA权限。经过有效的整合,这样可以减少成本、降低管理的复杂度。
在一个容器数据库内的可插拔式数据库共享后台进程、SGA和数据字典。容器数据库避免了以下结构不必要的冗余: 后台进程、内存、数据字典内的元数据
同时App1、App2和App3具备各自的数据文件。不同应用对于不同PDB数据库内容进行分别管理,可插拔式数据库也拥有各自独立的数据字典以及各自独立的用户(应用)数据,又保障了应用之间所需的独立性与安全性。
参考:
Introduction to the Multitenant Architecture
verview of Container Databases (CDB) and Pluggable Databases (PDB)