天天看點

Oracle-快速重建使用者&導入資料

一般情況下,我們将導出的DMP,導入到新庫時,新庫如果已經有相關使用者或資料了,那麼在導入的過程中,會有表或視圖已經存在這樣的提示,而這樣的提示呢,往往在最後導入完成後,會提示為errors。也就是說,關于導入日志,你還是要從頭再看一遍的,以确認是否真的按需求導入成功。而我們重建了使用者的相關資訊後,再導入,相當于是在一張白紙上作畫,當然最後是successfully。當然,如果不重建使用者,也可用參數 table_exists_action 控制。

##鎖定使用者

alter user USERNAME account lock;      

##檢視使用者資源占用

select saddr,sid,serial#,paddr,username,status from v$session where username = 'USERNAME';      

##停止資源占用

alter system kill session 'sid,serial#';      

##批量生成停止資源占用的SQL

select 'alter system kill session'||' '||''''||sid||','||serial#||''''||';' from v$session where username = 'USERNAME';      

##查詢使用者權限

select * from dba_sys_privs where grantee = 'USERNAME';
select * from dba_role_privs where grantee = 'USERNAME';      

##查詢使用者所屬表及表空間

select distinct owner,tablespace_name from dba_tables where owner = 'USERNAME';      

##删除使用者

drop user USERNAME cascade;      

##建立表空間

create tablespace DS datafile '/oracle/app/oracle/oradata/hstadb/DS1.dbf' size 10g;      

##建立使用者并指定預設表空間

create user USERNAME identified by oracle default tablespace DS;      

##使用者授權

grant CONNECT,RESOURCE to USERNAME;
grant CREATE VIEW,UNLIMITED TABLESPACE,CREATE SYNONYM,CREATE TABLE,CREATE DATABASE LINK to USERNAME;
grant DBA,IMP_FULL_DATABASE,EXP_FULL_DATABASE to USERNAME;      

##查詢虛拟目錄位置

set linesize 200;
col DIRECTORY_PATH for a80;
select * from dba_directories;      

##建立虛拟目錄 MYDMP

create directory MYDMP as '/home/oracle';      

##授權虛拟目錄讀寫權限給使用者 USERNAME

grant read,write on directory MYDMP to USERNAME;      

##授權導出導入權限給使用者 USERNAME

grant EXP_FULL_DATABASE,IMP_FULL_DATABASE to USERNAME;      

##導入資料  參數 table_exists_action 不設定

impdp USERNAME/[email protected]:1521/ORCL directory=mydmp dumpfile=orcl_dmp_20221017.dmp logfile=orcl_log_20221017.log remap_schema=USERNAME:USERNAME content=all;      

使用者未鎖定

SQL> drop user USERNAME cascade;

drop user USERNAME cascade