ORACLE_OCP多租户之12C手工建立CDB PDB版本

ORACLE_OCP多租户之12C手工建立CDB PDB版本

一、手工创建CDB

第一步:首先修改oracle环境变量为cdb1:
  • 可以使用以下命令进行修改:
  • 切换到oracle用户中: 后续步骤都在oracle用户中操作。

    su - oracle


[oracle@oracle12-cdb ~]$ vim .bash_profile
[oracle@oracle12-cdb ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export TMP=/tmp
export TMPDIR=$TMP
export ORACLE_SID=cdb1
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/12.2.0/db_1
export INVENTORY_LOCATION=/u01/oraInventory
export TNS_ADMIN=$ORACLE_HOME/network/admin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export NLS_LANG="AMERICAN_AMERICA.AL32UTF8"
export NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
export PATH=$ORACLE_HOME/bin:/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/X11R6/bin:$PATH:$HOME/bin
umask 022

alias sqlplus='rlwrap sqlplus'
alias rman='rlwrap rman'
[oracle@oracle12-cdb ~]$

  • 生成环境变量

    [oracle@oracle12-cdb ~]$ source .bash_profile

第二步:创建密码文件

[oracle@oracle12-cdb dbs]$ orapwd file=$ORACLE_HOME/dbs/orapwcdb1 password=oracle force=y format=12

第三步:创建pfile。使用下面的命令和参数去创建pfile
  • 请确定你已经建立好了一个监听。
  • cd /home/oracle --注意这是LINUX操作系统命令不能在sqlplus下执行。
  • mkdir -p /u01/app/oracle/admin/cdb1/adump --建立cdb1的审计目录
  • mkdir -p /u01/app/oracle/oradata/cdb1 --建立cdb1的数据文件存放目录
  • mkdir -p /u01/app/oracle/oradata/cdb1/pdbseed --创建pdbseed目录
[oracle@oracle12-cdb ~]$ mkdir -p /u01/app/oracle/admin/cdb1/adump
[oracle@oracle12-cdb ~]$ mkdir -p /u01/app/oracle/oradata/cdb1
[oracle@oracle12-cdb ~]$ mkdir -p /u01/app/oracle/oradata/cdb1/pdbseed
[oracle@oracle12-cdb ~]$ vim /home/oracle/init_cdb1.ora
[oracle@oracle12-cdb ~]$ cat /home/oracle/init_cdb1.ora
*.audit_file_dest='/u01/app/oracle/admin/cdb1/adump'
*.audit_trail='db'
*.control_files='/u01/app/oracle/oradata/cdb1/control01.ctl','/u01/app/oracle/oradata/cdb1/control02.ctl'
*.db_block_size=8192
*.db_name='cdb1'
*.diagnostic_dest='/u01/app/oracle'
*.dispatchers='(PROTOCOL=TCP) (SERVICE=cdb1XDB)'
*.enable_pluggable_database=true
*.open_cursors=300
*.pga_aggregate_target=200m
*.processes=300
*.remote_login_passwordfile='EXCLUSIVE'
*.sga_target=800m
*.undo_tablespace='UNDOTBS1'
第四步:使用pfile创建spfile
  • 使用sqlplus / as sysdba进入到oracle数据库,使用我们创建出来的pfile启动数据库到nomount状态,并创建spfile然后重新把数据库启动到startup nomount
    [oracle@oracle12-cdb ~]$ sqlplus / as sysdba
    
    SQL*Plus: Release 12.2.0.1.0 Production on Sat Jan 9 14:26:57 2021
    
    Copyright (c) 1982, 2016, Oracle.  All rights reserved.
    
    Connected to an idle instance.
    
    SQL> startup pfile='/home/oracle/init_cdb1.ora' nomount
    ORACLE instance started.
    
    Total System Global Area  838860800 bytes
    Fixed Size                  8798312 bytes
    Variable Size             343936920 bytes
    Database Buffers          478150656 bytes
    Redo Buffers                7974912 bytes
    SQL> create spfile from pfile='/home/oracle/init_cdb1.ora';
    
    File created.
    
    SQL> shutdown abort;
    ORACLE instance shut down.
    SQL> startup nomount;
    ORACLE instance started.
    
    Total System Global Area  838860800 bytes
    Fixed Size                  8798312 bytes
    Variable Size             343936920 bytes
    Database Buffers          478150656 bytes
    Redo Buffers                7974912 bytes
    SQL>
    
    
第五步:创建一个CDB的建库脚本
  • cd /home/oracle --不是重复了,是我要一直提醒大家,所有的相关文件都必须放到这里。
vim /home/oracle/create_cdb1.sql
CREATE DATABASE cdb1
  USER SYS IDENTIFIED BY oracle
  USER SYSTEM IDENTIFIED BY oracle
  LOGFILE GROUP 1 ('/u01/app/oracle/oradata/cdb1/redo01a.log','/u01/app/oracle/oradata/cdb1/redo01b.log')
             SIZE 100M BLOCKSIZE 512,
          GROUP 2 ('/u01/app/oracle/oradata/cdb1/redo02a.log','/u01/app/oracle/oradata/cdb1/redo02b.log')
             SIZE 100M BLOCKSIZE 512,
          GROUP 3 ('/u01/app/oracle/oradata/cdb1/redo03a.log','/u01/app/oracle/oradata/cdb1/redo03b.log')
             SIZE 100M BLOCKSIZE 512
  MAXLOGHISTORY 1
  MAXLOGFILES 16
  MAXLOGMEMBERS 3
  MAXDATAFILES 1024
  CHARACTER SET AL32UTF8
  NATIONAL CHARACTER SET AL16UTF16
  EXTENT MANAGEMENT LOCAL
  DATAFILE '/u01/app/oracle/oradata/cdb1/system01.dbf'
    SIZE 700M REUSE AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED
  SYSAUX DATAFILE '/u01/app/oracle/oradata/cdb1/sysaux01.dbf'
    SIZE 550M REUSE AUTOEXTEND ON NEXT 10240K MAXSIZE UNLIMITED
  DEFAULT TABLESPACE deftbs
     DATAFILE '/u01/app/oracle/oradata/cdb1/deftbs01.dbf'
     SIZE 500M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
  DEFAULT TEMPORARY TABLESPACE tempts1
     TEMPFILE '/u01/app/oracle/oradata/cdb1/temp01.dbf'
     SIZE 20M REUSE AUTOEXTEND ON NEXT 640K MAXSIZE UNLIMITED
  UNDO TABLESPACE undotbs1
     DATAFILE '/u01/app/oracle/oradata/cdb1/undotbs01.dbf'
     SIZE 200M REUSE AUTOEXTEND ON NEXT 5120K MAXSIZE UNLIMITED
  USER_DATA TABLESPACE usertbs
    DATAFILE '/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf'
    SIZE 50M REUSE AUTOEXTEND ON MAXSIZE UNLIMITED
  ENABLE PLUGGABLE DATABASE
    SEED
    FILE_NAME_CONVERT = ('/u01/app/oracle/oradata/cdb1/', '/u01/app/oracle/oradata/cdb1/pdbseed/')
    SYSTEM DATAFILES SIZE 125M AUTOEXTEND ON NEXT 10M MAXSIZE UNLIMITED
    SYSAUX DATAFILES SIZE 100M
    LOCAL UNDO ON;
  • 在已经启动到nomount状态下的数据库sqlplus工具中执行该脚本。

    SQL> @/home/oracle/create_cdb1.sql
    Database created.
    SQL>

  • 成功了!下一步我们执行一个脚本。不过我们在执行下一个脚本之前,我们需要运行点其他的东西,不然会出现错误。

    export PERL5LIB=$ORACLE_HOME/rdbms/admin:$PERL5LIB
    export PATH=$ORACLE_HOME/bin:$ORACLE_HOME/perl/bin:$PATH
    cd $ORACLE_HOME/perl/lib/5.22.0/x86_64-linux-thread-multi/Hash
    ln -s Util.pm util.pm

  • 运行完成后,我们现在去运行下面的脚本

    @?/rdbms/admin/catcdb.sql

  • 如果遇到让你输入参数的时候 ,输入下面的参数:

    Enter value for 1: /home/oracle
    Enter value for 2: /home/oracle/cdb.log
    Enter new password for SYS: oracle
    Enter new password for SYSTEM: oracle
    Enter temporary tablespace name: temp

  • 当最后输出这些时候代表脚本运行成功:

    catcon: ALL catcon-related output will be written to [/home/oracle/catclust_catcon_39700.lst]
    catcon: See [/home/oracle/catclust*.log] files for output generated by scripts
    catcon: See [/home/oracle/catclust_.lst] files for spool files, if any
    catcon.pl: completed successfully
    catcon: ALL catcon-related output will be written to [/home/oracle/catfinal_catcon_40093.lst]
    catcon: See [/home/oracle/catfinal
    .log] files for output generated by scripts
    catcon: See [/home/oracle/catfinal_.lst] files for spool files, if any
    catcon.pl: completed successfully
    catcon: ALL catcon-related output will be written to [/home/oracle/catbundleapply_catcon_40180.lst]
    catcon: See [/home/oracle/catbundleapply
    .log] files for output generated by scripts
    catcon: See [/home/oracle/catbundleapply_.lst] files for spool files, if any
    catcon.pl: completed successfully
    catcon: ALL catcon-related output will be written to [/home/oracle/utlrp_catcon_40266.lst]
    catcon: See [/home/oracle/utlrp
    .log] files for output generated by scripts
    catcon: See [/home/oracle/utlrp_*.lst] files for spool files, if any
    catcon.pl: completed successfully

第六步:实例的关闭和开启实验
```
SQL> show user;
USER is "SYS"
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area  838860800 bytes
Fixed Size                  8798312 bytes
Variable Size             343936920 bytes
Database Buffers          478150656 bytes
Redo Buffers                7974912 bytes
Database mounted.
Database opened.
SQL> select * from dual;

D
-
X
SQL> set lines 300
SQL> col NAME for a40
SQL> select con_id, dbid, NAME, OPEN_MODE from v$pdbs;

    CON_ID       DBID NAME                                     OPEN_MODE
---------- ---------- ---------------------------------------- ----------
         2 3014806594 PDB$SEED                                 READ ONLY

SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
SQL>

```
可以看到没问题,至此手工创建CDB的所有步骤完成了。

二、手工创建PDB

第一步:利用PDB$SEED创建PDB
  • 在SQLPLUS中执行以下的SQL语句

    CREATE PLUGGABLE DATABASE pdb1 ADMIN USER pdb1admin IDENTIFIED BY oracle
    FILE_NAME_CONVERT=(
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/system01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/system01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/sysaux01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/sysaux01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/undotbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/undotbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/deftbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/deftbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/usertbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/temp01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/temp01.dbf’
    )
    STORAGE UNLIMITED TEMPFILE REUSE;

  • 如果你想对新建成的pdb1进行使用空间限制的话,可以写成以下格式。

    CREATE PLUGGABLE DATABASE pdb1 ADMIN USER pdbadmin IDENTIFIED BY oracle
    FILE_NAME_CONVERT=(
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/system01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/system01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/sysaux01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/sysaux01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/undotbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/undotbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/deftbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/deftbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/usertbs01.dbf’,
    ‘/u01/app/oracle/oradata/cdb1/pdbseed/temp01.dbf’, ‘/u01/app/oracle/oradata/cdb1/pdb1/temp01.dbf’
    )
    STORAGE (
    MAXSIZE 2G
    MAX_SHARED_TEMP_SIZE 200M
    )
    TEMPFILE REUSE;

  • 结果:


SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB1                           MOUNTED
SQL> alter pluggable database pdb1 open;

Pluggable database altered.

SQL> show pdbs;

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 PDB1                           READ WRITE NO
SQL>

三、使用已有的PDB去创建新的PDB

  • 我们需要注意几点:
    • 使用的用户必须拥有’CREATE PLUGGABLE DATABASE’的权限(测试里使用sys用户)
    • 源PDB不可以是关闭状态或者是mount状态的
    • 如果CDB为shared undo,PDB必须为READ-ONLY状态。才能进行相关复制,不然丢失事务。
    • 如果CDB不是归档模式,那么PDB必须为READ-ONLY状态。才能进行相关复制
第一步:创建测试PDB:pdbt
SQL> show con_name;

CON_NAME
------------------------------
CDB$ROOT
SQL> set lines 300
SQL> col NAME for a40
SQL> select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

    CON_ID       DBID NAME                                     OPEN_MODE
---------- ---------- ---------------------------------------- ----------
         2 3014806594 PDB$SEED                                 READ ONLY
         3 2518959561 PDB1                                     READ WRITE

SQL>

CREATE PLUGGABLE DATABASE pdbt ADMIN USER pdbtadmin IDENTIFIED BY oracle
  FILE_NAME_CONVERT=(
    '/u01/app/oracle/oradata/cdb1/pdbseed/system01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/system01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/sysaux01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/sysaux01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/undotbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/undotbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/deftbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/deftbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/usertbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/usertbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbseed/temp01.dbf', '/u01/app/oracle/oradata/cdb1/pdbt/temp01.dbf'
  )
  STORAGE UNLIMITED TEMPFILE REUSE;
  • 尝试把pdbt以READ ONLY方式打开:

    SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY;
    ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY
    *
    ERROR at line 1:
    ORA-65085: cannot open pluggable database in read-only mode

    • 这里会报错,原因是:新的PDB一定要先启动到READ WRITE模式,因为新的PDB状态是NEW,而在NEW的这个模式下时,是不能启动到READ ONLY的,第一次启动到READ WRITE后,将PDB关掉,下次就能启动到READ ONLY状态了。
  • PDB状态检查语句:

set lines 500
col pdb_name for a10
col db_name for a10
col db_unique_name for a10
SQL> col db_version_string for a20
//查询PDB的详细信息:
SQL> select pdb_name,pdb_id,pdb_dbid,pdb_guid,OP_TIMESTAMP,operation,db_version,db_name,db_unique_name,db_dbid,db_version_string from dba_pdb_history order by OP_TIMESTAMP;

PDB_NAME       PDB_ID   PDB_DBID PDB_GUID                         OP_TIMESTAMP        OPERATION        DB_VERSION DB_NAME    DB_UNIQUE_    DB_DBID DB_VERSION_STRING
---------- ---------- ---------- -------------------------------- ------------------- ---------------- ---------- ---------- ---------- ---------- --------------------
PDB1                3 2518959561 B87ED8793A3F9EEBE0538CBEA8C0C689 2021-01-09 15:52:05 CREATE            203424000 CDB1       cdb1       1035107902 12.2.0.1.0
PDBT                4 2174543479 B87F08D83EACA512E0538CBEA8C0D193 2021-01-09 16:05:37 CREATE            203424000 CDB1       cdb1       1035107902 12.2.0.1.0

//查询PDB的状态
SQL> select pdb_name, status from cdb_pdbs;

PDB_NAME   STATUS
---------- ----------
PDB$SEED   NORMAL
PDB1       NORMAL
PDBT       NEW

  • pdb的status有 NEW、NORMAL、UNPLUGGED、NEEDS UPGRADE、CONVERTING、UNUSABLE 几种状态,对这几种状态的解释如下:(文档的最后有英文原版内容)
    • NEW -自创建以来,从未打开过PDB。 必须以READ WRITE模式打开该文件,Oracle才能执行将PDB集成到CDB中并将其标记为NORMAL所需的处理。 如果尝试以只读方式打开PDB,将引发错误。
    • NORMAL -准备使用(准许使用)的PDB。
    • UNPLUGGED -PDB已拔出。可以对其执行的唯一操作是DROP PLUGGABLE DATABASE。
    • NEEDS UPGRADE -PDB需要升级到已插入的CDB的版本。
    • CONVERTING -非CDB已插入CDB,并且正在进行转换,以使其表现得像真实的PDB。
    • UNUSABLE -正在创建PDB或在创建PDB时遇到不可恢复的错误。 当状态设置为“不可用”时,无法打开PDB。 如果PDB在创建过程中由于遇到错误而保持此状态,则只能将其删除。 可以检查告警日志以确定是否存在错误.
第二步:PDB改到READ ONLY状态
  • 首先:启动PDB到READ WRITE状态
SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ WRITE;

Pluggable database altered.

SQL>

  • 关闭:
SQL> ALTER PLUGGABLE DATABASE PDBt close;

Pluggable database altered.

SQL>

  • 然后启动到READ ONLY状态
SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY;

Pluggable database altered.

SQL>

第三步:执行PDB的克隆语句
CREATE PLUGGABLE DATABASE pdb2 FROM pdbt
  STORAGE UNLIMITED TEMPFILE REUSE
  FILE_NAME_CONVERT=(
    '/u01/app/oracle/oradata/cdb1/pdbt/system01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/system01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/sysaux01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/sysaux01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/undotbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/undotbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/deftbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/deftbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/usertbs01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/usertbs01.dbf',
    '/u01/app/oracle/oradata/cdb1/pdbt/temp01.dbf', '/u01/app/oracle/oradata/cdb1/pdb2/temp01.dbf'
  )
  PARALLEL 2;

四、把已拔出的PDB再插回到CDB中

  • 首先关掉PDB

    alter pluggable database pdb2 close;
    Pluggable database altered.
    SQL> alter pluggable database pdb2 unplug into ‘/home/oracle/pdb2.xml’;
    Pluggable database altered.
    SQL>

  • 删除PDB有两种方式 第一个是保留文件第二个是不保留文件

    drop pluggable database pdb2 including datafiles; #删除数据库和文件
    drop pluggable database pdb2 keep datafiles; #删除数据库并保存文件
    #默认状态下是保留文件的。
    drop pluggable database pdb2;

  • 创建PDB

    create pluggable database pdb2 using ‘/home/oracle/pdb2.xml’ nocopy;
    alter pluggable database pdb2 open read wirte ;
    #或
    alter pluggable database pdb2 open;

  • 结果:

SQL> select pdb_name, status from cdb_pdbs;

PDB_NAME   STATUS
---------- ----------
PDB$SEED   NORMAL
PDB1       NORMAL
PDBT       NORMAL
PDB2       NEW

SQL> alter pluggable database pdb2 open;

Pluggable database altered.

SQL> select pdb_name, status from cdb_pdbs;

PDB_NAME   STATUS
---------- ----------
PDB$SEED   NORMAL
PDB1       NORMAL
PDBT       NORMAL
PDB2       NORMAL

SQL>

上一篇:UDP网路编程


下一篇:oracle 12c中种子数据库的隐藏与保护