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
[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'
[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>
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>
```
在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>
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状态检查语句:
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
SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ WRITE;
Pluggable database altered.
SQL>
SQL> ALTER PLUGGABLE DATABASE PDBt close;
Pluggable database altered.
SQL>
SQL> ALTER PLUGGABLE DATABASE PDBt OPEN READ ONLY;
Pluggable database altered.
SQL>
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
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>