项目背景

       Goldengate同步做过很多项目,如oracle之间同步,oracle到mysql之间同步都很熟系,SQL SERVER到Oracle端还是第一次遇到,最近客户就有这个项目需求,本以为原理类似,应该能轻松搞定,谁知在配置过程中,困难重重,折腾了2天,以下是本次配置的全过程,特意记录下来,以备后用!

由于Goldengate支持SQL SERVER的技术还不够成熟,所以很多网上的案例很少,并且oracle官方支持的OGG FOR MSSQL版本只有2个,经测试,这2个版本都是不太可行的,后来网上找了很久才找到这个SQL Server 的 Oracle GoldenGate 12.1.2.1.0 版, 并且由于是异构之间的同步,两端版本的一致性要求很高,几乎要一致,否则会报错不断,而且报错信息也很隐晦,不会直接告诉你是版本的问题,所以为了少走坑,尽量要一致的,起码大版本要一致。

项目详情

服务器信息:

源端:   100.100.100.89 服务器: windows server 2012 x64    数据库: MSSQL 2008 R2目标端: 100.100.56.89 服务器: windows server 2012 x64    数据库: ORACLE oracle 11g.2.0.3.0

GoldenGate下载地址:
http://www.oracle.com/technetwork/cn/middleware/goldengate/downloads/index.html
https://edelivery.oracle.com/osdc/faces/SearchSoftware

本次测试下载的两个 OGG :

ggs_121210_Windows_x64_MSSQL_64bit.zip (解压可用) 适用于 Windows(64 位)上 SQL
Server 的 Oracle GoldenGate 12.1.2.1.0 版 (71 MB)

121210_ggs_Windows_x64_shiphome.zip (安装可用) 适用于 Windows(64 位)上 Oracle 的
Oracle GoldenGate 12.1.2.1.0 版 (239 MB)


/*####################################################################

SQL Server 源端配置

#####################################################################*/
先确定SQL SERVER端待同步的表,本案例是YEMR_DZSQ库下的两个表:
DBO.DZSQ_JY_SQD
DBO.DZSQ_JY_SQDINFO

创建一个用户,赋予sysadmin角色,并映射待同步的库:
在这里插入图片描述
–另外数据库必须保证是完整回复(FULL)模式

–创建ODBC 数据源 (开始-管理工具-数据源odbc)
/* 注:源端数据库驱动为 SQL Server (若sql server 作为目标端,则目标端驱动为 SQL Server Native Client 10.0)
数据源名称:OGG_ODBC
数据库账号:goldengate
数据库密码:goldengate
*/
– 查看数据库或表是否启用cdc (不需要启动,配置完成自动启动)

SELECT name,is_cdc_enabled FROM sys.databases WHERE is_cdc_enabled = 1
SELECT name,is_tracked_by_cdc FROM Demo.sys.tables WHERE is_tracked_by_cdc = 1
GO

–首次备份数据库(经实测,这一步可以不用)

BACKUP DATABASE [Demo] TO DISK= N’D:\MSSQL\Demo.bak’ WITH CHECKSUM,COMPRESSION
GO

/*####################################################################

ORACLE 目标端配置

#####################################################################*/

–查看是否归档(以下是源端需要的配置,如本例ORACLE是目标端不用设置归档)

SQL> archive log list;
数据库日志模式非存档模式
自动存档禁用
存档终点 USE_DB_RECOVERY_FILE_DEST
最早的联机日志序列8
当前日志序列10

–设置归档模式

SQL> shutdown immediate;
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter database open;

–查看日志附加属性

SQL> select supplemental_log_data_min,force_logging from v$database;

–设置日志附加属性

SQL> alter database add supplemental log data;
SQL> alter database force logging;
SQL> alter system switch logfile;

–启用 goldengate

SQL> alter system set enable_goldengate_replication = true scope=both;

–启用账号 scott (本测试以 scott 为例,真实环境另建!)

SQL> alter user scott identified by tiger account unlock;
SQL> grant connect, resource to scott;
SQL> grant select any dictionary,select any table to scott;
SQL> grant execute on utl_file to scott;
SQL> grant execute on dbms_streams to scott;
SQL> grant execute on dbms_streams_adm to scott;

–ORACLE目标端创建目标表
(这一步开始以为不用做,但实际上需要手动创建表结构,这里由于表少,我自己亲自该了建表脚本,如表比较多,可网上搜索存储过程来自动转换)

