配置环境:
建议在相同版本OGG(即Oracle GoldenGate)之间进行复制,我在这里之所以选择不同版本的OGG,便于后续的比较学习。
一、准备OGG的运行用户
在这里,我直接使用oracle用户作为OGG的运行用户。
二、确认OGG的安装目录
在源端,即Linux环境下,我选择/u01/app/goldengate作为OGG的安装目录,注意:/u01/app/goldengate目录的属主必须为OGG的运行用户,在这里,该目录的属主必须为oracle。
在目标端,即Win环境下,我选择G:\app\goldengate作为OGG的安装目录
三、打开数据库的归档模式
SQL> archive log list
Database log mode No Archive Mode
Automatic archival Disabled
Archive destination USE_DB_RECOVERY_FILE_DEST
Oldest online log sequence 5
Current log sequence 7
SQL> shutdown immediate
SQL> startup mount
SQL> alter database archivelog;
SQL> alter database open;
四、打开源端数据库最小附加日志
因为这里搭建的只是单向复制,OGG捕捉的是源端数据库的更改,所以只需对源端数据库开启最小附加日志
SQL> select supplemental_log_data_min from v$database;
SUPPLEME
--------
NO
SQL> alter database add supplemental log data;
Database altered.
SQL> alter system switch logfile; --切换日志使附加日志生效
System altered.
五、在数据库中建立OGG用户并赋予响应的权限
为了简化授权,直接将dba角色赋予给gg用户。
SQL> create user gg identified by gg;
SQL> grant dba to gg;
六、上传软件包到指定目录,进行安装
在源端:
[oracle@node1 ~]$ cd /u01/app/goldengate/
[oracle@node1 goldengate]$ ls
ogg112101_fbo_ggs_Linux_x64_ora11g_64bit.zip
[oracle@node1 goldengate]$ unzip ogg112101_fbo_ggs_Linux_x64_ora11g_64bit.zip
Archive: ogg112101_fbo_ggs_Linux_x64_ora11g_64bit.zip
inflating: fbo_ggs_Linux_x64_ora11g_64bit.tar
inflating: OGG_WinUnix_Rel_Notes_11.2.1.0.1.pdf
inflating: Oracle GoldenGate 11.2.1.0.1 README.txt
inflating: Oracle GoldenGate 11.2.1.0.1 README.doc
[oracle@node1 goldengate]$ tar xvf fbo_ggs_Linux_x64_ora11g_64bit.tar
在当前目录下,执行ggsci命令
[oracle@node1 goldengate]$ ./ggsci
./ggsci: error while loading shared libraries: libnnz11.so: cannot open shared object file: No such file or directory
报以上错误,在/home/oracle/.bash_profile添加如下内容:export LD_LIBRARY_PATH=$ORACLE_HOME/lib,添加完毕后,记得使配置文件生效:source /home/oracle/.bash_profile。
注意:为了ggsci命令在任何目录下可以执行,可在/home/oracle/.bash_profile设置如下环境变量:
export GG_HOME=/u01/app/goldengate
export PATH=$GG_HOME:$PATH
重新执行ggsci命令
[oracle@node1 goldengate]$ ggsci
Oracle GoldenGate Command Interpreter for Oracle
Version 11.2.1.0.1 OGGCORE_11.2.1.0.1_PLATFORMS_120423.0230_FBO
Linux, x64, 64bit (optimized), Oracle 11g on Apr 23 2012 08:32:14
Copyright (C) 1995, 2012, Oracle and/or its affiliates. All rights reserved.
GGSCI (node1.being.com) 1>
表示进入成功
在ggsci环境下创建GoldenGate子目录
[oracle@node1 goldengate]$ ggsci
GGSCI (node1.being.com) 1> create subdirs
Creating subdirectories under current directory /u01/app/goldengate
Parameter files /u01/app/goldengate/dirprm: already exists
Report files /u01/app/goldengate/dirrpt: created
Checkpoint files /u01/app/goldengate/dirchk: created
Process status files /u01/app/goldengate/dirpcs: created
SQL script files /u01/app/goldengate/dirsql: created
Database definitions files /u01/app/goldengate/dirdef: created
Extract data files /u01/app/goldengate/dirdat: created
Temporary files /u01/app/goldengate/dirtmp: created
Stdout files /u01/app/goldengate/dirout: created
在目标端:即Win8环境下,注意,Win8的OGG版本是12.1.2.1.0,需图形界面安装,而11.2.1.0.1则不需要,直接解压缩即可。
将12.1.2.1.0版本的OGG解压缩以后,双击里面的setup文件。
第一步:选择OGG对应的数据库版本
第二步:指定OGG的运行目录
其它直接点击下一步即可。
安装完毕后,会启动一个mgr进程的窗口。
同样需要在目标端ggsci环境下创建GoldenGate子目录,因该OGG安装完毕后,以上目录均已自动创建,故不再创建。
七、配置Manager进程
在ggsci环境下配置Manager进程
源端配置如下:
[oracle@node1 goldengate]$ ggsci
GGSCI (node1.being.com) 1> edit param mgr
GGSCI (node1.being.com) 2> start mgr
Manager started.
GGSCI (node1.being.com) 3> info all
Program Status Group Lag at Chkpt Time Since Chkpt
MANAGER RUNNING --可见MGR进程已启动
目标端同样如此。
八、在源端上添加表级附加日志
为了验证搭建的效果,在这里我们新建一个test用户,并新建一张test表。
SQL> create user test identified by test;
User created.
SQL> grant connect,resource to test;
Grant succeeded.
SQL> conn test/test
Connected.
SQL> create table test (id number);
Table created.
在ggsci命令行中登录数据库,为所有需要复制的表添加trandata
GGSCI (node1.being.com) 1> dblogin userid gg,password gg
Successfully logged into database.
GGSCI (node1.being.com) 2> add trandata test.*
2015-08-24 18:14:59 WARNING OGG-00869 No unique key is defined for table 'TEST'. All viable columns will be used to represent the key, but may not guarantee uniqueness. KEYCOLS may be used to define the key.
Logging of supplemental redo data enabled for table TEST.TEST.
从上面的WARNING我们可以看出,因为test表没有唯一性约束,故OGG将该行的所有列来作为它的唯一性约束。
九、在源端上添加Extract进程
GGSCI (node1.being.com) 5> add ext exta,tranlog,begin now
其中,exta为进程名,一般为ext开头表示是extract进程,后面可以加1-2位字符标识;
tranlog表示要抽取数据库日志;
begin now表示从当前时间点开始抽取
注意:添加完Extract进程后,我们有两种方式来传输数据。
第一种,exta进程直接将捕捉到的有效数据写入到远程队列,网络拓扑如下:
不难看出,该方式对网络要求较高,在实际生产环境中很少使用该种方式。
第二种,首先将数据抽取到本地,然后再由Pump进程传输到远程队列。网络拓扑如下:
在这里,我们采用第二种方式
十、在源端上配置Pump进程
创建完exta进程后,使用本地队列时为exta进程配置队列如下:
GGSCI (node1.being.com) 1> add exttrail /u01/app/goldengate/dirdat/la,ext exta,megabytes 20
megabytes表示每个队列文件的大小,超过该大小则会滚动写入到下一个队列文件。
此时exta复制到了本地,下面来配置Pump进程,负责将数据原封不动的搬运到目标端。
GGSCI (node1.being.com) 3> add extract dpea,exttrailsource /u01/app/goldengate/dirdat/la
此时exttrailsource指定的是本地队列的路径
为该Pump进程配置远程队列
GGSCI (node1.being.com) 4> add rmttrail G:\app\goldengate\dirdat\ra,ext dpea,megabytes 20
rmttrail为目标队列的位置,队列一般由路径加上两个标识如ra来表示。
十一、在源端配置Extract进程参数
GGSCI (node1.being.com) 6> edit param exta
内容如下:
EXTRACT exta
setenv ( NLS_LANG = AMERICAN_AMERICA.AL32UTF8 )
setenv ( ORACLE_SID = orcl )
USERID gg, PASSWORD gg
EXTTRAIL /u01/app/goldengate/dirdat/la
dynamicresolution
table test.*;
启动exta进程,看其能否正常启动
GGSCI (node1.being.com) 15> start exta
Sending START request to MANAGER ...
EXTRACT EXTA starting
GGSCI (node1.being.com) 17> info all
Program Status Group Lag at Chkpt Time Since Chkpt
MANAGER RUNNING
EXTRACT STOPPED DPEA 00:00:00 00:27:12
EXTRACT RUNNING EXTA 00:55:27 00:00:07
注意:正常情况下,exta进程启动后的status是running,如果依旧是stopped,可通过view report exta查看exta启动异常的原因。
PS:第一次配置后,启动异常,后来通过view report exta命令查看其报错信息:ERROR OGG-00396 Command not terminated by semi-colon,才知道table test.*参数后没有加分号。添加完毕后,重新启动,启动正常。
十二、在源端配置Pump进程参数
GGSCI (node1.being.com) 19> edit param dpea
内容如下:
extract dpea
setenv ( NLS_LANG = AMERICAN_AMERICA.AL32UTF8 )
passthru
rmthost 192.168.2.1,mgrport 7809, compress
rmttrail G:\app\goldengate\dirdat\ra
dynamicresolution
table test.*;
其中rmthost指定目标端的主机IP和端口,rmttrail指定远程队列的路径,passthru表示本进程是一个Pump进程,不需要跟数据库交互。
启动查看状态
GGSCI (node1.being.com) 20> start dpea
Sending START request to MANAGER ...
EXTRACT DPEA starting
GGSCI (node1.being.com) 21> info all
Program Status Group Lag at Chkpt Time Since Chkpt
MANAGER RUNNING
EXTRACT RUNNING DPEA 00:00:00 00:42:42
EXTRACT RUNNING EXTA 00:00:00 00:00:04
启动OK。
十三、在目标端上初始化目标表
可以通过exp/imp、RMAN、init load进行数据初始化
如果目标端不需要历史数据,可以只建立表结构
在这里,同源端一样,在目标端新建一个test用户,并新建一张test表。
十四、在目标端添加Replicat进程
GGSCI (Lenovo-PC) 5> add rep repa,exttrail G:\app\goldengate\dirdat\ra,nodbcheckpoint
REPLICAT added.
十五、配置Replicat进程参数
GGSCI (Lenovo-PC) 7> edit param repa
内容如下:
replicat repa
setenv (NLS_LANG = 'AMERICAN_AMERICA.ZHS16GBK')
setenv (ORACLE_SID = test)
userid gg, password gg
reperror default,abend
discardfile G:\app\goldengate\dirrpt\repa.dsc,append, megabytes 10
assumetargetdefs
dynamicresolution
map test.*, target test.*;
启动Replicat进程
GGSCI (Lenovo-PC) 25> start repa
Sending START request to MANAGER ...
REPLICAT REPA starting
GGSCI (Lenovo-PC) 26> info all
Program Status Group Lag at Chkpt Time Since Chkpt
MANAGER RUNNING
REPLICAT ABENDED REPA 00:00:00 01:45:47
发现REPA进程的状态是ABENDED,通过view report exta命令查看其报错信息:
2015-08-24 14:39:21 ERROR OGG-02091 Operation not supported because enable_goldengate_replication is not set to true.
在目标数据库中将enable_goldengate_replication的值设为true
SQL> alter system set enable_goldengate_replication=true;
系统已更改。
重新启动Replicat进程,状态OK
GGSCI (Lenovo-PC) 29> info all
Program Status Group Lag at Chkpt Time Since Chkpt
MANAGER RUNNING
REPLICAT RUNNING REPA 00:00:00 00:00:08
至此,各进程均已正常启动,下面,通过对源端test表进行增删改,看其目标端的变化
源端新增一条数据
SQL> insert into test values(1);
1 row created.
SQL> commit;
Commit complete.
目标端的结果如下:
SQL> select * from test;
ID
----------
1
对源端进行删、改,变化同样发生在目标端中。
也可通过查看各进程的状态来查看复制的情况
GGSCI (node1.being.com) 31> stats exta
Sending STATS request to EXTRACT EXTA ...
Start of Statistics at 2015-08-24 23:23:49.
Output to /u01/app/goldengate/dirdat/la:
Extracting from TEST.TEST to TEST.TEST:
*** Total statistics since 2015-08-24 23:15:07 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Daily statistics since 2015-08-24 23:15:07 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Hourly statistics since 2015-08-24 23:15:07 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Latest statistics since 2015-08-24 23:15:07 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
End of Statistics.
GGSCI (node1.being.com) 32> stats dpea
Sending STATS request to EXTRACT DPEA ...
Start of Statistics at 2015-08-24 23:23:54.
Output to G:\app\goldengate\dirdat\ra:
Extracting from TEST.TEST to TEST.TEST:
*** Total statistics since 2015-08-24 23:15:08 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Daily statistics since 2015-08-24 23:15:08 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Hourly statistics since 2015-08-24 23:15:08 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Latest statistics since 2015-08-24 23:15:08 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
End of Statistics.
GGSCI (Lenovo-PC) 30> stats repa
Sending STATS request to REPLICAT REPA ...
Start of Statistics at 2015-08-24 15:24:34.
Replicating from TEST.TEST to TEST.TEST:
*** Total statistics since 2015-08-24 15:15:14 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Daily statistics since 2015-08-24 15:15:14 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Hourly statistics since 2015-08-24 15:15:14 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
*** Latest statistics since 2015-08-24 15:15:14 ***
Total inserts 1.00
Total updates 1.00
Total deletes 1.00
Total discards 0.00
Total operations 3.00
End of Statistics.
至此,基于GoldenGate的单向复制环境搭建完毕。