通过Oracle Golden Gate 进行Oracle到SQL Server的同步

来源:互联网 发布:mac搜狗五笔无法使用 编辑:程序博客网 时间:2024/06/05 03:36

一、源端(Oracle)配置
1. 准备要同步的表,ogg建议同步的表必须有主键。

本例中同步scott的用户下的emp。

2. 添加defgen参数文件(由于是异构数据库之间的传输,需要转换字段类型,需用到defgen工具生成定义文件)
1
2
3
4
GGSCI (oracle) 4> edit params defgen
defsfile /ogg/dirdef/scott.def
userid system,password oracle
table scott.emp;

这里使用system用户,是因为使用scott用户会报如下错误:
表或试图不存在,select values$ from sys.props$ where name = 'NLS_CHARACTERSET';无法执行。
3. 运行defgen工具生成定义文件
进入ogg安装目录
1
./defgen paramfile /ogg/dirprm/defgen.prm

会在dirdef目录下生成一个scott.def的定义文件
4. 将scott.def拷贝到sql server ogg下的dirdef目录下。
5. 添加ogg用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
SQL>create tablespace ogg datafile 'XXX' size 200M;(必须为OGG创建独立的表空间)
SQL>create user ogg identified by ogg default tablespace ogg;
SQL>grant alter session to ogg;
SQL>grant create session to ogg;
SQL>grant connect,resource to ogg;
SQL>grant select any dictionary to ogg;
SQL>grant select any table to ogg;
SQL>grant flashback any table to ogg;
SQL>grant select any transaction to ogg;
SQL>grant insert any table to ogg;
SQL>grant update any table to ogg;
SQL>grant delete any table to ogg;
SQL>grant create any table to ogg;
SQL>grant drop any table to ogg;
SQL>grant alter any table to ogg;
SQL>exec dbms_goldengate_auth.grant_admin_privilege('OGG');
6. 添加表级附加日志
1
2
3
4
5
SQL> select supplemental_log_data_min from v$database;
SQL> alter database add supplemental log data;
SQL> alter system switch logfile;
GGSCI (oracle) 7> dblogin userid ogg,password ogg
GGSCI (oracle) 8> add trandata scott.*
7. 添加mgr及抽取进程
1
2
3
4
5
6
7
8
9
10
11
GGSCI (oracle) 5> edit params mgr
port 7809
GGSCI (oracle) 6> edit params extemp
extract ext_emp
userid ogg,password ogg
exttrail /ogg/dirdat/ext_emp
DYNAMICRESOLUTION
GETTRUNCATES
TABLE scott.emp;
GGSCI (oracle) 1> add extract ext_emp,tranlog,begin now
GGSCI (oracle) 2> add exttrail /ogg/dirdat/ra,extract ext_emp

8. 添加数据泵进程
1
2
3
4
5
6
7
8
9
GGSCI (oracle) 6> edit params dp_emp
extract dp_emp
userid ogg,password ogg
rmthost 192.168.140.208,mgrport 7809
rmttrail D:\ogg\dirdat\ra
PASSTHRU
TABLE scott.emp;
GGSCI (oracle) 5> add extract dp_emp,exttrailsource /ogg/dirdat/ra,begin now
GGSCI (oracle) 6> add rmttrail D:\ogg\dirdat\ra,extract dp_emp

9)执行4个脚本

cd /home/oracle/ogg/
sqlplus / as sysdba
@marker_setup.sql
@ddl_setup.sql
@role_setup.sql
此脚本执行完毕后,按提示执行grant ggs_ggsuser_role to ogg;
@ddl_enable.sql

切记:需将数据库的enable_goldengate_replication参数设置为true,之后可以启动mgr和ext进程。

 

二、目标端(sql server)配置

1.配置ODBC数据源
控制面板-管理工具-数据源(ODBC),添加系统DNS,取名为scott(或者自定义),注意择驱动程序类型为SQL Server Native Client 11.0,最好使用sa账号密码的登录方式。
2.添加checkpointtable(建议添加,stop rep后会产生一个检查点,start之后会继续同步)
1
2
3
4
GGSCI (oracle) 10> edit param ./globals
checkpointtable dbo.ckp
GGSCI (oracle) 8> dblogin sourcedb scott userid sa password xxx
GGSCI (oracle) 9> add checkpointtable dbo.ckp

3.添加复制rep进程
1
2
3
4
5
6
7
8
9
10
GGSCI (oracle) 11> edit param rep_emp
replicat rep_emp
sourcedefs D:\ogg\dirdef\scott.def
targetdb scott userid sa, password xxx
reperror default,discard
discardfile D:\ogg\dirrpt\rpl.dsc append
gettruncates
handlecollisions
MAP scott.emp, TARGET dbo.emp;
GGSCI (oracle) 12> add replicat rep_emp,exttrail D:\ogg\dirdat\ra,begin now,checkpointtable dbo.ckp

 
三、数据初始化同步(不停机,且源表随时更新的情况)
1. 源端配置init进程(如有多个表,这里不能写scott.*,要写为scott.emp...,写多行,否则会出现OGG-00918错误
 
2.  目标端配置load进程(如有多个表,这里不能写scott.*,要写为scott.emp...,写多行,否则会出现OGG-00918错误
 
 3.  进行数据同步
首先需要在目标端的sqlserver数据库中创建表结构,将oracle的建表语句复制到sqlserver端,修改后执行。
之后:

源端:

1
./extract paramfile /ogg/dirprm/init.prm reportfile /ogg/dirrpt/init.rpt

目标端:

1
D:\ogg>replicat paramfile dirprm\load.prm  reportfile dirrpt\load.rpt

在这里出现的一个错误:

OGG-00918 Key Column <Column Name> is Missing from Map

出现错误的原因是因为目标库的表忘了设置主键。(因此同步的表有主键很重要,不然会多出许多工作量)

若已经添加了主键则可能与异构数据库之间OGG同步的BUG有关,参考MOS,其中一个文档的解决办法如下:(文档 ID 1671873.1)--即将所有要同步的表分别配置进程。

 

加载完毕后出现如下图的提示

oracle到sqlserver的OGG最重要的一点在于源端零停机的实时同步,添加handlecollisions参数可以解决,但可能出现数据不一致的问题。

0 0
原创粉丝点击