标签 oracle 19c 下的文章

迁移时在目标库上兼容性检查:
SET SERVEROUTPUT ON
DECLARE
compatible CONSTANT VARCHAR2(3) :=

CASE DBMS_PDB.CHECK_PLUG_COMPATIBILITY(pdb_descr_file=>'PDB1.xml',pdb_name =>'testpdb2')
WHEN TRUE THEN 'YES'  
ELSE 'NO'  

END;
BEGIN
DBMS_OUTPUT.PUT_LINE(compatible);
END;

select con_id, name, type, message, statusfrom PDB_PLUG_IN_VIOLATIONSwhere status<>'RESOLVED' order by name,time;

创建管理用户:

使用SEED创建PDB
CREATE PLUGGABLE DATABASE salespdb
ADMIN USER salesadm IDENTIFIED BY password
ROLES = (dba)
DEFAULT TABLESPACE sales

DATAFILE '/disk1/oracle/dbs/salespdb/sales01.dbf' SIZE 250M AUTOEXTEND ON

FILE_NAME_CONVERT = ('/disk1/oracle/dbs/pdbseed/',

                   '/disk1/oracle/dbs/salespdb/')

STORAGE (MAXSIZE 2G)
PATH_PREFIX = '/disk1/oracle/dbs/salespdb/';

从现有的一个PDB克隆一个新的PDB
REATE PLUGGABLE DATABASE newpdb FROM salespdb
FILE_NAME_CONVERT = ('/disk1/oracle/dbs/salespdb/', '/disk1/oracle/dbs/newpdb/')
PATH_PREFIX = '/disk1/oracle/dbs/newpdb';

把一个PDB放进另外一个CDB
CREATE PLUGGABLE DATABASE salespdb
USING '/disk1/usr/salespdb.xml'
SOURCE_FILE_NAME_CONVERT =

('/disk1/oracle/dbs/salespdb/', '/disk2/oracle/dbs/salespdb/')

NOCOPY
STORAGE (MAXSIZE 2G)
TEMPFILE REUSE;

例子:把一个PDB放进另外一个CDB
有两个实例oracle12c和cdb1,原oracle12c实例有一个pdb2,现在需要把这个pdb2插入到cdb1实例中。
解决方法:
方法1
export ORACLE_SID=oracle12c
sqlplus / as sysdba
alter pluggable database pdb2 open read only;
begin
dbms_pdb.describe
(
pdb_descr_file=>'/u01/pdb2.xml',
pdb_name=>'pdb2'
);
end;
/
不unplug pdb2直接把pdb2 插入到cdb1容器中
export ORACLE_SID=cdb1
sqlplus / as sysdba
create pluggable database salespdb using '/u01/pdb2.xml'
copy
file_name_convert=('/u01/app/oracle/oradata/pdb2/','/u01/app/oracle/oradata/cdb1/pdb2/');
alter pluggable database salespdb open;

方法2
也可以通过把pdb2先unplug 然后在插入到cdb1容器中如下
export ORACLE_SID=oracle12c
sqlplus / as sysdba
alter pluggable database pdb2 close immediate;
alter pluggable database pdb2 unplug into '/u01/pdb2.xml';
unplug之后的pdb是不能打开的,把unplug的pdb2插入到cdb1中
加上AS CLONE字句可以确保新的pdb有唯一的标识
create pluggable database salespdb as clone using '/u01/pdb2.xml'
copy
file_name_convert=('/u01/app/oracle/oradata/pdb2/','/u01/app/oracle/oradata/cdb1/pdb2/');

方法3
通过dblink方式进行源库到目标库的pdb复制迁移
源库
export ORACLE_SID=oracle12c
sqlplus / as sysdba
alter session set container=pdb1;
create user createdb identified by createdb;
grant create session,create pluggable database to creatadb;
目标库
cd $ORACLE_HOME/network/admin
vi tnsnames.ora
PDB1 =
(DESCRIPTION =

(ADDRESS_LIST =
  (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.1)(PORT = 1521))
)
(CONNECT_DATA =
  (SERVER = DEDICATED)
  (SERVICE_NAME = pdb1)
)

)
export ORACLE_SID=cdb1
sqlplus / as sysdba
create public database link node2_node1 connect to createdb identified by createdb
using 'pdb1';
create pluggable database pdb1 from pdb1@node2_node1
file_name_convert=('/u01/app/oracle/oradata/oracle12c/pdb1/','/u01/app/oracle/oradata/cdb1/pdb1/');

