利用OGG实现Oracle数据库双向同步

与世无争的帅哥 提交于 2020-04-27 21:04:30

环境:

  • 服务器一(RAC实例一的任意一个节点)

IP地址:              10.19.100.91

Oracle数据库版本     11.2.0.4

Oracle数据库实例名   aegisdb

Oracle数据库端口     1521

待同步数据库schema   aegis

OGG端口               7809

  • 服务器二(RAC实例二的任意一个节点)

IP地址:              10.19.100.92

Oracle数据库版本     11.2.0.4

Oracle数据库实例名   aegisdb

Oracle数据库端口     1521

待同步数据库schema   aegis

OGG端口               7809

  • Oracle GoldenGate版本

12.2.0.1

  • 数据库连接串(tnsname.ora文件,2台服务器配置上相同):
AEGISDB91 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.19.100.91)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = aegisdb)
    )
  )

AEGISDB92 =
  (DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 10.19.100.92)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = aegisdb)
    )
  )

在两个服务器的Oracle数据库上各创建表用于测试同步

$ sqlplus aegis/123456


SQL*Plus: Release 11.2.0.4.0 Production on Sat Mar 5 21:26:36 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options


SQL> create table t1(col1 number);

Table created.

SQL> create table t2(col1 varchar2(10),col2 number);

Table created.

一、 安装前准备

此步骤需要在两台服务器上都执行

  1. 配置环境变量
[oracle@localhost ~]$ more /home/oracle/.bash_profile

# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/dbhome_1
export ORACLE_SID=aegisdb
export OGG_HOME=/u01/app/ogg

export PATH=$OGG_HOME:$ORACLE_HOME/bin:$PATH:$HOME/bin
export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$OGG_HOME
  1. 创建Oracle GoldenGate软件目录
mkdir /u01/app/ogg
  1. 准备安装文件
$ cd /opt/Oracle/

$ unzip fbo_ggs_Linux_x64_shiphome.zip

二、 安装Oracle GoldenGate

此步骤需要在两台服务器上都执行

这里使用SecureCRT + Xmanager的图形界面来安装Oracle GoldenGate。

 

$ cd fbo_ggs_Linux_x64_shiphome/Disk1/

$ ./runInstaller

根据数据库版本选择GoldenGate版本,点击next

 

选择软件安装路径。Manager进程可启可不启,就算这里不启动之后也要手工启动,所以这里可以选start Manager。点击next

 

确认安装信息,点击Install

 

等待安装完成

 

点击Close完成安装

三、 对数据库进行必要的配置

此步骤需要在两台服务器上都执行

3.1 修改数据库参数

ALTER SYSTEM SET ENABLE_GOLDENGATE_REPLICATION = TRUE SCOPE=BOTH;

3.2 启动归档模式

SQL> shutdown immediate

Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount

ORACLE instance started.

Total System Global Area  521936896 bytes
Fixed Size                  2254824 bytes
Variable Size             209717272 bytes
Database Buffers          306184192 bytes
Redo Buffers                3780608 bytes
Database mounted.

SQL> alter database archivelog;

Database altered.

SQL> alter database open;

Database altered.

SQL> alter database add supplemental log data;

Database altered.

SQL> alter database force logging;

Database altered.

SQL> ALTER SYSTEM ARCHIVE LOG CURRENT;

System altered.

全部完成后应有如下结果:

SQL>  select log_mode,supplemental_log_data_min,force_logging from v$database;

LOG_MODE     SUPPLEME FOR
------------ -------- ---
ARCHIVELOG   YES      YES

3.3  创建OGG用户,OGG用户需要独立表空间

SQL> create tablespace oggtbs datafile '/DATA/aegisdb/oggtbs.dbf' size 100M autoextend on;

Tablespace created.
 
SQL> create user ogg identified by ogg default tablespace oggtbs temporary tablespace temp;

User created.

SQL> grant connect,resource to ogg;

Grant succeeded.

SQL> grant execute on utl_file to ogg;

Grant succeeded.

3.4  在数据库上执行GoldenGate脚本

注意:一定要cd到ogg的目录下去执行,而且oracle用户的bash_profile里需要有export LD_LIBRARY_PATH=$ORACLE_HOME/lib

$ cd /u01/app/ogg/

$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Sat Mar 5 20:34:34 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> @marker_setup.sql;

