简单说明:
依据《CentOS6u9 Oracle11g 静默安装手工建库统一配置方案》
搭建两台实验机:源端 source/192.168.1.100,目标端 target/192.168.1.200
实验机预配置:
1° 源端配置:
hostname source
sed -i "s/^HOSTNAME=.*$/HOSTNAME=$(hostname)/g" /etc/sysconfig/network
echo "$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "$(ifconfig eth0|grep inet|awk -F'[ :]' '{print $13}') $(hostname)">>/etc/hosts
2° 目标端配置:
hostname target
sed -i "s/^HOSTNAME=.*$/HOSTNAME=$(hostname)/g" /etc/sysconfig/network
echo "$(grep -E '127|::1' /etc/hosts)">/etc/hosts
echo "$(ifconfig eth0|grep inet|awk -F'[ :]' '{print $13}') $(hostname)">>/etc/hosts
3° 两节点均配置,修改原监听配置的IP:
su - oracle
SID=orcl
NLS=UTF8
rm -rf $ORACLE_HOME/network/admin/listener.ora
cat >$ORACLE_HOME/network/admin/listener.ora<<EOF
SID_LIST_LISTENER =
(SID_LIST =
(SID_DESC =
(GLOBAL_DBNAME = ${SID})
(ORACLE_HOME = $ORACLE_HOME)
(SID_NAME = ${SID})
)
)
LISTENER =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = $(hostname -i))(PORT = 1521))
)
ADR_BASE_LISTENER = $ORACLE_BASE
EOF
lsnrctl start
4° 两节点均配置,启动库:
su - oracle
echo 'startup;'|sqlplus / as sysdba
配置安装OGG:
1° 两节点均配置,创建OGG使用的数据库用户ggs并赋权
考虑灾备切换的情形,建议源端和目标端统一配置:
su - oracle
sqlplus / as sysdba
create tablespace GGS datafile '/oradata/orcl/ggs01.dbf' size 50m
autoextend on next 64m maxsize 30G EXTENT MANAGEMENT LOCAL SEGMENT SPACE MANAGEMENT AUTO;
CREATE USER ggs IDENTIFIED BY ggs DEFAULT TABLESPACE GGS;
GRANT CONNECT TO ggs;
GRANT ALTER ANY TABLE TO ggs;
GRANT ALTER SESSION TO ggs;
GRANT CREATE SESSION TO ggs;
GRANT FLASHBACK ANY TABLE TO ggs;
GRANT SELECT ANY DICTIONARY TO ggs;
GRANT SELECT ANY TABLE TO ggs;
GRANT RESOURCE TO ggs;
GRANT drop ANY TABLE TO ggs;
GRANT SYSDBA TO ggs;
GRANT DBA TO ggs;
GRANT EXECUTE ANY TYPE TO ggs;
GRANT SELECT ANY TRANSACTION TO ggs;
GRANT SELECT ON DBA_CLUSTERS TO ggs;
GRANT EXECUTE ON DBMS_FLASHBACK TO ggs;
grant execute on utl_file to ggs;
exec dbms_streams_auth.grant_admin_privilege('GGS');
grant insert on system.logmnr_restart_ckpt$ to ggs;
grant update on sys.streams$_capture_process to ggs;
grant become user to ggs;
alter system set enable_goldengate_replication=true scope=both;
exit
2° 两节点均配置,打开库级最小附加日志:
su - oracle
sqlplus / as sysdba
select count(1) from v$transaction;
-- 查看当前的事务数量,如果是一个正在使用的生产库,建议在业务低峰期操作
alter database force logging;
alter database add supplemental log data (primary key) columns;
alter database add supplemental log data (unique) columns;
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
alter system archive log current;
select SUPPLEMENTAL_LOG_DATA_MIN,SUPPLEMENTAL_LOG_DATA_PK,
SUPPLEMENTAL_LOG_DATA_UI from v$database;
-- 查看状态,3个yes
exit
3° 两节点均配置,解压OGG软件:
su -
# 创建OGG家目录
mkdir /ggs
chown -R oracle: /ggs
su - oracle
cd /ggs
unzip /tmp/Oracle\ GoldenGate\ 11.2.1.0.33\ for\ Oracle\ 11g\ on\ Linux\ x86-64.zip
rm -rf O*
tar -xf fbo_ggs_Linux_x64_ora11g_64bit.tar
rm -rf fbo_ggs_Linux_x64_ora11g_64bit.tar
echo "export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib">>/home/oracle/.bash_profile
source /home/oracle/.bash_profile
# 创建运行时所需的目录
./ggsci
create subdirs
exit
echo 'GGSCHEMA ggs'>GLOBALS
chmod 640 GLOBALS
# 也可以在./ggsci下使用命令创建:
# ./ggsci
# edit param ./GLOBALS
# GGSCHEMA ggs
# 命令edit param其实就是vi
4° 两节点均配置,配置DDL支持和SEQUENCE支持:
su - oracle
cd /ggs
# 配置DDL支持
sqlplus / as sysdba
@/ggs/marker_setup.sql
-- Enter Oracle GoldenGate schema name: ggs
@/ggs/ddl_setup.sql
-- Enter Oracle GoldenGate schema name:ggs
@/ggs/role_setup.sql
--Enter GoldenGate schema name:ggs
GRANT GGS_GGSUSER_ROLE TO ggs;
@/ggs/ddl_enable.sql
@/ggs/ddl_pin ggs
exit
# 配置SEQUENCE同步支持
sqlplus / as sysdba
@/ggs/sequence.sql
-- Please enter the name of a schema for the GoldenGate database objects:
-- ggs
exit
5° 两节点均配置,配置OGG的mgr管理进程:
su - oracle
cd /ggs/dirprm
# 配置mgr管理进程的配置文件
# 使用8000端口作为管理进程监听端口
# 配置8001~8200端口作为后续进程的端口
# 需要打通源端和目标端这些端口的连通权限
cat >mgr.prm<<EOF
port 8000
DYNAMICPORTLIST 8001-8200
--AUTOSTART EXTRACT *
--AUTORESTART EXTRACT *
PURGEOLDEXTRACTS ./dirdat*/*,usecheckpoints, minkeepdays 7
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
EOF
# 启动管理进程,查看
cd /ggs
./ggsci
start mgr
info mgr
exit
至此,OGG预部署完成
配置测试OGG:
1° 源端配置,创建测试用业务账号,脚本模拟生产:
su - oracle
# 创建测试用业务账号,赋权,创建测试sequence和测试table
sqlplus / as sysdba
create user test identified by test default tablespace users;
grant connect,resource to test;
grant select on v_$session to test;
grant select on v_$sesstat to test;
grant select on v_$statname to test;
grant execute on sys.dbms_lock TO test;
conn test/test
create table test1(id number);
create sequence seq_test
minvalue 1
maxvalue 999999999999999999999999999
start with 1
increment by 1
cache 20;
exit
# 创建脚本,并行执行,模拟生产
cat >/tmp/test.sh<<EOFALL
sqlplus -s test/test<<EOF
begin
for i in 1..100000 loop
insert into test1 values(seq_test.nextval);
commit;
dbms_lock.sleep(1);
end loop;
end;
/
EOF
EOFALL
for i in $(seq 10);do bash /tmp/test.sh & done
2° 源端配置,创建配置抽取进程:
su - oracle
# 创建目录,将抽取同步数据放在自己专有的目录内
mkdir /ggs/dirdat_test
# 创建抽取进程配置文件
cd /ggs/dirprm
cat >exttest.prm<<EOF
EXTRACT exttest
setenv (ORACLE_SID=orcl)
setenv (NLS_LANG=AMERICAN_AMERICA.UTF8)
SETENV (ORACLE_HOME = "/u01/app/oracle/product/11.2.0/db_1")
USERID ggs, PASSWORD ggs
THREADOPTIONS MAXCOMMITPROPAGATIONDELAY 20000
REPORT AT 01:59
DYNAMICRESOLUTION
DDL INCLUDE ALL
ddloptions addtrandata,report
-- 动态添加trandata
reportrollover at 02:00
discardfile ./dirrpt/exttest.dsc,append,megabytes 10
gettruncates
numfiles 3000
EXTTRAIL ./dirdat_test/et
dynamicresolution
BR BROFF
--- table
table test.*;
--- sequence
sequence test.*;
EOF
# 配置启动抽取进程
cd /ggs
./ggsci
dblogin userid ggs,password ggs
add trandata test.*
info trandata test.*
add extract exttest,tranlog,begin now
add exttrail ./dirdat_test/et, extract exttest
start exttest
info exttest
exit
3° 源端配置,创建配置发送进程:
su - oracle
# 在目标端创建对应的目录,配置发送进程时会将文件放入该目录中
ssh oracle@192.168.1.200 'mkdir /ggs/dirdat_test'
cd /ggs/dirprm
cat >puptest.prm<<EOF
EXTRACT puptest
setenv (ORACLE_SID="orcl")
setenv ( NLS_LANG = AMERICAN_AMERICA.UTF8)
passthru
REPORT AT 01:59
reportrollover at 02:00
RMTHOST 192.168.1.200, MGRPORT 8000, compress
RMTTRAIL ./dirdat_test/rt
dynamicresolution
numfiles 3000
---table
table USERCENTER.*;
---sequence
sequence USERCENTER.*;
EOF
# 配置启动发送进程
# 确认目标端mgr进程已启动
ssh oracle@192.168.1.200 'netstat -tupln|grep 8000'
cd /ggs
./ggsci
add extract puptest,exttrailsource ./dirdat_test/et
add rmttrail ./dirdat_test/rt,extract puptest
start puptest
info puptest
exit
# 确认目标端接收到了文件
ssh oracle@192.168.1.200 'ls -l /ggs/dirdat_test/rt*'
4° 源端配置,配置directory,数据泵导出测试业务账号,发送到目标端:
su - oracle
mkdir /home/oracle/dmp
sqlplus / as sysdba
create directory dmp as '/home/oracle/dmp';
exit
# 查询出当前的SCN,建议在业务低峰时期进行数据同步操作
CUR_SCN=$(echo 'select to_char(current_scn) from v$database;'|sqlplus -s / as sysdba|grep '[0-9]')
# 数据泵导出,根据SCN导出
expdp \'/ as sysdba\' directory=dmp dumpfile=test.dmp schemas=test FLASHBACK_SCN=${CUR_SCN}
# 如果dmp包较大,又需要使用生产网络传输,建议切分后限速传输
# cd /home/oracle/dmp
# split -b 500m -d test.dmp test.dmp.
# scp -l 20000 test.dmp.* oracle@IP:/PWD
# 目标端使用cat恢复
# cat test.dmp.*>test.dmp
# 两端可以使用md5sum做校验
# md5sum test.dmp
scp /home/oracle/dmp/test.dmp oracle@192.168.1.200:/tmp/
5° 目标配置,配置directory,数据泵导入测试业务账号,禁用外键等:
su - oracle
mkdir /home/oracle/dmp
sqlplus / as sysdba
create directory dmp as '/home/oracle/dmp';
exit
cp /tmp/test.dmp /home/oracle/dmp
impdp \'/ as sysdba\' directory=dmp dumpfile=test.dmp
# 跑脚本,禁用外键、触发器等对象
cd /tmp
sqlplus / as sysdba
set null "NULL VALUE"
set feedback off
set heading off
set linesize 180
set pagesize 9999
set echo off
set verify off
SET SQLPROMPT --SQL>
col table_name for a30
col column_name for a30
col data_type for a15
col object_type for a20
col constraint_type_desc for a30
define owner_list=" in ('TEST')"
--Disable triggers
spool disable_triggers.sql
select 'alter trigger '||owner||'.'||trigger_name||' disable;' from dba_triggers where owner&owner_list and status='ENABLED';
spool off
--Disable FKs
spool disable_fks.sql
select 'alter table '||owner||'.'||table_name||' disable constraint '||constraint_name||';'
from dba_constraints where constraint_type='R' and status='ENABLED' and owner&owner_list;
spool off
--Disable cascade delete
spool disable_cas_del.sql
select 'alter table '||a.owner||'.'||a.table_name||' disable constraint '||a.constraint_name||';'
from dba_constraints a,dba_constraints b,dba_cons_columns c
where a.r_constraint_name=b.constraint_name
and a.constraint_name=c.constraint_name
and a.status ='ENABLED'
and a.delete_rule like '%CASCADE%'
and a.owner &owner_list;
spool off
--Disable jobs
spool disable_jobs.sql
select 'exec sys.dbms_ijob.broken(' ||job || ',true);' from dba_jobs
where schema_user &owner_list;
spool off
SET SQLPROMPT SQL>
@disable_triggers.sql
@disable_fks.sql
@disable_cas_del.sql
@disable_jobs.sql
COMMIT;
select '--------------check jobs info' from dual;
alter session set NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';
col interval format a60
col what format a60
set trimspool on
set linesize 1000
set pagesize 80
select job,schema_user,last_date,next_date,interval,broken,what from dba_jobs where schema_user &owner_list;
select '---------------------------------end' from dual;
select '---------------------check trigger info' from dual;
select owner,trigger_name,status from dba_triggers where owner&owner_list;
select '---------------------------------end' from dual;
select '--------------------fk info' from dual;
select owner,table_name,constraint_name,status from dba_constraints where constraint_type='R' and owner&owner_list;
select '----------------end' from dual;
select '------------------cascade delete info' from dual;
select a.owner,a.table_name,a.constraint_name,a.status from dba_constraints a,dba_constraints b,dba_cons_columns c where a.r_constraint_name=b.constraint_name and a.constraint_name=c.constraint_name and a.delete_rule like '%CASCADE%' and a.owner &owner_list;
------end
exit
6° 目标配置,创建配置应用进程:
su - oracle
# 创建应用进程配置文件
cd /ggs/dirprm
cat >reptest.prm<<EOF
REPLICAT reptest
SETENV (ORACLE_SID = orcl)
SETENV (NLS_LANG = "American_America.UTF8")
USERID ggs,PASSWORD ggs
sqlexec "Alter session set constraints=deferred"
REPORT AT 01:59
reportrollover at 02:00
--handlecollisions
DBOPTIONS SUPPRESSTRIGGERS
DBOPTIONS DEFERREFCONST
DDL include objname "test.*"
--ddlerror 1403 ignore
DDLERROR DEFAULT abend
DDLSUBST 'enable' WITH 'disable' INCLUDE OBJTYPE 'trigger', OPTYPE alter
DDLSUBST 'enable' WITH 'disable' INCLUDE INSTR 'ZGLT_CASCADE', OPTYPE alter, OBJTYPE 'CONSTRAINT'
reperror default,abend
--reperror default,discard
discardfile ./dirrpt/reptest.dsc,append, megabytes 100
assumetargetdefs
checksequencevalue
allownoopupdates
dynamicresolution
numfiles 3000
--HANDLECOLLISIONS
mapexclude test.SYS_JOURNAL_*;
map test.*, target test.*;
EOF
# 配置启动抽取进程
cd /ggs
./ggsci
dblogin userid ggs,password ggs
add checkpointtable ggs.rep_test_ckpt
-- 此处的检查点表的命名也要符合一定规则
add replicat reptest,exttrail ./dirdat_test/rt,checkpointtable ggs.rep_test_ckpt
start reptest, aftercsn 270440
info reptest
exit
7° 监控测试表,测试:
for i in $(seq 10)
do
echo 'select max(id) from test.TEST1;'|sqlplus -s / as sysdba
sleep 1
done
回滚:
源端回滚:
su - oracle
# 关闭抽取和发送进程
cd /ggs
./ggsci
dblogin userid ggs,password ggs
stop *
delete EXTRACT PUPTEST
delete EXTRACT EXTTEST
exit
# 删除抽取和发送进程配置文件,删除数据文件目录
rm -rf /ggs/dirprm/puptest.prm
rm -rf /ggs/dirprm/exttest.prm
rm -rf /ggs/dirdat_test
rm -rf /ggs/dirrpt/*
# 删除测试账号
ps -ef|grep test.sh|grep -v grep|awk '{print $2}'|xargs kill -9
sqlplus -s / as sysdba
set heading off feedback off
spool /tmp/kill_session.sql
select 'alter system kill session '''||SID||','||SERIAL#||''';'
from v$session where SCHEMANAME='TEST';
spool off
@/tmp/kill_session.sql
drop user test cascade;
exit;
目标端回滚:
su - oracle
# 应用进程
cd /ggs
./ggsci
dblogin userid ggs,password ggs
stop *
delete REPLICAT REPTEST
delete checkpointtable ggs.rep_test_ckpt
-- 需要交互确认
exit
# 删除进程配置文件,删除数据文件目录
rm -rf /ggs/dirprm/reptest.prm
rm -rf /ggs/dirdat_test
rm -rf /ggs/dirrpt/*
# 删除测试账号
echo 'drop user test cascade;'|sqlplus / as sysdba
[TOC]
版权声明:本文为zwjzqqb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。