goldengate同源一目标+多表和同源多目标+多表

小结一下,永记心中!
几经修改,看见完美
曾经遇到的问题或值得注意的地方,就此记录一下,以免再犯
开始。。。
******************同源一目标+多表******************
针对部分表进行OGG同步的话,可以参考下面的步骤进行操作,正常情况下按顺序执行就差不多了
简单操作的话可以按照下面试一下
【数据库准备】
oracle环境略过
首先,源端开启归档、全局附加日志和FORCELOGGING,目标端也开归档了,不知道不开行不行
检查开启归档
--查看是否开启
SQL> archive log list;
--开启(须重启DB)
SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
ALTER DATABASE ARCHIVELOG;
ALTER DATABASE OPEN;
--检查全局附加日志和FORCELOGGING
--查询结果要是YES,不是的话执行ALTER,这个不需要重启DB
SELECT SUPPLEMENTAL_LOG_DATA_MIN,FORCE_LOGGING FROM V\$DATABASE;
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
ALTER DATABASE FORCE LOGGING;

下面是源库和目标库都需要做的,可以同时做,下面都是在oracle用户下运行的
0、环境变量设置
--下面是OGG的一些环境变量
export GG_HOME=/home/oracle/goldengate
export PATH=\$GG_HOME:\$PATH
--下面是OGG需要的动态链接库,必须加上,否则ggsci就进不去
export LD_LIBRARY_PATH=\$LD_LIBRARY_PATH:\$ORACLE_HOME/bin

1、创建ogguser用户,是用来管理OGG的,与源端数据撇开关系
--创建的简单,没指定表空间也没限额等等,只加了一些必要的授权
create user ogguser identified by ogguser;
grant connect to ogguser;
grant resource to ogguser;
grant unlimited tablespaces to ogguser;
grant execute on tul_file to ogguser;
grant select any dictionary to ogguser;
grant select any table to ogguser;
grant alter any table to ogguser;
grant flashback any table to ogguser;
grant execute on dbms_flashback to ogguser;

2、解压安装goldengate并创建目录 ./ggsci、create subdirs
创建goldengate目录,解压文件,goldengate这个也要给oracle权限
mkdir -p /home/oracle/goldengate
tar -zxvf ggs_Adapters_Linux_x64_122.tar
执行报错了可以参考下 https://www.cnblogs.com/ritchy/p/9851450.html
[oracle@rac1 goldengate]\$ ./ggsci
GGSCI (rac1) 2> create subdirs

3、创建CKPT表
GGSCI (rac1) 1> edit param ./GLOBALS
ggschema ogguser
checkpointtable ogguser.checkpoint

GGSCI (DBDATA) 9> dblogin userid ogguser password ogguser
Successfully logged into database.

GGSCI (DBDATA as ogguser@test233) 10> add checkpointtable ogguser.checkpoint
Successfully created checkpoint table ogguser.checkpoint.

--如果出现已存在下面两个表,通过sqlplus来drop,再重新执行上面的
sqlplus ogguser/ogguser
drop table checkpoint;
drop table checkpoint_lox;