输入之前创建的ogg用户名

SQL> @ddl_setup.sql;

输入之前创建的ogg用户名

SQL> @role_setup.sql;

输入之前创建的ogg用户名

SQL>grant GGS_GGSUSER_ROLE to ogg;

Grant succeeded.

SQL>@ddl_enable.sql;

Trigger altered.

四、 配置Oracle GoldenGate

注意:一定要cd到ogg的目录下去执行

GoldenGate的日志位于其安装目录下的ggserr.log中,在配置过程中出现错误的时候可以通过日志排查问题。

4.1    启动两个服务器上GoldenGate的Manager进程

此步骤需要在两台服务器上都执行

$ cd /u01/app/ogg/

./ggsci

Oracle GoldenGate Command Interpreter for Oracle
Version 12.2.0.1.1 OGGCORE_12.2.0.1.0_PLATFORMS_151211.1401_FBO
Linux, x64, 64bit (optimized), Oracle 11g on Dec 12 2015 00:54:38
Operating system character set identified as UTF-8.

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

   
GGSCI (localhost.localdomain) 1> start mgr

Manager started.

GGSCI (localhost.localdomain) 2> info all

Program     Status      Group       Lag at Chkpt  Time Since Chkpt

MANAGER     RUNNING                   

下面这个命令可以看到mgr的运行端口,也可以修改它,并对管理进程进行配置,不配置并不影响同步

GGSCI (localhost.localdomain) 3>edit params mgr

PORT 7809

#DYNAMICPORTLIST 7840-7939
#AUTORESTART EXTRACT *,RETRIES5,WAITMINUTES 3
#PURGEOLDEXTRACTS /ogg/app/dirdat/*,usecheckpoints, minkeepdays 3
#LAGREPORTHOURS 1
#LAGINFOMINUTES 30
#LAGCRITICALMINUTES 45

参数说明:

  • PORT:指定服务监听端口,默认端口为7809
  • DYNAMICPORTLIST:动态端口:可以制定最大256个可用端口的动态列表,当指定的端口不可用时,管理进程将会从列表中选择一个可用的端口,源端和目标段的Collector、Replicat、GGSCI进程通信也会使用这些端口;
  • COMMENT:注释行,也可以用--来代替;
  • AUTOSTART:指定在管理进程启动时自动启动哪些进程;
  • AUTORESTART:自动重启参数设置:本处设置表示每3分钟尝试重新启动所有EXTRACT进程,共尝试5次;
  • PURGEOLDEXTRACTS:定期清理trail文件设置:本处设置表示对于超过3天的trail文件进行删除。
  • LAGREPORT、LAGINFO、LAGCRITICAL:定义数据延迟的预警机制:本处设置表示MGR进程每隔1小时检查EXTRACT的延迟情况,如果超过了30分钟就把延迟作为信息记录到错误日志中,如果延迟超过了45分钟,则把它作为警告写到错误日志中。

4.2    在服务器一上创建一个extract进程

GGSCI (localhost.localdomain) 1> add extract ext_t2,tranlog,begin now

EXTRACT added.

GGSCI (localhost.localdomain) 2> add exttrail /u01/app/ogg/dirdat/t2,extract ext_t2,megabytes 10

EXTTRAIL added.

其中megabytes表示队列大小,本处为10M

编辑extract进程的参数

GGSCI (localhost.localdomain) 3> edit params ext_t2

extract ext_t2

userid ogg@aegisdb91,password ogg     #如果是rac的话,@后请写rac唯一实例名
rmthost 10.19.100.92,mgrport 7809
rmttrail /u01/app/ogg/dirdat/t2
table aegis.t2, FILTER(@STREQ (COL1, 'PEK')=1);

该extract进程会把服务器一上所有对aegis.t2表里col1=‘PEK’的数据的修改(insert、delete、update)同步出去。

为了保证update操作被同步,还需要在ogg登录服务一的数据库,并执行一些命令

GGSCI (localhost.localdomain) 4> dblogin userid ogg@aegisd,password ogg

Successfully logged into database.

GGSCI (localhost.localdomain as ogg@aegisdb) 5> ADD TRANDATA aegis.t2

