goldendate实现oracle到postgresql的数据同步

来源:互联网 发布:网络运营人员要求 编辑:程序博客网 时间:2024/05/21 06:19
环境:redhat 6.5
源端:oracle 11g                 对应ogg版本11.2.1.30   ip192.168.100.67
目标端:postgresql9.4.4       对应ogg版本11.2.1.30  ip192.168.100.77
(注:在源端和目标端安装ogg的时候要安装与数据库匹配,如oracle的需要安装符合oracle的,pg的就要安装支持pg的,)
本人新手,在安装ogg的时候没有注意到这个问题,导致安装后的ogg与数据库无法连接,很闹心。
源端:   

  
目标端: 

安装ogg(源端):
1 下载介质
GoldenGate的安装介质可以从Oracle的官网上下载。
http://www.oracle.com/technetwork/middleware/goldengate/downloads/index.html
2 配置GoldenGate用户
下载完成后将其拷贝到源和目标的相应位置解压完成后,即可以开始进行配置。
# su – oracle
$ mkdir /oracle/ggate
$ cd /oracle/ggate
$ unzip  ……
$tar  ……
$ vi ~/.bash_profile
 
添加如下的内容:
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1
export ORACLE_SID=orcl
export PATH=$ORACLE_HOME/bin:$PATH
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/oracle/ggate/lib
export GGATE=/oracle/ggate
 
3 创建目录
 
使用ggsci工具,创建必要的目录。
$ cd /oracle/ggate
$ ./ggsci
Oracle GoldenGate Command Interpreter for Oracle
Version 11.2.1.0.30 20671604 21111031_FBO
Linux, x64, 64bit (optimized), Oracle 11g on Aug 20 2015 02:52:03


Copyright (C) 1995, 2015, Oracle and/or its affiliates. All rights reserved.


GGSCI (localhost.localdomain) 1>


检查源端归档情况,如果没有,开启归档
alter system set log_archive_dest='LOCATION=USE_DB_RECOVERY_FILE_DEST' scope=both sid='*';
shutdown immediate
startup mount
alter database archivelog;
alter database open;


源库开启附加日志创建goldengate用户并授权
create user goldengate identified by goldengate;
alter database add supplemental log data;
grant dba to goldengate;


源端create subdirs
GGSCI (localhost.localdomain) 2> create subdirs


Creating subdirectories under current directory /oracle/ggate


Parameter files               /oracle/ggatedirprm: already exists
Report files                   /oracle/ggatedirrpt: created
Checkpoint files             /oracle/ggatedirchk: created
Process status files          /oracle/ggatedirpcs: created
SQL script files               /oracle/ggate/dirsql: created
Database definitions files     /oracle/ggatedirdef: created
Extract data files             /oracle/ggatedirdat: created
Temporary files                /oracle/ggatedirtmp: created
Stdout files                   /oracle/ggatedirout: created


源端创建mgr进程


GGSCI (localhost.localdomain) 3> edit param mgr


添加内容:
PORT 7809        #ogg的默认端口号7809


GGSCI (localhost.localdomain) 4> view param mgr


PORT 7809
启动mgr进程:
GGSCI (localhost.localdomain) 1> start mgr


Program     Status      Group       Lag at Chkpt  Time Since Chkpt


MANAGER     RUNNING                                           


在源端建测试表


SQL> connect goldengate/goldengate
Connected.
SQL> create table ggtest (col1 number, col2 varchar2(20));


Table created.


SQL> alter table ggtest add primary key (col1);


Table altered.


确认ogg可以和数据库进行连接

GGSCI (localhost.localdomain) 5> dblogin userid goldengate password goldengate
Successfully logged into database.


查询数据库中的表
GGSCI (localhost.localdomain) 7> list table *  
GOLDENGATE.GGTEST


Found 1 tables matching list criteria.


GGSCI (localhost.localdomain) 8> capture tabledef goldengate.ggtest
Table definitions for GOLDENGATE.GGTEST:
COL1                           NUMBER NOT NULL PK
COL2                           VARCHAR (20)


在目标端安装ogg
用postgresql数据库的用户postgres即可
安装方式与在oracle上的安装一样
添加环境变量;
export PATH=$PGBASE/bin:$PATH
export PGDATA=/opt/pgsql/data
export PGBASE=/opt/pgsql-9.4.4
export PGPORT=5432
export LD_LIBRARY_PATH=$PGBASE/lib:/oracle/ggate/lib
export GGATE=/oracle/ggate


