同步需求:
增加生产库表 t_order_group到目的镜像库的ogg同步。
源用户jyc1到目标库jyc2,表名不变
源端和目标端:
数据库版本均oracle 11.2.0.4
ogg版本:
oracle goldengate command interpreter for oracle
version 12.2.0.1.170919 27731916_fbo
linux, x64, 64bit (optimized), oracle 11g on sep 4 2018 20:23:18
处理思路:(同步多表的思路一致)
1.源端检查表是否满足同步要求(主键)
2.目标端是否存在该表(不存在)
3.配置步骤:
- 1) 源端附加表日志
- 2) 源端抽取进程增加表,并重新启动抽取进程
- 3) 源端推送进程增加表,重新启动推送进程
- 4) 目标端停止复制进程
- 5) 源端获取scn(确认该表无事务存在)
- 6) 源端exp/expdp根据scn导出表dmp
- 7) 源端dmp拷贝到目标端
- 8) 目标端导入表
- 9) 目标端复制进程增加表带过滤filter csn
- 10)目标端启动复制进程,待同步完成后,去掉fiter,重新启动复制进程
- 11)源端和目标端检查记录是否一致
详细步骤:
1.源端检查源jyc1用户下t_order_group表信息,是否有主键或唯一索引(无主键的表要求加上主键,彻底避免性能问题和数据同步不一致的问题)
select a.constraint_name, a.column_name from user_cons_columns a, user_constraints b where a.constraint_name = b.constraint_name and b.constraint_type = 'p' and a.table_name = 't_order_group';
constraint_name column_name
------------------------
pk_t_order_group suborderid
sql> select count(*) from t_order_group;
count(*)
----------
1594
sql> set time on
15:27:09 sql> /
count(*)
----------
1594
15:27:11 sql> /
count(*)
----------
1595
15:30:38 sql> /
count(*)
----------
1597
确认结果:有主键并且dml操作数据变化频繁。由此,我们需要采用scn标准的步骤来配置ogg同步。
2.目标端是否存在该表(不存在)
在目标端conn jyc2/xxx检查表desc t_order_group提示不存在,后续无需考虑drop表操作
3.配置步骤:
- 1) 源端附加表日志
dblogin userid oggadm,password xxxxxx
add trandata jyc1.t_order_group
- 2) 源端抽取进程增加表,并重新启动抽取进程
edit param cq
table jyc1.t_order_group;
start cq
- 3) 源端推送进程增加表,重新启动推送进程
edit param ts
table jyc1.t_order_group;
start cq
- 4) 目标端停止复制进程
stop fz
- 5) 源端获取scn(确认该表无事务存在)
col current_scn for 9999999999999999
select d.spid,a.sid,e.event,b.status,c.sql_text,b.username,b.osuser,b.machine,b.program
from v$sess_io a,v$session b,v$sql c,v$process d,v$session_wait e
where a.sid=b.sid and b.sql_hash_value=c.hash_value and b.sid=e.sid and upper(c.sql_text) like '%t_order_group%'
and b.paddr=d.addr
and b.sid in(
select a.sid
from v$transaction b, v$session a
where b.addr=a.taddr
);
上述sql的关联查询根据网友【秋枫暮霞】建议,改成如下更合适:
select s.sid,t.start_time,osuser o, username u,sa.sql_text
from v$session s, v$transaction t, dba_rollback_segs r, v$sqlarea sa
where s.taddr=t.addr and t.xidusn=r.segment_id( )
and s.sql_address=sa.address( ) and upper(sa.sql_text) like '%t_order_group%';
select current_scn from v$database;
no rows selected --查询无事务,则该scn可用,否则重新执行上述语句查询,直到无记录为止。
current_scn
-----------------
17461233119488
sql>
- 6) 源端exp/expdp根据scn导出表dmp
注意使用flashback_scn=17461233119488和排除触发器trigger(目标端无需触发器)
[oracle@racj1 ~]$ expdp jyc1/xxx directory=dmp tables=jyc1.t_order_group dumpfile=t_order_group.dmp parallel=1 logfile=t_order_group.log flashback_scn=17461233119488 cluster=n exclude=trigger
export: release 11.2.0.4.0 - production on thu dec 2 16:19:24 2021
米乐app官网下载 copyright (c) 1982, 2011, oracle and/or its affiliates. all rights reserved.
connected to: oracle database 11g enterprise edition release 11.2.0.4.0 - 64bit production
with the partitioning, real application clusters, automatic storage management, olap,
data mining and real application testing options
flashback automatically enabled to preserve database integrity.
starting "jyc1"."sys_export_table_01": jyc1/******** directory=dmp tables=jyc1.t_order_group dumpfile=t_order_group.dmp parallel=1 logfile=t_order_group.log flashback_scn=17461233119488 cluster=n exclude=trigger
estimate in progress using blocks method...
processing object type table_export/table/table_data
total estimation using blocks method: 2 mb
processing object type table_export/table/procact_instance
processing object type table_export/table/table
processing object type table_export/table/comment
processing object type table_export/table/index/index
processing object type table_export/table/constraint/constraint
processing object type table_export/table/index/statistics/index_statistics
processing object type table_export/table/statistics/table_statistics
. . exported "jyc1"."t_order_group" 1.705 mb 1607 rows
master table "jyc1"."sys_export_table_01" successfully loaded/unloaded
******************************************************************************
dump file set for jyc1.sys_export_table_01 is:
/bak/dmp/t_order_group.dmp
job "jyc1"."sys_export_table_01" successfully completed at thu dec 2 16:21:24 2021 elapsed 0 00:01:49
- 7) 源端dmp拷贝到目标端
[oracle@racj1 ~]$ scp /bak/dmp/t_order_group.dmp 192.168.52.245:/oradata/dmp/
the authenticity of host '192.168.52.245 (192.168.52.245)' can't be established.
ecdsa key fingerprint is sha256:i68drbpih6rfi/vnppclklgy4wxumjjjbhqnycno83g.
are you sure you want to continue connecting (yes/no/[fingerprint])? yes
warning: permanently added '192.168.52.245' (ecdsa) to the list of known hosts.
oracle@192.168.52.245's password:
t_order_group.dmp 100% 2564kb 62.7mb/s 00:00
- 8) 目标端导入表
注意检查dmp路径及导入到不同用户参数remap_schema=jyc1:jyc2 tables=jyc1.t_order_group
sql> set line 132
sql> set wrap off
sql> select * from dba_directories;
owner directory_name directory_path
------------------------------ ------------------------------ ----------------------------------------------
sys dmp /oradata/dmp
[oracle@zyjxnew dmp]$ impdp jyc2/xxx directory=dmp remap_schema=jyc1:jyc2 tables=jyc1.t_order_group dumpfile=t_order_group.dmp parallel=1 logfile=t_order_group.log cluster=n
import: release 11.2.0.4.0 - production on thu dec 2 16:21:36 2021
米乐app官网下载 copyright (c) 1982, 2011, oracle and/or its affiliates. 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
master table "jyc2"."sys_import_table_01" successfully loaded/unloaded
starting "jyc2"."sys_import_table_01": jyc2/******** directory=dmp remap_schema=jyc1:jyc2 tables=jyc1.t_order_group dumpfile=t_order_group.dmp parallel=1 logfile=t_order_group.log cluster=n
processing object type table_export/table/procact_instance
processing object type table_export/table/table
processing object type table_export/table/table_data
. . imported "jyc2"."t_order_group" 1.705 mb 1607 rows
processing object type table_export/table/comment
processing object type table_export/table/index/index
processing object type table_export/table/constraint/constraint
processing object type table_export/table/index/statistics/index_statistics
processing object type table_export/table/statistics/table_statistics
job "jyc2"."sys_import_table_01" successfully completed at thu dec 2 16:21:47 2021 elapsed 0 00:00:11
- 9) 目标端复制进程增加表带过滤filter csn
edit param fz
map jyc1.t_order_group target jyc2.t_order_group, filter ( @getenv(‘transaction’, ‘csn’) >17461233119488);
提示:如果ogg版本是11.x的,需要使用双引号"transaction",“csn”,如果ogg版本>=12.x的使用单引号,如上。
- 10)目标端启动复制进程,待同步完成后,去掉fiter,重新启动复制进程
start fz
info fz
info all
edit param fz
map jyc1.t_order_group target jyc2.t_order_group;
stop fz
start fz
info all
info fz
- 11)源端和目标端检查记录是否一致
源端和目标端同时查询即可。还可以检查./dirrpt/fz.dsc日志是否有异常,根据问题处理即可。
sql> set time on
16:24:52 sql> select count(*) from t_order_group;
count(*)
----------
1609
16:24:54 sql> /
count(*)
----------
1609
16:25:03 sql> /
count(*)
----------
1611
16:26:40 sql> /
count(*)
----------
1611
生产遇到的问题:
1.源端附加日志时卡住,无法取消
2021-12-02 15:55:45 warning ogg-00706 failed to add supplemental log group on table jyc1.t_order_group due to ora-00054: resource busy and acquire with nowait specified or timeout expired sql alter table "jyc1"."t_order_group" add supplemental log group "ggs_2948562" ("suborderid") always /* goldengate_ddl_replication */.
^c
^c
^c^c^z
^c
处理方法:断开该ssh连接,并检查处理锁问题
sql> select *
from (select a.inst_id, a.sid, a.serial#,
a.sql_id,
a.event,
a.status,
connect_by_isleaf as isleaf,
sys_connect_by_path(a.sid||'@'||a.inst_id, ' <- ') tree,
level as tree_level
from gv$session a
start with a.blocking_session is not null
connect by (a.sid||'@'||a.inst_id) = prior (a.blocking_session||'@'||a.blocking_instance))
where isleaf = 1
order by tree_level asc
rows will be truncated
inst_id sid serial# sql_id event status isleaf tree
---------- ---------- ---------- ------------- ---------------------------------------------------------------- -------- ---------- ------------------------------------------------
1 7183 54211 sql*net message from client inactive 1 <- 7832@1 <- 7183@1
1 7183 54211 sql*net message from client inactive 1 <- 2532@1 <- 7183@1
sql>
sql>
sql>
sql> alter system kill session '7183,54211,@1' immediate;
system altered.
2.拓展思考:
如果源端表是新表,无记录,那么就只需要1)2)3) 目标端创建新表 同步测试验证即可。
如果源端表有记录,但使用不频繁,记录数少(不超过十万条),比如几个小时才有数据变化,那么只需要1)2)3) 如下参考方法
- create target table,dblink truncate, insert into
- exp/expdp/imp/impdp
- create target table,ogg initial load
如果源端的整个数据同步ogg是新搭建,那么处理步骤就又不同了,更简单些。
总之ogg属于逻辑同步复制,只要把握好数据变化的时间点即可灵活应对。