CREATE TABLE scott.DZSQ_JY_SQD(
HZType varchar(20) NULL,
OutpatientNumber varchar(72) NULL,
OutpatientID varchar(72) NULL,
ZYH varchar(72) NULL,
SQDID varchar(100) NULL,
SQDMXID varchar(100) NULL,
DLCode varchar(72) NULL,
ID varchar(20) NULL,
LBCode varchar(100) NULL,
ZHID varchar(100) NULL,
ZHCode varchar(100) NULL,
ZHName varchar(100) NULL,
YZCode varchar(100) NULL,
YZName varchar(255) NULL,
FYLB varchar(100) NULL,
FYID varchar(100) NULL,
FYCode varchar(100) NULL,
Quantity varchar(100) NULL,
Price float NULL,
MoneyPrice float NULL,
BBCode varchar(100) NULL,
StateType varchar(100) NULL,
IndexNum varchar(100) NULL,
ReimbursementSign varchar(5) NULL,
Company varchar(72) NULL,
ExternalCode varchar(50) NULL,
Urgent varchar(1) NULL
);
REATE TABLE scott.DZSQ_JY_SQDInfo(
SQDID varchar(100) PRIMARY KEY NOT NULL,
CFBH varchar(72) NULL,
OutpatientNumber varchar(72) NULL,
OutpatientID varchar(72) NULL,
ZYH varchar(72) NULL,
DeptCode varchar(72) NULL,
WardCode varchar(72) NULL,
HZType varchar(10) NULL,
SQDName varchar(max) NULL,
CostState char(1) NULL,
Urgent char(1) NULL,
StateType varchar(10) NULL,
CZYGH varchar(10) NULL,
SQDDateTime timestamp NULL,
CYW varchar(255) NULL);

/*####################################################################

SQL Server 源端配置

#####################################################################*/

  • 源端安装 GoldenGate: 解压 ggs_121210_Windows_x64_MSSQL_64bit.zip ,更名并移动到C盘(C:\software\ogg)
    –目标端安装 GoldenGate: 解压 121210_ggs_Windows_x64_shiphome.zip 安装到C盘(C:\software\ogg)
    –本案例中,源端和目标端的 OGG位置相同,所以以下配置注意区别.

–创建相关目录

C:\software\ogg> GGSCIGGSCI (MSSQL)> CREATE SUBDIRS/*执行结果:Parameter files                C:\software\ogg\dirprm: createdReport files                   C:\software\ogg\dirrpt: createdCheckpoint files               C:\software\ogg\dirchk: createdProcess status files           C:\software\ogg\dirpcs: createdSQL script files               C:\software\ogg\dirsql: createdDatabase definitions files     C:\software\ogg\dirdef: createdExtract data files             C:\software\ogg\dirdat: createdTemporary files                C:\software\ogg\dirtmp: createdCredential store files         C:\software\ogg\dircrd: createdMasterkey wallet files         C:\software\ogg\dirwlt: createdDump files                     C:\software\ogg\dirdmp: created

–将 Manager 进程添加为 Windows 服务(名称为: GGSMGR)

C:\software\ogg> INSTALL ADDSERVICE/*执行结果:Service 'GGSMGR' created.Install program terminated normally.

–配置 Manager 参数文件(内容如下)
GGSCI (MSSQL)> EDIT PARAM mgr

PORT 7809DYNAMICPORTLIST 7840-7850AUTORESTART ER *, WAITMINUTES 5, RETRIES 5

–启动 GGSMGR 服务 (删除服务: sc delete GGSMGR)
GGSCI (MSSQL)> START MGR
/*执行结果:
Starting Manager as service (‘GGSMGR’)…
Service started.
*/
–查看进程
GGSCI (MSSQL)> INFO ALL
GGSCI (MSSQL)> INFO MGR

登录SQL SERVER库:

GGSCI (WIN-GISL2ND9JES) 1> dblogin sourcedb OGG_ODBC userid goldengate passwordgoldengate2019-12-15 14:41:34   WARNING OGG-05236   ODBC Warning: The specified DSN 'OGG_ODBC' uses a client driver that may be incompatible with the database server. Microsoft SQL Server 2008 R2 requires SQLNCLI10.DLL or a more recent version.2019-12-15 14:41:34   INFO    OGG-03036   Database character set identified as windows-936. Locale: zh_CN.2019-12-15 14:41:34   INFO    OGG-03037   Session character set identified as GBK.Successfully logged into database.

查看可访问的表:

GGSCI (WIN-GISL2ND9JES as goldengate@OGG_ODBC) 2> lIST TABLES DBO.*
DBO.CDSS_JC_FORCYL
DBO.CDSS_JY_FORCYL
DBO.DZSQ_JC_JCPACKAGE
DBO.DZSQ_JC_SQD
DBO.DZSQ_JC_SQDINFO
DBO.DZSQ_JY_JYBB
DBO.DZSQ_JY_JYPACKAGE
DBO.DZSQ_JY_SQD
DBO.DZSQ_JY_SQDINFO
DBO.DZSQ_YJ_SPECIMENDATA
DBO.DZSQ_YJ_SPECIMENINFO

SQL SERVER源端增加待同步表的附加日志:

GGSCI (WIN-GISL2ND9JES as goldengate@OGG_ODBC) 3> add trandata YEMR.DZSQ.DBO.DZSQ_JY_SQD
2019-12-15 14:36:32 ERROR OGG-25168 The specified GGSCHEMA name 'goldengate
’ in the GLOBALS file does not exist in the database, or you do not have permiss
ion to use it. Specify a valid GGSCHEMA name in the GLOBALS file.
Create a GLOBALS file in the base Oracle GoldenGate installation directory, and set the GGSCHEMA parameter to that of an existing or new schema in the source database. Oracle recommends that you create a specific schema for Oracle GoldenGate objects. For example - CREATE SCHEMA ggs. Do not to use the dbo schema.

以上错误主要是需要在/GLOBALS下指定GGSCHEMA ,下面指定:

GGSCI> EDIT PARAMS ./GLOBALS
Save the GLOBALS file.
Using GGSCHEMA in the GLOBALS file is a new requirement for Oracle GoldenGate for SQL Server CDC Capture. It is required so that ADD TRANDATA can identify which schema to create necessary objects under then Extract knows which schema to call those objects from during runtime. Classic Extract does not have this requirement.

GGSCI (WIN-GISL2ND9JES as goldengate@OGG_ODBC) 4> edit params ./GLOBALS

GGSCHEMA dbo

这里谨记是dbo,并不是goldengate这个创建的用户

GGSCI (WIN-GISL2ND9JES as goldengate@OGG_ODBC) 3> add trandata dbo.DZSQ_JY_SQDInfo
Logging of supplemental log data is enabled for table dbo.DZSQ_JY_SQDInfo
GGSCI (WIN-GISL2ND9JES as goldengate@OGG_ODBC) 4> add trandata dbo.DZSQ_JY_SQD
Logging of supplemental log data is enabled for table dbo.DZSQ_JY_SQD

–配置定义文件(内容如下)

GGSCI(MSSQL)> edit params defgen
sourcedb OGG_ODBC userid goldengate password goldengate
defsfile C:\software\ogg\dirdef\YEMR_DZSQ_tabless.def
table dbo.DZSQ_JY_SQD;
table dbo.DZSQ_JY_SQDInfo;

–生成表定义文件C:\software\ogg\dirdef\YEMR_DZSQ_tabless.def

c:\software\ogg>defgen paramfile C:\software\ogg\dirprm\defgen.prm INFO    OGG-03037   Session character set identified as GBK.defsfile C:\software\ogg\dirdef\YEMR_DZSQ_tabless.deftable dbo.DZSQ_JY_SQD;Retrieving definition for dbo.DZSQ_JY_SQD.table dbo.DZSQ_JY_SQDInfo;Retrieving definition for dbo.DZSQ_JY_SQDInfo.Definitions generated for 2 tables in C:\software\ogg\dirdef\YEMR_DZSQ_tabless.def.

–SQL SERVER源端配置 Extract 文件(内容如下)
GGSCI(MSSQL)> EDIT PARAMS EXTA

EXTRACT EXTASETENV (NLS_LANG ="SIMPLIFIED CHINESE_CHINA.ZHS16GBK")sourcedb OGG_ODBC userid goldengate password goldengateTRANLOGOPTIONS MANAGESECONDARYTRUNCATIONPOINTEXTTRAIL C:\software\ogg\dirdat\prEOFDELAYCSECS 10table dbo.DZSQ_JY_SQD;table dbo.DZSQ_JY_SQDInfo;

增加抽取进程:

GGSCI(MSSQL)> ADD EXTRACT EXTA, TRANLOG,BEGIN NOWGGSCI(MSSQL)> ADD RMTTRAIL C:\software\ogg\dirdat\pr, EXTRACT EXTA

–配置 pump 文件(配置 REPLICAT 文件)
GGSCI(MSSQL)> EDIT PARAMS PUMPA

