Oracle遷移記錄
背景說(shuō)明
自建機(jī)房中的所有服務(wù)全部遷移至阿里云服務(wù)器,重中之重-Oracle數(shù)據(jù)庫(kù)遷移疚宇,包括之前所有老數(shù)據(jù)都需要遷移到阿里云服務(wù)器中卢厂。本文假設(shè)阿里云服務(wù)器已成功安裝Oracle進(jìn)行遷移說(shuō)明。
準(zhǔn)備工作
登錄舊數(shù)據(jù)庫(kù)服務(wù)器施戴,以下命令供參考~
導(dǎo)出目錄
這里所有操作都在舊Oracle數(shù)據(jù)庫(kù)服務(wù)器上進(jìn)行,建立directory目的用于舊數(shù)據(jù)庫(kù)資料導(dǎo)出萌丈,oracle導(dǎo)出必須先行建立directory暇韧,相當(dāng)于為物理路徑取一個(gè)別名。
切換到Oracle用戶(hù)下(包含環(huán)境變量)
su – oracle
創(chuàng)建目錄
mkdir -pv /zone/bak
owner浓瞪,group設(shè)為Oracle
chown -R oracle:oinstall /zone/bak/
進(jìn)入SQLPLUS控制臺(tái)
sqlplus /nolog
以dba身份登錄SQLPLUS控制臺(tái)
conn / as sysdba
conn system/hanley //功能同上
創(chuàng)建目錄,存?zhèn)浞菸募覆#毐WC/zone/bak在OS中物理路徑存在
create or replace directory db_bak as ‘/zone/bak
目錄授權(quán)為讀寫(xiě)
grant read,write on directory db_bak to public
創(chuàng)建用戶(hù)
當(dāng)前操作均在阿里云新數(shù)據(jù)庫(kù)服務(wù)器中進(jìn)行,若源數(shù)據(jù)包括多用戶(hù),需按如下步驟建立所有用戶(hù)及對(duì)應(yīng)表空間涂乌,為方便后續(xù)導(dǎo)入艺栈,新庫(kù)資料建立完全比照舊庫(kù)進(jìn)行,盡量保持一致湾盒。
創(chuàng)建表空間
create tablespace mtbs_01 logging datafile '/zone/oracle_bak/mtbs_data_01' size 10G autoextend on next 10m maxsize 20G extent management local;
創(chuàng)建臨時(shí)表空間
create temporary tablespace mtbs_temp tempfile '/zone/oracle_bak/temp0.dbf' size 50m autoextend on next 50m maxsize 1G extent management local;
創(chuàng)建用戶(hù)并指定表空間
create user hanley identified by 123456 default tablespace mtbs_01 temporary tablespace mtbs_temp;
為新用戶(hù)授權(quán)
grant connect,resource,dba to hanley;
表空間新增數(shù)據(jù)文件湿右,若源數(shù)據(jù)大于40G則需為表空間建立多個(gè)數(shù)據(jù)文件
alter tablespace mtbs_01 add datafile '/zone/oracle_bak/mtbs_data_02' size 1G autoextend on next 100m maxsize 2G;
修改表空間.數(shù)據(jù)文件大小,注意原則上設(shè)置的值要比設(shè)置前的值要大
alter database datafile '/zone/oracle_bak/mtbs_data_02' resize 2g;
導(dǎo)入目錄
參考“導(dǎo)出目錄”罚勾,在新服務(wù)器.阿里云上建立directory(為遷移方便毅人, directory最好與“導(dǎo)出目錄”保持一致,但不是必須一定要這么做)尖殃,后續(xù)自舊數(shù)據(jù)庫(kù)下載的數(shù)據(jù)文件將上傳到此目錄丈莺,以便進(jìn)行數(shù)據(jù)遷移(導(dǎo)入新數(shù)據(jù)庫(kù))。
數(shù)據(jù)遷移
開(kāi)始導(dǎo)出
這里所有操作均在舊數(shù)據(jù)庫(kù)服務(wù)器上進(jìn)行送丰,需要注意expdp 并不是SQL 命令缔俄,它屬于$Oracle_home/bin 下的命令,建議在oracle賬戶(hù)下進(jìn)行器躏。
按用戶(hù)導(dǎo)出所有對(duì)象數(shù)據(jù)俐载,包括表及其表數(shù)據(jù)
數(shù)據(jù)量比較大時(shí),可指定壓縮參數(shù)COMPRESSION=(ALL,DATA_ONLY)
expdp who/enjarwhodb@testdb schemas=who dumpfile=who.dmp directory=db_bak
導(dǎo)出整個(gè)數(shù)據(jù)庫(kù)實(shí)例登失,需要system或擁有dba權(quán)限的賬戶(hù)遏佣,一般不建議全部導(dǎo)出
expdp system/hanley@testdb directory=db_bak dumpfile=full.dmp full=y;
按用戶(hù)導(dǎo)出并排除部分表
expdp hub/hub123qwe@testdb directory=db_bak dumpfile=demo.dmp schemas=hub exclude=table:"in('HUB_SMS_LOG')" logfile=demo.log owner=hub
開(kāi)始導(dǎo)入
這里所有操作均在新數(shù)據(jù)庫(kù)服務(wù)器上進(jìn)行,開(kāi)始之前需將上一步導(dǎo)出的文件上傳到當(dāng)前新數(shù)據(jù)庫(kù)服務(wù)器上揽浙,并確保新數(shù)據(jù)庫(kù)上表空間及對(duì)應(yīng)用戶(hù)已存在贼急。
按用戶(hù)導(dǎo)入
impdp hub/hanley@who directory=db_bak dumpfile=hub.dmp logfile=x.log
全庫(kù)導(dǎo)入,不推薦
impdb system/CI123who@testdb directory=dump_dir dumpfile=full.dmp full=y;
源庫(kù)與目標(biāo)庫(kù)instance不一致捏萍,scheam不一致情況
impdp hub/hanley@who directory=db_bak dumpfile=hub.dmp logfile=x.log
remap_schema=源用戶(hù)名:目標(biāo)用戶(hù)名 remap_tablespace=源表空間:目標(biāo)表空間
導(dǎo)入指定表表空間
impdp system/manager@orcl directory=dump_dir dumpfile=tablespace.dmp tablespaces=example;
幫助命令
表空間相關(guān)
按用戶(hù)查詢(xún)表空間
select username,default_tablespace,temporary_tablespace from dba_users where username='WHO';
查詢(xún)表空間-數(shù)據(jù)文件
select tablespace_name,file_id,bytes/1024/1024 as file_size,file_name from dba_data_files
order by file_id;
查詢(xún)臨時(shí)表空間
select tablespace_name,tablespace_size,allocated_space,free_space/1024/1024 as "free size(m)" from dba_temp_free_space;
EXPDP,IMPDP參數(shù)
https://www.cnblogs.com/champaign/p/7681288.html
連接相關(guān)
不連接數(shù)據(jù)庫(kù)instance以sysdba進(jìn)入控制臺(tái)
sqlplus / as sysdba
無(wú)日志方式連接sqlplus,且不連接Instance,進(jìn)入后使用conn連接
sqlplus /nolog
在sqlplus中使用conn連接數(shù)據(jù)庫(kù)
conn username/password
conn username/password@localhost/orcl
啟動(dòng)相關(guān)
oracle身份登入數(shù)據(jù)庫(kù)服務(wù)器
su – oracle
數(shù)據(jù)庫(kù)監(jiān)聽(tīng)[查看空闲、啟動(dòng)令杈、停止]
lsnrctl stat|start|stop
instance啟動(dòng)
conn /as sysdba
startup
instance關(guān)閉
shutdown immediate
進(jìn)程會(huì)話相關(guān)
數(shù)據(jù)庫(kù)目前的進(jìn)程數(shù)
select count(*) from v$process;
進(jìn)程數(shù)上限
select value from v$parameter where name = 'processes';
數(shù)據(jù)庫(kù)目前的會(huì)話數(shù)
select count(*) from v$session;
修改processes和sessions值
alter system set processes=1000 scope=spfile;
alter system set sessions=1000 scope=spfile;
重置用戶(hù)密碼
進(jìn)入sqlplus控制臺(tái)修改密碼
sqlplus /nolog
connect / as sysdba
alter user system identified by 新密碼;