2016-03-05 21:38:23  WARNING OGG-06439  No unique key is defined for table T2. 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 AEGIS.T2.
TRANDATA for scheduling columns has been added on table 'AEGIS.T2'.
TRANDATA for instantiation CSN has been added on table 'AEGIS.T2'.

启动同步进程

 

GGSCI (localhost.localdomain) 5> start ext_t2

Sending START request to MANAGER ...

EXTRACT EXT_T2 starting

GGSCI (localhost.localdomain) 6> info all

Program     Status      Group       Lag at Chkpt  Time Since Chkpt

MANAGER     RUNNING                                          
EXTRACT     RUNNING     EXT_T2        00:00:00      00:28:16

可以看到在服务器二的/u01/app/ogg/dirdat/目录下产生了文件

$ ls /u01/app/ogg/dirdat/

t2000000000

4.3    在服务器二上创建一个replicat进程

$ ./ggsci

Oracle GoldenGate Command Interpreter for Oracle
Version 12.2.0.1.1 OGGCORE_12.2.0.1.0_PLATFORMS_151211.1401_FBO
Linux, x64, 64bit (optimized), Oracle 11g on Dec 12 2015 00:54:38
Operating system character set identified as UTF-8.

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

GGSCI (localhost.localdomain) 1> edit params ./GLOBALS
GGSCHEMA ogg
CHECKPOINTTABLE ogg.checkpoint

GGSCI (localhost.localdomain) 2> dblogin userid ogg,password ogg
Successfully logged into database.

GGSCI (localhost.localdomain as ogg@aegisdb) 3> add checkpointtable ogg.checkpoint
Successfully created checkpoint table ogg.checkpoint.

GGSCI (localhost.localdomain as ogg@aegisdb) 4> add replicat rep_t2, exttrail /u01/app/ogg/dirdat/t2, checkpointtable ogg.checkpoint
REPLICAT added.

GGSCI (localhost.localdomain as ogg@aegisdb) 5> edit params rep_t2
replicat rep_t2
ASSUMETARGETDEFS
userid ogg, password ogg
discardfile /u01/app/ogg/dirdat/rep_t2_discard.txt, append, megabytes 10
DDL include all

map aegis.t2, target aegis.t2;

启动replicate进程

GGSCI (localhost.localdomain) 6> start rep_t2

Sending START request to MANAGER ...

REPLICAT REP_T2 starting

GGSCI (localhost.localdomain) 7> info all

Program     Status      Group       Lag at Chkpt  Time Since Chkpt

MANAGER     RUNNING                                          
REPLICAT    RUNNING     REP_T2        00:00:00      00:00:02

4.4    检查从服务器一到服务器二的同步是否成功

在服务器一的sqlplus中执行

SQL> select * from t1;

no rows selected

SQL> insert into t2 values('PEK',1);

1 row created.

SQL> insert into t2 values('FOC',2);

1 row created.

SQL> commit;

Commit complete.

如果同步配置成功可以在服务器二上看到

SQL> select * from t2;

COL1             COL2
---------- ----------
PEK                 1

对服务器一上aegis.t2表中col1=’PEK’的修改被同步到了服务器二上

为了避免数据不一致导致GoldenGate出问题,这里重置一下数据。

停止复制进程在两个库上均执行

truncate table t2;
insert into t2 values (‘PEK’,1);
insert into t2 values (‘FOC’,2);
commit;

4.5    在服务器二上创建一个extract进程

GGSCI (localhost.localdomain) 1> add extract ext1,tranlog,begin now

EXTRACT added.

GGSCI (localhost.localdomain) 2> add exttrail /u01/app/ogg/dirdat/lt,extract ext1

EXTTRAIL added.

GGSCI (localhost.localdomain) 3> edit params ext1

extract ext1

userid ogg@aegisdb92,password ogg

rmthost 10.19.100.91,mgrport 7809

rmttrail /u01/app/ogg/dirdat/lt

table aegis.t2, FILTER(@STREQ (COL1, 'PEK')=0);

该extract进程会把服务器二上所有对aegis.t2表里col1 <>‘PEK’的数据的修改(insert、delete、update)同步出去。

为了保证update操作被同步,同样也需要在ogg登录服务二的数据库,并执行一些命令

GGSCI (localhost.localdomain) 4> dblogin userid ogg,password ogg
Successfully logged into database.