在目标端配置odbc.ini文件
[ODBC Data Sources]
Postgres=DataDirect 9.4 PostgreSQL Wire Protocol
[ODBC]
IANAAppCodePage=106
InstallDir=/oracle/ggate
[Postgres]
Driver=/oracle/ggate/lib/GGpsql25.so
Description=DataDirect 9.4 PostgreSQL Wire Protocol
Database=postgres
HostName=192.168.100.77
PortNumber=5432
LogonID=postgres
Password=postgres
参数说明
[ODBC Data Sources]里边配置该ODBC的别名,本文件中是Postgres
[ODBC]:
IANAAppCodePage指的是字符集的设置 这里的106值得是UTF8,如果是4则为ISO-8859-1,注意这个应该始终和postgres的字符集设置相同,不同字符集对应的值见附件。
InstallDir对应ogg的安装目录
[Postgres]:这里的名称对应的是上边ODBC的别名
Driver这里指向的是ogg安装目录下的lib/GGpsql25.so
Description是描述
Database填写数据库名称
HostName填写本机的hostname(最好填写ip地址)。
PosrNumber是postgres的监听端口。
LogonID填写postgres的用户名
password填写postgres的密码
 
之后再指定环境变量ODBCINI,加入到oracle用户.bash_profile中。并生效(odbc.ini建在哪里就改成对应的路径)
export ODBCINI=/oracle/ggate/odbc.ini


目标端create subdirs
GGSCI (localhost.localdomain) 1> create subdirs


Creating subdirectories under current directory /oracle/ggate


Parameter files               /oracle/ggatedirprm: already exists
Report files                   /oracle/ggatedirrpt: created
Checkpoint files             /oracle/ggatedirchk: created
Process status files          /oracle/ggatedirpcs: created
SQL script files               /oracle/ggate/dirsql: created
Database definitions files     /oracle/ggatedirdef: created
Extract data files             /oracle/ggatedirdat: created
Temporary files                /oracle/ggatedirtmp: created
Stdout files                   /oracle/ggatedirout: created


目标端配置mgr


GGSCI (localhost.localdomain) 2> edit param mgr


添加内容:
PORT 7809        #ogg的默认端口号7809


GGSCI (localhost.localdomain) 4> view param mgr


PORT 7809
启动mgr进程:
GGSCI (localhost.localdomain) 1> start mgr


Program     Status      Group       Lag at Chkpt  Time Since Chkpt


MANAGER     RUNNING             


在目标端建测试表
CREATE TABLE "public"."ggtest"
(
  "col1" integer NOT NULL,
  "col2" varchar(20),
  CONSTRAINT "PK_Col111" PRIMARY KEY ("col1")
);
ogg连接数据并查看表
GGSCI (localhost.localdomain) 1> dblogin sourcedb gg_postgres userid postgres password postgres


2016-11-03 13:25:54  INFO    OGG-03036  Database character set identified as UTF-8. Locale: en_US.


2016-11-03 13:25:54  INFO    OGG-03037  Session character set identified as UTF-8.
Successfully logged into database.
GGSCI (localhost.localdomain)  2> list table *
public.ggtest     #有我们建的表
public.node1
public.node2
public.sales_detail
public.score
public.student
public.testtab01
public.users


Found 8 tables matching list criteria.
GGSCI (localhost.localdomain) 3> capture tabledef "public"."ggtest"
Table definitions for public.ggtest:
col1                                                                                                                             NUMBER (10) NOT NULL PK
col2                                                                                                                             VARCHAR (20)




在源端建立extract进程
GGSCI (localhost.localdomain) 9> edit param epos
添加一下内容
EXTRACT epos
USERID goldengate, PASSWORD goldengate
RMTHOST 192.168.100.77, MGRPORT 7809
RMTTRAIL ./dirdat/ep
TABLE goldengate.ggtest;


参数说明:
epos是新建extract进程的名字
userid ,password为数据库中创建的用户名和密码
RMTHOST 指定目标系统的用户或ip及其GoldengateManager进程的端口号,还用于定义是否使用压缩进行传输。
RMTTRAIL:指定写入到目标断的哪个队列;
TABLE 是测试表的名字。


GGSCI (localhost.localdomain) 5> add extract epos, tranlog, begin now
EXTRACT added.


GGSCI (localhost.localdomain) 6> add exttrail ./dirdat/ep, extract epos, megabytes 5
EXTTRAIL added.




GGSCI (localhost.localdomain) 7> start epos


Sending START request to MANAGER ...
EXTRACT EPOS starting






GGSCI (localhost.localdomain) 8> info all


Program     Status      Group       Lag at Chkpt  Time Since Chkpt


MANAGER     RUNNING
EXTRACT        RUNNING     EPOS        00:00:00      00:00:05


源端创建DEFINITIONS文件
GGSCI (localhost.localdomain) 10> edit param defgen


DEFSFILE ./dirdef/GGTEST.def
USERID goldengate, password goldengate
TABLE GOLDENGATE.GGTEST;


[oracle@localhost ggate]$./defgen paramfile ./dirprm/defgen.prm
  
