之前写了第一篇Oracle 12c PDB浅析 http://blog.itpub.net/23718752/viewspace-1823792/?
在上次的基础上继续来学习学习。
首先关于多租户的架构设计来说,就好比在一座已经几十年的老房子上动地基一般,这个变化着实够大,如此重大的变化Oracle不遗余力的想引入进来,肯定有更深层次的原因,当然关于这种设计在SQLServer中确实已经早有实现,在Oracle中却被大家相传为一种略带神奇的架构设计。不过话说回来,这个和Oracle坚定不移的云战略是息息相关的。
在这种设计中,PDB作为可插拔的数据库,多个PDB会拥有自己独立的system表空间,temp表空间,但是redo,conrolfile,undo都是共享的。
还是先花点时间是说明一下环境。
SQL> select name, decode(cdb, 'YES', 'Multitenant Option enabled', 'Regular 12c Database: ') "Multitenant Option" , open_mode, con_id from v$database
NAME Multitenant Option OPEN_MODE CON_ID
------------------------------ ------------------------------ ---------- ------
newtest Multitenant Option enabled READ WRITE 0
查看PDB
SQL> select con_id, dbid, guid, name , open_mode from v$pdbs;
CON_ID DBID GUID NAME OPEN_MODE
---------- ---------- -------------------------------- ------------------------------ ----------
2 2188059502 23CD335E7F55687CE0532F857F0A714A PDB$SEED READ ONLY
3 4013429668 23CDBE4149E16F08E0532F857F0A58E3 PDB1 READ WRITE
或者使用快捷方式
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1 READ WRITE NO
然后我们生成控制文件的trace,来看看和早期版本有什么差别。
alter database backup controlefile to trace 之后,在trace目录下得到的文件内容如下:
STARTUP NOMOUNT
CREATE CONTROLFILE REUSE DATABASE "NEWTEST" RESETLOGS NOARCHIVELOG
MAXLOGFILES 16
MAXLOGMEMBERS 3
MAXDATAFILES 1024
MAXINSTANCES 8
MAXLOGHISTORY 292
LOGFILE
GROUP 1 '/U01/app/oracle/oradata/newtest/redo01.log' SIZE 50M BLOCKSIZE 512,
GROUP 2 '/U01/app/oracle/oradata/newtest/redo02.log' SIZE 50M BLOCKSIZE 512,
GROUP 3 '/U01/app/oracle/oradata/newtest/redo03.log' SIZE 50M BLOCKSIZE 512
-- STANDBY LOGFILE
DATAFILE
'/U01/app/oracle/oradata/newtest/system01.dbf',
'/U01/app/oracle/oradata/newtest/sysaux01.dbf',
'/U01/app/oracle/oradata/newtest/undotbs01.dbf',
'/U01/app/oracle/oradata/newtest/pdbseed/system01.dbf',
'/U01/app/oracle/oradata/newtest/users01.dbf',
'/U01/app/oracle/oradata/newtest/pdbseed/sysaux01.dbf',
'/U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/datafile/o1_mf_system_c3px88q0_.dbf',
'/U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/datafile/o1_mf_sysaux_c3px88q1_.dbf',
'/U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/users01.dbf'
CHARACTER SET UTF8
;
从这部分日志可以看出,这个PDB是采用了omf的方式创建的,产生的一个GUID会默认作为对应的PDB目录名称。每个PDB中都有独立的系统表空间system,sysaux
下面是控制文件中的附加日志
-- Commands to re-create incarnation table
-- Below log names MUST be changed to existing filenames on
-- disk. Any one log file from each branch can be used to
-- re-create incarnation records.
-- ALTER DATABASE REGISTER LOGFILE '/U01/app/oracle/fast_recovery_area/NEWTEST/archivelog/2016_03_05/o1_mf_1_1_%u_.arc';
-- ALTER DATABASE REGISTER LOGFILE '/U01/app/oracle/fast_recovery_area/NEWTEST/archivelog/2016_03_05/o1_mf_1_1_%u_.arc';
-- Recovery is required if any of the datafiles are restored backups,
-- or if the last shutdown was not normal or immediate.
RECOVER DATABASE USING BACKUP CONTROLFILE
-- Database can now be opened zeroing the online logs.
ALTER DATABASE OPEN RESETLOGS;
-- Open all the PDBs.
ALTER PLUGGABLE DATABASE ALL OPEN;
-- Commands to add tempfiles to temporary tablespaces.
-- Online tempfiles have complete space information.
-- Other tempfiles may require adjustment.
ALTER TABLESPACE TEMP ADD TEMPFILE '/U01/app/oracle/oradata/newtest/temp01.dbf'
SIZE 206569472 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
ALTER SESSION SET CONTAINER = PDB$SEED;
ALTER TABLESPACE TEMP ADD TEMPFILE '/U01/app/oracle/oradata/newtest/pdbseed/pdbseed_temp012015-11-05_10-47-05-PM.dbf'
SIZE 104857600 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
ALTER SESSION SET CONTAINER = PDB1;
ALTER TABLESPACE TEMP ADD TEMPFILE '/U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/datafile/o1_mf_temp_c3px88q2_.dbf'
SIZE 20971520 REUSE AUTOEXTEND ON NEXT 655360 MAXSIZE 32767M;
ALTER SESSION SET CONTAINER = CDB$ROOT;
-- End of tempfile additions.
可以看到在重建控制文件的时候,会自动创建临时表空间,都是彼此独立的。
如果我们使用sqlplus / as sysdba登录之后,默认接入的就是cdb的环境。
所以我们如果尝试创建一个普通用户,就会抛出下面的问题。
SQL> create user test identified by test
2 ;
create user test identified by test
*
ERROR at line 1:
ORA-65096: invalid common user or role name
这个时候需要用c##的格式来创建用户,这个时候,CDB的用户和PDB的用户的一个最大不同,就是CDB的是一个共有的用户,在PDB中依旧可见,但是在PDB中具体的权限会有所不同。
SQL> create user c##jeanron identified by oracle;
User created.
如果查看角色的信息,会发现存在PDB_DBA,CDB_DBA,但是细看对应的权限,权限也着实少的可怜,和角色DBA完全不在一个级别啊。
SQL> SElect *from dba_sys_privs where grantee='CDB_DBA';
GRANTEE PRIVILEGE ADM COM
------- ---------------------------------------- --- ---
CDB_DBA SET CONTAINER NO YES
可以把这个权限付给c##jeanron
SQL> grant cdb_dba to c##jeanron;
Grant succeeded.
当然使用cdb的用户可以正常连接到实例
conn c##jeanron/oracle
Connected.
SQL> show con_id con_name
CON_ID
------------------------------
1
CON_NAME
------------------------------
CDB$ROOT
我们在cdb的用户下创建一个表作为标记。
SQL> create table cdb_test as select * from cat;
Table created.
SQL> select *from cat;
TABLE_NAME TABLE_TYPE
------------------------------ -----------
CDB_TEST TABLE
然后使用同样的用户登录PDB1,就胡抛出下面的错误。
SQL> conn c##jeanron/oracle@pdb1
ERROR:
ORA-01045: user C##JEANRON lacks CREATE SESSION privilege; logon denied
这个例子就充分说明了,这些权限信息对于PDB而言都是需要重新来初始化的。但是如果有大量的PDB容器,可以指定container的方式。
SQL> conn / as sysdba
Connected.
SQL> alter session set container=pdb1;
Session altered.
SQL> grant connect,resource to c##jeanron container=all ;
然后再次登录pdb,看看刚建的表是否还存在。
SQL> conn c##jeanron/oracle@pdb1;
Connected.
SQL> select *from cat;
no rows selected
通过这个小例子就会明白大体的区别了吧。
最后来删除一下pdb,整个操作类似表空间级的操作。
SQL> alter pluggable database all close;
Pluggable database altered.
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
3 PDB1 MOUNTED
SQL> drop pluggable database pdb1 including datafiles;
Pluggable database dropped.
SQL> show pdbs;
CON_ID CON_NAME OPEN MODE RESTRICTED
---------- ------------------------------ ---------- ----------
2 PDB$SEED READ ONLY NO
查看alert日志会发现,文件是确确实实删掉了。
drop pluggable database pdb1 including datafiles
Sat Mar 05 20:57:56 2016
Deleted file /U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/users01.dbf
Deleted Oracle managed file /U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/datafile/o1_mf_temp_c3px88q2_.dbf
Deleted Oracle managed file /U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/datafile/o1_mf_sysaux_c3px88q1_.dbf
Deleted Oracle managed file /U01/app/oracle/oradata/newtest/NEWTEST/23CDBE4149E16F08E0532F857F0A58E3/datafile/o1_mf_system_c3px88q0_.dbf
Completed: drop pluggable database pdb1 including datafiles