show con_name

查看数据库是否为CDB
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
--------- ----------------------------- -------------------- ----------
EPPS Multitenant Option enabled READ WRITE 0
YES表示该数据库是CDB,如果是NO表示是NO-CDB(普通数据库)

查看容器:
show con_name
select name from v$containers ;
SELECT SYS_CONTEXT('USERENV','CON_NAME') FROM dual;
show pdbs ;

确认当前是cdb还是no-cdb ?
SQL> select name,cdb from v$database;
NAME CDB
--------- ---
ZARADB YES

查询pdb
col pdb_name for a30
select pdb_id,pdb_name,dbid,status,creation_scn from dba_pdbs;
select con_id,dbid,NAME,OPEN_MODE from v$pdbs;

PDB_ID PDB_NAME                             DBID STATUS        CREATION_SCN

---------- ------------------------------ ---------- ------------- ------------

     3 PDB1                           3337485269 NORMAL             1909544
     2 PDB$SEED                       4072348290 NORMAL             1720741

SQL>show pdbs

CON_ID       DBID NAME                           OPEN_MODE

---------- ---------- ------------------------------ ----------

     2 4072348290 PDB$SEED                       READ ONLY
     3 3337485269 PDB1                           READ WRITE

直接创建pdb
CREATE PLUGGABLE DATABASE pdb2
ADMIN USER pdb2dba IDENTIFIED BY anbaisheng
STORAGE (MAXSIZE 2G MAX_SHARED_TEMP_SIZE 100M)
DEFAULT TABLESPACE tbs_pdb2 DATAFILE '/opt/oracle/oradata/ZARADB/datafile/pdb2_001.dbf' SIZE 25M AUTOEXTEND ON;

模板创建pdb
以现有pdb为模板创建,现有pdb必须在read only模式下才能被创建,否则报错
以pdb1为模板创建pdb2
drop pluggable database pdb2 including datafiles;
alter pluggable database pdb1 close;
alter pluggable database pdb1 open read only;
create pluggable database pdb2
from pdb1
file_name_convert =('/opt/oracle/oradata/ZARADB/E98BEB85B1A80C56E0439A02A8C05841/datafile/','/opt/oracle/oradata/ZARADB/datafile/pdb2');

打开、关闭pdb
alter pluggable database pdb2 close;
alter pluggable database all close;
alter pluggable database pdb2 close immediate;
alter pluggable database pdb2 open;
alter pluggable database all open;

删除pdb,与删除表空间类似,可以级联删除数据文件
drop pluggable database pdb2;
drop pluggable database pdb2 including datafiles;

unplug后pdb只能mount不能open
alter pluggable database pdb2 unplug into '/tmp/pdb2.xml';

unplug后删除pdb,再使用xml文件加回pdb
drop pluggable database pdb2;
create pluggable database pdb2 using '/tmp/pdb2.xml' nocopy;

在pdb管理用户权限:
alter session set container=PDBORCL;
GRANT CONNECT,RESOURCE,CREATE VIEW TO COMPANY IDENTIFIED BY <password> ;
grant create any directory to company;
ALTER USER companycompany QUOTA UNLIMITED ON sales;
GRANT RESOURCE TO company;
ALTER USER COMPANY DEFAULT TABLESPACE company_data TEMPORARY TABLESPACE company_temp;

连接到CDB
[oracle@Ora12c /]$ sqlplus / as sysdba
--查看CDB中可用的service:
SQL> SELECT name,pdb FROM v$services ORDER BY name;