***********************************************************************  
        Oracle GoldenGate Table Definition Generator for Oracle  
 Version 11.2.1.0.30 16934304 OGGCORE_11.2.1.0.30_PLATFORMS_130709.1600.1  
   Linux, x64, 64bit (optimized), Oracle 11g on Jul 18 2013 04:10:02  
   
Copyright (C) 1995, 2013, Oracle and/or its affiliates. All rights reserved.  
  
  
  
  
                    Starting at 2013-09-04 22:24:21  
***********************************************************************  
  
  
Operating System Version:  
Linux  
Version #1 SMP Wed DEC 22 14:37:40 EST 2016, Release 2.6.32-300.10.1.el5uek  
Node: ggos  
Machine: x86_64  
                         soft limit   hard limit  
Address Space Size   :    unlimited    unlimited  
Heap Size            :    unlimited    unlimited  
File Size            :    unlimited    unlimited  
CPU Time             :    unlimited    unlimited  
  
  
Process id: 39345  
  
  
***********************************************************************  
**            Running with the following parameters                  **  
***********************************************************************  
DEFSFILE /oracle/ggate/dirdef/GGTEST.def  
USERID goldengate, password ***  
TABLE GOLDENGATE.GGTEST;  
Retrieving definition for GOLDENGATE.GGTEST  
  
Definitions generated for 1 table in /oracle/ggate/dirdef/GGTEST.def  


[oracle@localhost ggate]$ more ./dirdef/GGTEST.def
*+- Defgen version 2.0, Encoding UTF-8
*
* Definitions created/modified  2016-11-3 13:48
*
*  Field descriptions for each column entry:
*
*     1    Name
*     2    Data Type
*     3    External Length
*     4    Fetch Offset
*     5    Scale
*     6    Level
*     7    Null
*     8    Bump if Odd
*     9    Internal Length
*    10    Binary Length
*    11    Table Length
*    12    Most Significant DT
*    13    Least Significant DT
*    14    High Precision
*    15    Low Precision
*    16    Elementary Item
*    17    Occurs
*    18    Key Column
*    19    Sub Data Type
*
Database type: ORACLE
Character set ID: UTF-8
National character set ID: UTF-16
Locale: neutral
Case sensitivity: 14 14 14 14 14 14 14 14 14 14 14 14 11 14 14 14
*
Definition for table GOLDENGATE.GGTEST
Record length: 262
Syskey: 0
Columns: 2
COL1   64     50        0  0  0 1 0     50     50     50 0 0 0 0 1    0 1 2
COL2   64    200       56  0  0 1 0    200    200      0 0 0 0 0 1    0 0 0
End of definition


然后通过scp将def文件传输到目标端
scp ./dirdef/GGTEST.def postgres@192.168.100.77:/oracle/ggate/dirdef/GGTEST.def


目标端建立replicat进程
GGSCI (localhost.localdomain) 1> edit param rpos
添加以下内容:
REPLICAT rpos
SOURCEDEFS ./dirdef/GGTEST.def
SETENV ( PGCLIENTENCODING = "UTF8" )
SETENV (ODBCINI="/oracle/ggate/odbc.ini" )
SETENV (NLS_LANG="AMERICAN_AMERICA.AL32UTF8")
TARGETDB Postgres, USERID postgres, PASSWORD postgres
DISCARDFILE ./dirrpt/diskg.dsc, purge
MAP GOLDENGATE.GGTEST, TARGET public.ggtest, COLMAP (COL1=col1,COL2=col2);
参数说明:
SOURCEDEFS:假定两端数据结构不一致,使用此参数指定源端的数据结构定义文件,该文件需要由GlodenGate工具产生。
SETENV:配置系统环境变量,PGCLIENTENCODING 指的是PG的字符集 。
TARGETDB :目标端的信息。
DISCARDFILE: 定义discardfile文件位置,如果处理中有记录出错会写入到此文件中。
MAP:用于指定源端与目标端表的映射关系;


GGSCI (localhost.localdomain) 2> add replicat rpos,  NODBCHECKPOINT, exttrail ./dirdat/ep


REPLICAT added.


GGSCI (localhost.localdomain) 3> start rpos


Sending START request to MANAGER ...
REPLICAT REPKG starting




GGSCI (localhost.localdomain) 4> info all


Program     Status      Group       Lag at Chkpt  Time Since Chkpt


MANAGER     RUNNING
REPLICAT    RUNNING     RPOS        00:00:00      00:00:04


同步测试

同步成功。
感想:ogg实现不同数据库的同步问题大多出现在了进程的配置上面,以上方法我在源端只用了一个进程,epos,把表中产生变化的文件ep直接发送到了目标端,让目标端读取变化,实现数据的同步。
还可以在源端配置两个进程,一个接受变化的进程把文件保存在本地,一个发送进程,把变化的文件发送到目标端,这种方式相对稳妥。只需要注意:源头的trail文件名一定要与目的端的trail文件名用不同的名称。
0 0
原创粉丝点击