Ip地址:192.168.124.128
源端数据库 DB2 V9.7.0.0
Ip地址:192.168.124.79
目标端数据库: Oracle 11.2.0.1
[root@web1 ~]# groupadd -g 904 gg
[root@web1 ~]# useradd -g gg -u 804 -d /home/ggs -m -s /bin/bash gguser
[root@web1 ~]# passwd gguser
[root@web1 ~]# su - gguser
[gguser@web1 ~]$ vi .bash_profile
添加内容
export LD_LIBRARY_PATH=/opt/goldengate:/opt/ibm/db2/V9.7/lib64:$LD_LIBRARY_PATH
export PATH=$PATH:/opt/goldengate:/home/db2inst1/bin/sqllib/bin
# db2
export DB2INSTANCE=db2inst1
export LANG=zh_CN.GBK
[gguser@web1 ~]$ . .bash_profile
使环境变量生效。
如果需要在非ogg的目录下启动ggsci,需要添加别名,命令如下:
alias dggsci='/opt/goldengate/ggsci'
上传安装介质122022_ggs_Linux_x64_db297_64bit.zip到OGG安装目录
进入安装目录,解压
[root@web1 goldengate]# unzip 122022_ggs_Linux_x64_db297_64bit.zip
[root@web1 goldengate]# tar -xvf ggs_Linux_x64_db297_64bit.tar
添加以下内容,并运行source命令使变量生效。
[root@web1 goldengate]# vi ~/.bash_profile
# ggs
export LD_LIBRARY_PATH=/opt/ibm/db2/V9.7/lib64:$LD_LIBRARY_PATH
export GG_HOME=/opt/goldengate
export PATH=$GG_HOME:$PATH
[root@web1 goldengate]# source ~/.bash_profile
执行ggsci命令
[root@web1 goldengate]# ggsci
GGSCI (web1) 1> create subdirs
GGSCI (web1) 3> edit params mgr
输入以下内容并保存,存储路径为./dirprm/mgr.prm
--GoldenGate Manager Parameter file
PORT 7809 -- mgr主进程端口号
ACCESSRULE, PROG *, IPADDR 172.16.*.*, ALLOW
--DYNAMICPORTLIST 7810-7899 --动态端口,但要保证这些端口可用
AUTOSTART EXTRACT * --启动mgr进程启动启动extract进程
AUTORESTART EXTRACT *,RETRIES 5,WAITMINUTES 2 --当extract进程异常终止时会自动启动,每2分钟尝试重新启动所有的extract进程,一共尝试5次
--删除2天前的nd开头的trail文件
PURGEOLDEXTRACTS /opt/goldengate/nd*, USECHECKPOINTS, MINKEEPDAYS 2
LAGREPORTHOURS 1 --mgr进程每隔1小时检查extract延时情况
LAGINFOMINUTES 30 --超过30分钟作为信息记录log中
LAGCRITICALMINUTES 45 --超过45分钟作为警告记录log中
GGSCI (web1) 5> start mgr
GGSCI (web1) 6> info manager
创建测试表
[root@web1 ~]# db2 connect to db2test user db2inst1 using db2inst1
[root@web1 ~]# db2 "create table oggtest(id int,name varchar(10))"
[root@web1 ~]# db2 "insert into oggtest values(1,'ces')"
[root@web1 ~]# db2 "insert into oggtest values(2,'asd')"
[root@web1 ~]# db2 "select * from oggtest"
GoldenGate 提供了一个名为 DEFGEN 的专用工具,用于生成数据定义,当源表和目标表中的定义不同时,Oracle GoldenGate 进程将引用该专用工具。在运行 DEFGEN 之前,需要为其创建一个参数文件,指定该工具应检查哪些表以及在检查表之后存放类型定义文件的位置。可以在 GGSCI 内使用 EDIT PARAMS 命令创建这样一个参数文件。
[root@web1 ~]# cd /opt/goldengate/
[root@web1 goldengate]# ggsci
GGSCI (web1) 1> edit param defgen
DEFSFILE dirdef/source.def,PURGE
SOURCEDB db2test,USERID db2inst1,PASSWORD db2inst1
TABLE oggtest;
[root@web1 goldengate]# defgen paramfile dirprm/defgen.prm
检查一下dirdef/source.def的内容:
它列出了数据库表定义。
[root@web1 goldengate]# scp dirdef/source.def 192.168.124.79:/home/oracle/product/ogg_trg/dirdef/
db2 get db cfg for db2test|grep log
目前数据库已经处于归档状态,参数LOGARCHMETH1标志归档路径
将安装包上传至/home/oracle文件夹下并解压
instantclient-jdbc-linux.x64-12.2.0.1.0.zip
instantclient-basic-linux.x64-12.2.0.1.0.zip
instantclient-sqlplus-linux.x64-12.2.0.1.0.zip
instantclient-sdk-linux.x64-12.2.0.1.0.zip
instantclient-odbc-linux.x64-12.2.0.1.0-2.zip
解压后
创建network/admin目录
创建监听文件tnsnames.ora
[root@web1 instantclient_12_2]# mkdir -p network/admin
[root@web1 instantclient_12_2]# vi network/admin/tnsnames.ora
orcl =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.124.79)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE = DEDICATED)
(SID = orcl)
)
)
配置环境变量
[root@web1 instantclient_12_2]# vi ~/.bash_profile
# oracle_client
export ORACLE_HOME=/home/oracle/instantclient_12_2
export TNS_ADMIN=$ORACLE_HOME/network/admin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORACLE_HOME
export PATH=$ORACLE_HOME:$PATH
配置完成后,将环境变量生效
[root@web1 instantclient_12_2]# source ~/.bash_profile
连接数据库测试
[root@web1 admin]# cd /home/oracle
[root@web1 oracle]# sqlplus /nolog
SQL> conn ogg/ogg@orcl
安装包为122022_fbo_ggs_Linux_x64_shiphome.zip,
将安装包上传到/opt/goldengate目录(属组为oinstall),解压安装包
[root@localhost opt]# su - oracle
[oracle@localhost opt]$ cd goldengate/
[oracle@localhost goldengate]$ unzip 122022_fbo_ggs_Linux_x64_shiphome.zip
静默安装需要配置一个自动应答文件,位置在刚才解压出来的介质目录中,具体如下:
fbo_ggs_Linux_x64_shiphome/Disk1/response
修改内容
[oracle@localhost response]$ mv oggcore.rsp oggcore.rsp.bak
[oracle@localhost response]$ cp oggcore.rsp.bak oggcore.rsp
[oracle@localhost response]$ ls
oggcore.rsp oggcore.rsp.bak
[oracle@localhost response]$ vi oggcore.rsp
*INSTALL_OPTION=ORA11g # 根据oracle版本选择安装
*SOFTWARE_LOCATION=/opt/goldengate # 安装目录,最好指定一个空目录
*START_MANAGER=false #是否在配置完成后自动启动mgr进程,是就选true,否就选false
*MANAGER_PORT= #在start_manager为true时添加,选择mgr启动端口号,如7809
*DATABASE_LOCATION= # 在start_manager为true时添加,写上$ORACLE_HOME的值
*INVENTORY_LOCATION=
# 指定inventory目录的位置,在使用windows操作系统时是一个可选参数。
*UNIX_GROUP_NAME=
指定inventory文件夹所属用户组,windows系统不需要写
[oracle@localhost response]$ cd ..
[oracle@localhost Disk1]$ ./runInstaller -silent -responseFile /opt/goldengate/fbo_ggs_Linux_x64_shiphome/Disk1/response/oggcore.rsp
[oracle@localhost goldengate]$ ./ggsci
GGSCI (localhost.localdomain) 1> create subdirs
GGSCI (localhost.localdomain) 2> edit params mgr
PORT 7909
ACCESSRULE, PROG *, IPADDR 172.16.*.*, ALLOW
--dynamicportlist 7910-7999
purgeoldextracts /home/oracle/product/ogg_trg/dirdat/nd*,USECHECKPOINTS, MINKEEPDAYS 1
LAGREPORTHOURS 1
LAGINFOMINUTES 30
LAGCRITICALMINUTES 45
GGSCI (localhost.localdomain) 3> info mgr
GGSCI (localhost.localdomain) 4> start mgr
GGSCI (localhost.localdomain) 5> info mgr
设置别名是为了在任何地方都可以执行oggsci启动ggsci,只加入PATH不行。
[oracle@localhost goldengate]$ vi ~/.bash_profile
添加
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
export GG_HOME=/opt/goldengate
export PATH=$GG_HOME:$PATH
alias oggsci='/opt/goldengate/ggsci'
保存退出,并使环境变量生效
[oracle@M02 goldengate]$ source ~/.bash_profile
SQL> create table oggtest(id int,name varchar(10));
赋予ogg用户dba权限(偷懒)
SQL> grant dba to ogg;
[root@web1 goldengate]# ggsci
GGSCI (web1) 1> add extract inext,sourceistable
SOURCEISTABLE 参数指示 Extract 进程直接从表而不是从事务日志获取数据。
GGSCI (web1) 5> edit params inext
--
-- GoldenGate Initial Data Capture
-- for T1
--
EXTRACT INEXT
SOURCEDB db2test, USERID db2inst1, PASSWORD db2inst1
RMTHOST 192.168.124.79, MGRPORT 7909
RMTTASK REPLICAT, GROUP INLOAD
TABLE db2inst1.oggtest;
该文件默认存储位置./dirprm/inext.prm,SOURCEDB 指向包含数据的数据库。RMTHOST 和 MGRPORT 指定远程计算机和 Manager 端口。RMTFILE 指定所提取的数据将写入的文件。
测试源数据库连接
GGSCI (web1) 7> dblogin sourcedb db2test userid db2inst1,password db2inst1
[oracle@localhost goldengate]$ ggsci
为Replicat进程设置参数,参数文件名INLOAD(表示Initial LOADing,初始加载,路径为~/product/ogg_trg/dirprm/inload.prm)。
GGSCI (localhost.localdomain) 2> add replicat inload, specialrun
REPLICAT added.
GGSCI (M02.03) 1> edit params inload
--
-- GoldenGate Initial Load Delivery for T1
--
REPLICAT INLOAD
USERID ogg,PASSWORD ogg
DISCARDFILE ./dirrpt/rini.dsc, PURGE
SOURCEDEFS /home/oracle/product/ogg_trg/dirdef/source.def
MAP db2inst1.oggtest, TARGET ogg.oggtest;
对于Oracle数据库,不需要指定SOURCEDB,但如果存在多个实例,需要设置环境变量SETENV(ORACLE_SID="ORCL"),字符集不同的情况下,也是需要单独设置环境变量SETENV (NLS_LANG=AMERICAN_AMERICA.ZHS16GBK),最后一个参数MAP指示Replicat将表重新映射。
第三行DISCARDFILE官方文档里没有要求,看文件内容,应该是记录INLOAD开始和结束时间。
GGSCI (web1 as db2inst1@DB2TEST) 8> start inext
GGSCI (web1 as db2inst1@DB2TEST) 9> view report inext
查看初始提取报告
在目标端查看加载数据报告
GGSCI (localhost.localdomain) 1> view report inload
版本信息,字符集信息
数据加载统计信息,本次新增2条,与源一致。
进程的名字不能超过8个字符,默认存储路径dirprm/XXX.prm
GGSCI (web1) 5> EDIT PARAM edb2
EXTRACT edb2
dboptions NOCATALOGCONNECT
SOURCEDB db2test, USERID db2inst1,PASSWORD db2inst1
EXTTRAIL /opt/goldengate/nd
COMPRESSUPDATES
GETTRUNCATES
WILDCARDRESOLVE DYNAMIC
TABLE db2inst1.*;
GGSCI (web1) 7> DBLOGIN SOURCEDB db2test,userid db2inst1,password db2inst1;
GGSCI (web1 as db2inst1@DB2TEST) 8> ADD TRANDATA db2inst1.*
使用info trandata YTEC.*查看表是否添加trandata
在db2端删除表后需要重新添加trandata
trail文件名前缀不能超过2个字符
GGSCI (web1 as db2inst1@DB2TEST) 10> ADD EXTRACT edb2,TRANLOG,BEGIN NOW
GGSCI (web1 as db2inst1@DB2TEST) 11> add exttrail /opt/goldengate/nd,extract edb2,megabytes 5
GGSCI (M02-01) 3> start edb2
GGSCI (M02-01) 4> INFO EXTRACT edb2
GGSCI (web1 as db2inst1@DB2TEST) 14> edit params tdb2
EXTRACT tdb2
RMTHOST 192.168.124.79, MGRPORT 7909, compress
RMTTRAIL /home/oracle/product/ogg_trg/dirdat/nd
SOURCEDEFS /opt/goldengate/dirdef/source.def
gettruncates
wildcardresolve dynamic
TABLE db2inst1.*;
GGSCI (web1 as db2inst1@DB2TEST) 15> add extract tdb2,exttrailsource /opt/goldengate/nd
配置RMTTRAIL文件
GGSCI (web1 as db2inst1@DB2TEST) 16> ADD RMTTRAIL /home/oracle/product/ogg_trg/dirdat/nd,EXTRACT tdb2,megabytes 5
验证是否添加成功
GGSCI (web1 as db2inst1@DB2TEST) 17> INFO RMTTRAIL *
GGSCI (web1 as db2inst1@DB2TEST) 18> start tdb2
GGSCI (web1 as db2inst1@DB2TEST) 51> info all
CheckPointTable中ogg为oracle数据库用户名
GGSCI (localhost.localdomain) 2> edit params ./GLOBALS
添加
CheckPointTable ogg.ggschkpt
在ggs里登录oracle新增ua.ggschkpt表
GGSCI (localhost.localdomain as ogg@orcl) 42> dblogin userid ogg,password ogg
GGSCI (localhost.localdomain as ogg@orcl) 43> add checkpointtable ogg.ggschkpt
GGSCI (localhost.localdomain as ogg@orcl) 46> edit param repnd
添加
REPLICAT repnd
USERID ogg, PASSWORD ogg
--DISCARDFILE /home/oracle/product/ogg_trg/dirrpt/repnd.dsc, PURGE
DISCARDFILE /home/oracle/product/ogg_trg/dirrpt/repnd.dsc, append, MEGABYTES 5
numfiles 1000
SOURCEDEFS /home/oracle/product/ogg_trg/dirdef/source.def
dynamicresolution
handlecollisions
MAP db2inst1.*, TARGET ogg.*;
GGSCI (localhost.localdomain as ogg@orcl) 50> add replicat repnd,exttrail /home/oracle/product/ogg_trg/dirdat/nd,checkpointtable ogg.ggschkpt
【删除的方法如下】
GGSCI (localhost.localdomain as ogg@orcl) 15>dblogin userid ogg,password ogg
GGSCI (localhost.localdomain as ogg@orcl) 16> stop replicat repnd
GGSCI (localhost.localdomain as ogg@orcl) 17> delete replicat repnd
GGSCI (localhost.localdomain as ogg@orcl) 20> start replicat REPND
GGSCI (localhost.localdomain as ogg@orcl) 21> info replicat REPND
GGSCI (localhost.localdomain) 20> info all
建表
db2 |
oracle |
db2 "create table db2inst1.t0705(id int,addr varchar(10),name varchar(10))"; |
create table ogg.t0705(id int,addr varchar(10),name varchar(10)); |
在源端goldengate上添加trandata
GGSCI (web1 as db2inst1@DB2TEST) 3> add trandata db2inst1.t0705
插入
db2 |
oracle |
db2 "insert into db2inst1.t0705 values(1,'a','123')" db2 "insert into db2inst1.t0705 values(2,'b','456')" |
select * from ogg.t0705; |
|
删除
db2 |
oracle |
db2 "delete from db2inst1.t0705 where id=1" |
select * from ogg.t0705; |
|
修改
db2 |
oracle |
db2 "update db2inst1.t0705 set addr='c' where id=2" |
select * from ogg.t0705; |
|
ORA-12541: TNS:no listener将目标端监听文件中LISTENER中host选项由localhost改为ip地址
2018-07-05 08:28:33 ERROR OGG-06580 No schema is specified for the source table oggtest. The default schema is not allowed.
需将param文件中表名添加schema
2018-07-05 23:39:04 ERROR OGG-02247 Failed to read SOURCEDEFS file /home/oracle/product/ogg_trg/dirdef/source.def: Could not open /home/o
racle/product/ogg_trg/dirdef/source.def: Permission denied.
需将/home/oracle/product/ogg_trg/dirdef/文件夹属组改为oinstall,属主为oracle
2018-07-06 08:35:20 ERROR OGG-01031 There is a problem in network communication, a remote file problem, encryption keys for target and so
urce do not match (if using ENCRYPT) or an unknown error. (Reply received is Output file /home/oracle/product/ogg_trg/dirdat/nd000000 is not
in any allowed output directories.).
需将./GLOBALS配置文件中添加参数
ALLOWOUTPUTDIR /home/oracle/product/ogg_trg/dirdat
源端数据库名 db2test
源端数据库用户/密码 db2inst1/db2inst1
目标端数据库名 orcl
目标端数据库用户/密码 ogg/ogg
单表测试传输时
源端 extract进程名 inext
目标端replicat进程名 inload
实时变化数据传输时
源端extract进程 edb2
源端pump进程 tdb2
目标端replicat进程 repnd