4、创建并启动MGR进程
GGSCI (rac1) 2> edit param mgr
PORT 7839
DYNAMICPORTLIST 7840-7914
AUTOSTART EXTRACT *
AUTORESTART EXTRACT *, RETRIES 3, WAITMINUTES 3
PURGEOLDEXTRACTS ./dirdat/*, USECHECKPOINTS, MINKEEPDAYS 5
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
--启动
GGSCI (rac1 as ogguser@rac1) 15> start mgr
Manager started.

【源库】
0、识别需要同步的表并添加附加日志
[oracle@rac1 ~]\$ sqlplus / as sysdba
ALTER TABLE TEST.T_ORDER ADD SUPPLEMENTAL LOG DATA (ALL) COLUMNS;
ALTER TABLE TEST.AA ADD SUPPLEMENTAL LOG DATA (ALL) COLUMNS;
ALTER TABLE TEST.BB ADD SUPPLEMENTAL LOG DATA (ALL) COLUMNS;

1、抽取进程 ext1
下面针对单实例 THREADS 1
ADD EXT ext1, TRANLOG, THREADS 1, BEGIN now
add exttrail ./dirdat/r1, extract ext1,megabytes 1000

GGSCI (rac1) 3> edit param ext1
EXTRACT ext1
setenv (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK)
setenv (ORACLE_SID=rac1)
USERID ogguser, PASSWORD ogguser
REPORTCOUNT EVERY 1 MINUTES, RATE
NUMFILES 5000
discardfile ./dirrpt/ext.dsc,append, megabytes 100
DISCARDROLLOVER AT 3:00
EXTTRAIL ./dirdat/r1,megabytes 100
DYNAMICRESOLUTION
TRANLOGOPTIONS CONVERTUCS2CLOBS
TRANLOGOPTIONS DBLOGREADER
FETCHOPTIONS NOUSESNAPSHOT
FETCHOPTIONS FETCHPKUPDATECOLS
STATOPTIONS REPORTFETCH
WARNLONGTRANS 5h,CHECKINTERVAL 30m
----TABLES------
table orders.t_order;
table orders.aa;
table orders.bb;

--启动
start ext1

--数据库环境变量字符集的查询,上面的要与下面的对应
SQL> select * from nls_database_parameters;
PARAMETER VALUE
------------------------------ --------------------------------------------------
--NLS_LANGUAGE AMERICAN
--NLS_TERRITORY AMERICA
NLS_CURRENCY \$
NLS_ISO_CURRENCY AMERICA
NLS_NUMERIC_CHARACTERS .,
--NLS_CHARACTERSET ZHS16GBK

2、传输进程
add extract pxt1,exttrailsource ./dirdat/r1
add rmttrail ./dirdat/p1, extract pxt1,megabytes 1000
--其中 192.168.5.233 对应的是目标端的主机IP
GGSCI (rac1) 4> edit param pxt1
EXTRACT pxt1
DYNAMICRESOLUTION
PASSTHRU
RMTHOST 192.168.5.233, MGRPORT 7839, COMPRESS
RMTTRAIL ./dirdat/p1
NUMFILES 500
---tables
TABLE ORDERS.T_ORDER;
TABLE ORDERS.AA;
TABLE ORDERS.BB;

--启动
start pxt1

3、注意事项:
*启动这个之前要启动目标机的MGR进程
*检查源、目标库抓取和获取数据是否正常 ll dirdat
验证源端抓取是否正常,已产生r1000000 文件
[oracle@rac1 goldengate]\$ ll dirdat
16
-rw-r----- 1 oracle oinstall 1370 10-17 18:33 r1000000

验证目标端获取数据是否正常,已产生 p1000000 文件
[oracle@DBDATA goldengate]\$ ll dirdat
total 12
-rw-r----- 1 oracle oinstall 0 Oct 17 18:46 p1000000

【目标库数据初始化】
1、源库获取SCN
需要用到源库SCN
col current_scn format 999999999999999
Select current_scn from v\$database;
2、源+目标库
create or replace directory dump_dir as '/home/oracle/dump_dir';
grant read,write on directory dump_dir to ogguser;
3、EXPDP/IMPDP
源库
expdp TEST/TEST directory=dump_dir tables=T_ORDER,aa,bb dumpfile=expdp_TEST_2tabs.dmp logfile=expdp_TEST_2tabs.log FLASHBACK_SCN=8689213127
--传输文件到目标库,然后就恢复
scp /mnt/dump_dir/expdp_TEST_2tabs* 192.168.5.233:/home/oracle/dump_dir
目标库
impdp ogguser/ogguser directory=dump_dir remap_schema=TEST:ogguser dumpfile=expdp_TEST_bb.dmp logfile=expdp_TEST_bb_imp.log

【目标库】
复制进程
用到的文件/目录是源端传输进程的p1,对应关系要一一对应
add replicat rxt1, exttrail ./dirdat/p1, checkpointtable ogguser.checkpoint

--mgr进程上面已经配置完成了
GGSCI (DBDATA) 20> edit param rxt1
replicat rxt1
handlecollisions
ASSUMETARGETDEFS
--setenv (ORACLE_SID=TEST233)
setenv (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK)
userid ogguser,password ogguser
DBOPTIONS NOSUPPRESSTRIGGERS
REPORTCOUNT EVERY 1 MINUTES, RATE
--REPERROR DEFAULT, ABEND
NUMFILES 500
discardfile ./dirrpt/rxt.dsc, append, megabytes 100
ALLOWNOOPUPDATES
----TABLES-----
map orders.t_order, target ogguser.t_order;
map orders.aa, target ogguser.aa;
map orders.bb, target ogguser.bb;

--启动
start rxt1

注意事项
NOSUPPRESSTRIGGERS 目标端需要用到触发器时,要加这个参数,否则触发器不能用(OGG默认禁用的)。
至此OGG配置算差不多了。。。

【带触发器的表的同步测试】
--源表与目标表(结构一样,初始从源库impdp过来),必须有主键哦,主键是触发器中的条件
--源端orders用户下创建
create table BB
(
bb VARCHAR2(30) not null,
sdate DATE not null
);
alter table BB add constraint PK_B primary key (BB);

--目标端ogguser创建中间表,可不要主键
create table BB_CHANGES
(
bb VARCHAR2(30),
timestamp TIMESTAMP(6),
status NUMBER default 0
);

--目标端ogguser创建触发器,源表主键必是其中的一个条件
CREATE OR REPLACE TRIGGER TR_BB_CHANGES
AFTER INSERT OR UPDATE ON BB
FOR EACH ROW
DECLARE
CNT NUMBER;
BEGIN
SELECT COUNT(1)
INTO CNT
FROM BB_CHANGES S
WHERE :NEW.BB = S.BB
AND S.STATUS = 0;
IF CNT = 0 THEN
INSERT INTO BB_CHANGES
(BB, TIMESTAMP)
VALUES
(:NEW.BB, CURRENT_TIMESTAMP);
END IF;
END;

在源库orders下执行
INSERT INTO BB (BB, SDATE) VALUES ('1', SYSDATE);
COMMIT;
INSERT INTO BB (BB, SDATE) VALUES ('2', SYSDATE);
COMMIT;
INSERT INTO BB (BB, SDATE) VALUES ('3', SYSDATE);
COMMIT;

UPDATE BB SET BB='11' WHERE BB='1';
COMMIT;
--在目标库可以看到BB表中已同步存在上面的数据,且BB_CHANGES中会增加相应的DML记录
SELECT * FROM OGGUSER.BB ORDER BY SDATE DESC ;
SELECT * FROM OGGUSER.BB_CHANGES;

******************同源两目标+多表******************
其实跟第一个差不多,如法炮制,可能比较笨,但可以互不干扰,便于维护
【源端】
1、抽取进程
ADD EXTRACT ext2, TRANLOG, THREADS 1, BEGIN now
add exttrail ./dirdat/r2, extract ext2,megabytes 100

GGSCI (rac1) 6> edit param ext2
EXTRACT ext2
setenv (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK)
setenv (ORACLE_SID=rac1)
USERID ogguser, PASSWORD ogguser
REPORTCOUNT EVERY 1 MINUTES, RATE
NUMFILES 5000
discardfile ./dirrpt/ext.dsc,append, megabytes 100
DISCARDROLLOVER AT 3:00
EXTTRAIL ./dirdat/r2,megabytes 100
DYNAMICRESOLUTION
TRANLOGOPTIONS CONVERTUCS2CLOBS
TRANLOGOPTIONS DBLOGREADER
FETCHOPTIONS NOUSESNAPSHOT
FETCHOPTIONS FETCHPKUPDATECOLS
STATOPTIONS REPORTFETCH
WARNLONGTRANS 5h,CHECKINTERVAL 30m
----TABLES------
table orders.t_order;
table orders.aa;
table orders.bb;

--启动
start ext2

2、传输进程
add extract pxt2,exttrailsource ./dirdat/r2
add rmttrail ./dirdat/p2, extract pxt2,megabytes 100

--另外一个目标端是192.168.5.247
GGSCI (rac1) 7> edit param pxt2
EXTRACT pxt2
DYNAMICRESOLUTION
PASSTHRU
RMTHOST 192.168.5.247, MGRPORT 7839, COMPRESS
RMTTRAIL ./dirdat/p2
NUMFILES 5000
---tables
TABLE ORDERS.T_ORDER;
TABLE ORDERS.AA;
TABLE ORDERS.BB;

--启动
start pxt2

【目标端】
其实与一个的一样
mgr,imp初始化等略去。。。为了省事,也创建了同样的ogguser,
复制进程来一发
add replicat rxt2, exttrail ./dirdat/p2, checkpointtable ogguser.checkpoint

GGSCI (localhost.localdomain as ogguser@test247) 53> edit param rxt2
replicat rxt2
handlecollisions
ASSUMETARGETDEFS
--setenv (ORACLE_SID=TEST247)
setenv (NLS_LANG=AMERICAN_AMERICA.AL32UTF8)
userid ogguser,password ogguser
DBOPTIONS NOSUPPRESSTRIGGERS
REPORTCOUNT EVERY 1 MINUTES, RATE
--REPERROR DEFAULT, ABEND
NUMFILES 500
discardfile ./dirrpt/rxt2.dsc, append, megabytes 100
ALLOWNOOPUPDATES
----TABLES-----
map orders.t_order, target ogguser.t_order;
map orders.aa, target ogguser.aa;
map orders.bb, target ogguser.bb;

同上测试,同步正常
不会时候觉得无从下手,寝食难安,会怀疑自己的智商是不是回到几千年了,多做几次,通了,发现也就那关键的几步,注意事项记在心就行了
当然生产可能规划比实施更重要

posted @ 2018-10-23 11:47  ritchy  阅读(869)  评论(0编辑  收藏  举报