extract PUMPASETENV (NLS_LANG = "SIMPLIFIED CHINESE_CHINA.ZHS16GBK")sourcedb OGG_ODBC userid goldengate password goldengatermthost 100.100.56.89, mgrport 7809, compressrmttrail C:\software\ogg\dirdat\prEOFDELAYCSECS 10table dbo.DZSQ_JY_SQD;table dbo.DZSQ_JY_SQDInfo;

–添加 pump 进程
ADD EXTRACT PUMPA,EXTTRAILSOURCE C:\software\ogg\dirdat\pr, BEGIN NOW
ADD RMTTRAIL C:\software\ogg\dirdat\pr, EXTRACT PUMPA

–查看所有进程 或某个进程
GGSCI(MSSQL)> INFO ALL
GGSCI(MSSQL)> INFO MGR

–启动进程
GGSCI(MSSQL)> START EXTA
GGSCI(MSSQL)> START PUMPA

– 查看数据库或表是否启用cdc(增加附加日志后会自动启用)
SELECT name,is_cdc_enabled FROM sys.databases WHERE is_cdc_enabled = 1
SELECT name,is_tracked_by_cdc FROM Demo.sys.tables WHERE is_tracked_by_cdc = 1
GO

/*####################################################################

SQL Server 初始化数据

#####################################################################*/

–源: 初始提取配置
GGSCI(MSSQL)> EDIT PARAMS INEXT

SOURCEISTABLEsourcedb OGG_ODBC userid goldengate password goldengatermthost 100.100.56.89, mgrport 7809, compressRMTFILE C:\software\ogg\dirdat\exEOFDELAYCSECS 10table dbo.DZSQ_JY_SQD;table dbo.DZSQ_JY_SQDInfo;

–目标: 初始加载配置
GGSCI(MSSQL)> EDIT PARAMS INLOAD

SPECIALRUNEND RUNTIMESETENV (NLS_LANG = "SIMPLIFIED CHINESE_CHINA.ZHS16GBK")USERID scott, PASSWORD tigerEXTFILE C:\software\ogg\dirdat\exSOURCEDEFS C:\software\ogg\dirdef\YEMR_DZSQ_tabless.defMAP dbo.DZSQ_JY_SQD,target scott.DZSQ_JY_SQD;MAP dbo.DZSQ_JY_SQDInfo,target scott.DZSQ_JY_SQDInfo;

–源: 提取数据到目标文件夹
C:\software\ogg> extract paramfile dirprm\inext.prm reportfile dirrpt\inext.rpt

–目标: 加载数据到表
C:\software\ogg> replicat paramfile dirprm/inload.prm
注:这一步可能会遇到很多报错,大部分是OGG版本兼容性问题,注意源端和目标端的OGG版本要尽量一致,起码大版本要一致,例如两边必须都是12.1,否则会报很多奇怪的错误,这里走了很多弯路。

/*####################################################################

ORACLE 目标端配置

#####################################################################*/

–检查点用于存储 Extract 和 REPLICAT 进程的当前读/写位置
GGSCI(MSSQL)> DBLOGIN USERID scott, PASSWORD tiger
GGSCI(MSSQL)> ADD CHECKPOINTTABLE scott.chkpt
这个地方,如OGG更换升级了版本, chkpt也要做一下更新,否则会报commit同步错误:

GGSCI> UPGRADE CHECKPOINTTABLE [owner.table] Start the replicat once
the checkpoint table upgrade is done.

–配置同步复制进程
GGSCI(MSSQL)> EDIT PARAM MSREP

REPLICAT MSREPSETENV ( NLS_LANG = "SIMPLIFIED CHINESE_CHINA.ZHS16GBK")USERID scott, PASSWORD tigerSOURCEDEFS C:\software\ogg\dirdef\YEMR_DZSQ_tabless.defHANDLECOLLISIONSASSUMETARGETDEFSMAP dbo.DZSQ_JY_SQD,target scott.DZSQ_JY_SQD;MAP dbo.DZSQ_JY_SQDInfo,target scott.DZSQ_JY_SQDInfo;

–添加进程
GGSCI(MSSQL)> ADD REPLICAT MSREP,CHECKPOINTTABLE scott.chkpt,EXTTRAIL ./dirdat/pr

–启用进程
GGSCI(MSSQL)> START REPLICAT MSREP

–查看进程
GGSCI(MSSQL)> INFO ALL
GGSCI(MSSQL)> INFO MSREP

至此,历尽艰辛,终于配置完成!!测试同步正常!!