GGSCI (localhost.localdomain as ogg@aegisdb) 5> ADD TRANDATA aegis.t2
2016-03-05 21:38:23  WARNING OGG-06439  No unique key is defined for table T2. 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 AEGIS.T2.
TRANDATA for scheduling columns has been added on table 'AEGIS.T2'.
TRANDATA for instantiation CSN has been added on table 'AEGIS.T2'.

启动同步进程

GGSCI (localhost.localdomain) 6> start ext1

Sending START request to MANAGER ...

EXTRACT EXT1 starting

GGSCI (localhost.localdomain) 7> info all

Program     Status      Group       Lag at Chkpt  Time Since Chkpt

MANAGER     RUNNING                                          
EXTRACT     RUNNING     EXT1        00:00:00      00:04:50   
REPLICAT    RUNNING     REP1        00:00:00      00:00:07

4.6    在服务器一上创建一个replicat进程

GGSCI (localhost.localdomain) 1> edit params ./GLOBALS
GGSCHEMA ogg
CHECKPOINTTABLE ogg.checkpoint

GGSCI (localhost.localdomain) 2> dblogin userid ogg,password ogg
Successfully logged into database.

GGSCI (localhost.localdomain as ogg@aegisdb) 3> add checkpointtable ogg.checkpoint
Successfully created checkpoint table ogg.checkpoint.

GGSCI (localhost.localdomain as ogg@aegisdb) 4> add replicat rep1, exttrail /u01/app/ogg/dirdat/lt, checkpointtable ogg.checkpoint
REPLICAT added.

GGSCI (localhost.localdomain as ogg@aegisdb) 5> edit params rep1

replicat rep1
ASSUMETARGETDEFS
userid ogg, password ogg
discardfile /u01/app/ogg/dirdat/rep1_discard.txt, append, megabytes 10
DDL include all
map aegis.t2, target aegis.t2;

启动replicate进程

GGSCI (localhost.localdomain as ogg@aegisdb) 6> start rep1

Sending START request to MANAGER ...
REPLICAT REP1 starting

GGSCI (localhost.localdomain as ogg@aegisdb) 7> info all

Program     Status      Group       Lag at Chkpt  Time Since Chkpt

MANAGER     RUNNING                                          
EXTRACT     RUNNING     EXT1        00:00:00      00:00:03   
REPLICAT    RUNNING     REP1        00:00:00      00:00:01

4.7    双向同步是否成功

服务器一上

SQL> select * from t2;

COL1             COL2
---------- ----------
PEK                 1
FOC                 2

SQL> update t2 set col2=10 where col2=1;  

1 row updated.

SQL> commit;

Commit complete.

服务器二上可以看到

SQL> select * from t2;

COL1             COL2
---------- ----------
PEK                10
FOC                 2

服务器二上

SQL> update t2 set col2=20 where col2=2;

1 row updated.

SQL> commit;

Commit complete.

SQL> select * from t2;

COL1             COL2
---------- ----------
PEK                10
FOC                20

服务器一上可以看到

SQL> select * from t2;

COL1             COL2
---------- ----------
PEK                10
FOC                20

这样在基础数据相同,且服务器一的数据库只处理COL1=’PEK’的数据,服务器二的数据库只处理COL1<>’PEK’的数据的情况下,实现了数据双主双活。

五、 卸载

$ cd /ogg/app/

$ sqlplus / as sysdba

SQL*Plus: Release 11.2.0.4.0 Production on Mon Jul 11 10:31:48 2016
Copyright (c) 1982, 2013, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> @ddl_disable.sql

Trigger altered.

SQL> @ddl_remove.sql

DDL replication removal script.
WARNING: this script removes all DDL replication objects and data.

You will be prompted for the name of a schema for the Oracle GoldenGate database objects.

NOTE: The schema must be created prior to running this script.
Enter Oracle GoldenGate schema name:ogg
Working, please wait ...
Spooling to file ddl_remove_spool.txt

Script complete.

SQL> @marker_remove.sql

Marker removal script.
WARNING: this script removes all marker objects and data.

You will be prompted for the name of a schema for the Oracle GoldenGate database objects.

NOTE: The schema must be created prior to running this script.
Enter Oracle GoldenGate schema name:ogg

PL/SQL procedure successfully completed.

Sequence dropped.

Table dropped. 

Script complete.

SQL> exit

Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

$ ./deinstall/deinstall.sh
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!