--通过lsnrctl 也可以判断:
oracle@node1:/home/db/oracle$ lsnrctl status
....
(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=node1)(PORT=1521)))
(DESCRIPTION=(ADDRESS=(PROTOCOL=ipc)(KEY=EXTPROC1521)))
Services Summary...
Service "86b637b62fdf7a65e053f706e80a27ca" has 1 instance(s).
Instance "testdb", status READY, has 1 handler(s) for this service...
Service "a2f51f35f5e34975e0536542a8c0adea" has 1 instance(s).
Instance "testdb", status READY, has 1 handler(s) for this service...
Service "testdb" has 1 instance(s).
Instance "testdb", status READY, has 1 handler(s) for this service...
Service "testdbXDB" has 1 instance(s).
Instance "testdb", status READY, has 1 handler(s) for this service...
Service "testpdb" has 1 instance(s).
Instance "testdb", status READY, has 1 handler(s) for this service...
The command completed successfully
[oracle@Ora12c /]$

通过这些service,就可以远程连接CDB。
--EZCONNECT
oracle@node1:/home/db/oracle$ sqlplus system/oracle@127.0.0.1:1521/testdb
oracle@node1:/home/db/oracle$ sqlplus system/oracle@127.0.0.1:1521/testpdb

--通过TNSNAMES.ORA连接:
在tnsnames.ora 中配置如下:
testdb=
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.10)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = testdb)
)
)

--连接:
oracle@node1:/home/db/oracle$ sqlplus system/oracle@testdb
SQL> show con_name

CON_NAME

CDB$ROOT

pdb切换
SQL> select con_id,name from v$pdbs ;

SQL> show con_name
SQL> SELECT SYS_CONTEXT('USERENV','CON_NAME') FROM dual;

SQL> show con_name
SQL> alter session set container=testpdb;
Session altered

SQL> show con_name ;
SQL> SELECT SYS_CONTEXT('USERENV','CON_NAME') FROM dual;

SYS_CONTEXT('USERENV','CON_NAM

TESTPDB
SQL>
SQL> alter session set container=CDB$ROOT ;
SQL> show con_name ;
SQL> SELECT SYS_CONTEXT('USERENV','CON_NAME') FROM dual;

SYS_CONTEXT('USERENV','CON_NAM

CDB$ROOT

手动隔离-为SQL语句的执行计划创建隔离配置

SQL只要执行计划不变,每次都会被直接终止

使用以下任一包函数为SQL语句的执行计划创建隔离配置:DBMS_SQLQ.CREATE_QUARANTINE_BY_SQL_IDDBMS_SQLQ .CREATE_QUARANTINE_BY_SQL_TEX

DECLARE
quarantine_config VARCHAR2(30);
BEGIN
quarantine_config:= DBMS_SQLQ.CREATE_QUARANTINE_BY_SQL_ID( SQL_ID => ’8vu7s907prbgr’, PLAN_HASH_VALUE => '3488063716');
END;
/

DECLARE
quarantine_config VARCHAR2(30);
BEGIN

quarantine_config:= DBMS_SQLQ.CREATE_QUARANTINE_BY_SQL_ID( SQL_ID => '152sukb473gsk'); 

END;
/
DECLARE

quarantine_config VARCHAR2(30); 

BEGIN
quarantine_config:= DBMS_SQLQ.CREATE_QUARANTINE_BY_SQL_TEXT( SQL_TEXT => to_clob('select count(*) from emp'));
END;

手动隔离-在隔离配置中指定隔离阈值
可指定如下隔离阈值:
-CPU time
-Elapsed time
-I/O in megabytes
-Number of physical I/O requests
-Number of logical I/O requests

BEGIN
DBMS_SQLQ.ALTER_QUARANTINE(

       QUARANTINE_NAME => 'SQL_QUARANTINE_3z0mwuq3aqsm8cfe7a0e4', 
       PARAMETER_NAME => 'CPU_TIME', 
       PARAMETER_VALUE => '5'); 

DBMS_SQLQ.ALTER_QUARANTINE(

      QUARANTINE_NAME => 'SQL_QUARANTINE_3z0mwuq3aqsm8cfe7a0e4', 
       PARAMETER_NAME => 'ELAPSED_TIME', PARAMETER_VALUE => '10'); 

END;