﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-八方朋友 -文章分类-大小数据库</title><link>http://www.cnitblog.com/ashleyshuo/category/6829.html</link><description>技术共享</description><language>zh-cn</language><lastBuildDate>Sat, 01 Oct 2011 07:45:49 GMT</lastBuildDate><pubDate>Sat, 01 Oct 2011 07:45:49 GMT</pubDate><ttl>60</ttl><item><title>oracle数据库同步技术 （转载）</title><link>http://www.cnitblog.com/ashleyshuo/articles/40039.html</link><dc:creator>张朔</dc:creator><author>张朔</author><pubDate>Fri, 22 Feb 2008 06:41:00 GMT</pubDate><guid>http://www.cnitblog.com/ashleyshuo/articles/40039.html</guid><wfw:comment>http://www.cnitblog.com/ashleyshuo/comments/40039.html</wfw:comment><comments>http://www.cnitblog.com/ashleyshuo/articles/40039.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ashleyshuo/comments/commentRss/40039.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ashleyshuo/services/trackbacks/40039.html</trackback:ping><description><![CDATA[「 oracle数据库同步技术 」&nbsp;<br>&nbsp;<br>高级复制<br>什么是复制？简单地说复制就是在由两个或者多个数据库系统构成的一个分布式数据库环境中拷贝数据的过程。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 高级复制，是在组成分布式数据库系统的多个数据库中复制和维护数据库对象的过程。 Oracle 高级复制允许应用程序更新数据库的任何副本，并将这些更改自动传递到其他数据库，同时确保全局事务处理的一致性和数据完整性。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 同步复制，复制数据在任何时间在任何复制节点均保持一致。如果复制环境中的任何一个节点的复制数据发生了更新操作，这种变化会立刻反映到其他所有的复制节点。这种技术适用于那些对于实时性要求较高的商业应用中。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 异步复制，所有复制节点的数据在一定时间内是不同步的。如果复制环境中的其中的一个节点的复制数据发生了更新操作，这种改变将在不同的事务中被传播和应用到其他所有复制节点。这些不同的事务间可以间隔几秒，几分种，几小时，也可以是几天之后。复制节点之间的数据临时是不同步的，但传播最终将保证所有复制节点间的数据一致。 <br>一、实现数据库复制的前提条件&nbsp; <br>1、数据库支持高级复制功能&nbsp; <br>您可以用system身份登录数据库，查看v$option视图，如果其中Advanced replication为TRUE，则支持高级复制功能；否则不支持。&nbsp; <br>2、数据库初始化参数要求&nbsp; <br>①、db_domain = test.com.cn&nbsp; <br>指明数据库的域名(默认的是WORLD)，这里可以用您公司的域名。&nbsp; <br>②、global_names = true&nbsp; <br>它要求数据库链接(database link)和被连接的数据库名称一致。&nbsp; <br>现在全局数据库名：db_name+&#8221;.&#8221;+db_domain&nbsp; <br>③、有跟数据库job执行有关的参数&nbsp; <br>job_queue_processes = 1&nbsp; <br>job_queue_interval = 60&nbsp; <br>distributed_transactions = 10&nbsp; <br>open_links = 4&nbsp; <br>第一行定义SNP进程的启动个数为n。系统缺省值为0，正常定义范围为0～36，根据任务的多少，可以配置不同的数值。&nbsp; <br>第二行定义系统每隔N秒唤醒该进程一次。系统缺省值为60秒，正常范围为1～3600秒。事实上，该进程执行完当前任务后，就进入睡眠状态，睡眠一段时间后，由系统的总控负责将其唤醒。&nbsp; <br>如果修改了以上这几个参数，需要重新启动数据库以使参数生效。&nbsp; <br>二、实现数据库同步复制的步骤&nbsp; <br>假设在Internet上我们有两个数据库：一个叫深圳(shenzhen)，一个叫北京(beijing)。&nbsp; <br>具体配置见下表：&nbsp; <br>数据库名 shenzhen beijing&nbsp; <br>数据库域名 test.com.cn test.com.cn&nbsp; <br>数据库sid号 shenzhen beijing&nbsp; <br>Listener端口号 1521 1521&nbsp; <br>服务器ip地址 10.1.1.100 10.1.1.200&nbsp; <br>&nbsp; <br>1、确认两台数据库之间可以互相访问，在tnsnames.ora里设置数据库连接字符串。&nbsp; <br>①、例如：深圳这边的数据库连接字符串是以下的格式&nbsp; <br>beijing =&nbsp; <br>(DESCRIPTION =&nbsp; <br>(ADDRESS_LIST =&nbsp; <br>(ADDRESS = (PROTOCOL = TCP)(HOST = 10.1.1.200)(PORT = 1521))&nbsp; <br>)&nbsp; <br>(CONNECT_DATA =&nbsp; <br>(SERVICE_NAME = beijing)&nbsp; <br>)&nbsp; <br>)&nbsp; <br>运行$tnsping beijing&nbsp; <br>出现以下提示符：&nbsp; <br>Attempting to contact (ADDRESS=(PROTOCOL=TCP)(HOST=10.1.1.200)(PORT=1521))&nbsp; <br>OK（n毫秒）&nbsp; <br>表明深圳数据库可以访问北京数据库。&nbsp; <br>②、在北京那边也同样配置，确认$tnsping shenzhen 是通的。&nbsp; <br>2、改数据库全局名称，建公共的数据库链接。&nbsp; <br>①、用system身份登录shenzhen数据库&nbsp; <br>SQL&gt;alter database rename global_name to shenzhen.test.com.cn;&nbsp; <br>用system身份登录beijing数据库：&nbsp; <br>SQL&gt;alter database rename global_name to beijing.test.com.cn;&nbsp; <br>②、用system身份登录shenzhen数据库&nbsp; <br>SQL&gt;create public database link beijing.test.com.cn using 'beijing';&nbsp; <br>测试数据库全局名称和公共的数据库链接&nbsp; <br>SQL&gt;select * from <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#103;&#108;&#111;&#98;&#97;&#108;&#95;&#110;&#97;&#109;&#101;&#64;&#98;&#101;&#105;&#106;&#105;&#110;&#103;&#46;&#116;&#101;&#115;&#116;&#46;&#99;&#111;&#109;&#46;&#99;&#110;">global_name@beijing.test.com.cn</a>;&nbsp; <br>返回结果为beijing.test.com.cn就对了。&nbsp; <br>用system身份登录beijing数据库：&nbsp; <br>SQL&gt;create public database link shenzhen.test.com.cn using 'shenzhen';&nbsp; <br>测试数据库全局名称和公共的数据库链接&nbsp; <br>SQL&gt;select * from <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#103;&#108;&#111;&#98;&#97;&#108;&#95;&#110;&#97;&#109;&#101;&#64;&#115;&#104;&#101;&#110;&#122;&#104;&#101;&#110;&#46;&#116;&#101;&#115;&#116;&#46;&#99;&#111;&#109;&#46;&#99;&#110;">global_name@shenzhen.test.com.cn</a>;&nbsp; <br>返回结果为shenzhen.test.com.cn就对了。&nbsp; <br>3、建立管理数据库复制的用户repadmin，并赋权。&nbsp; <br>①、用system身份登录shenzhen数据库&nbsp; <br>SQL&gt;create user repadmin identified by repadmin default tablespace users temporary tablespace temp;&nbsp; <br>SQL&gt;execute dbms_defer_sys.register_propagator('repadmin');&nbsp; <br>SQL&gt;grant execute any procedure to repadmin;&nbsp; <br>SQL&gt;execute dbms_repcat_admin.grant_admin_any_repgroup('repadmin');&nbsp; <br>SQL&gt;grant comment any table to repadmin;&nbsp; <br>SQL&gt;grant lock any table to repadmin;&nbsp; <br>②、同样用system身份登录beijing数据库，运行以上的命令，管理数据库复制的用户repadmin，并赋权。&nbsp; <br>说明：repadmin用户名和密码可以根据用户的需求自由命名。&nbsp; <br>4、在数据库复制的用户repadmin下创建私有的数据库链接。&nbsp; <br>①、用repadmin身份登录shenzhen数据库&nbsp; <br>SQL&gt;create database link beijing.test.com.cn connect to repadmin identified by repadmin;&nbsp; <br>测试这个私有的数据库链接：&nbsp; <br>SQL&gt;select * from <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#103;&#108;&#111;&#98;&#97;&#108;&#95;&#110;&#97;&#109;&#101;&#64;&#98;&#101;&#105;&#106;&#105;&#110;&#103;&#46;&#116;&#101;&#115;&#116;&#46;&#99;&#111;&#109;&#46;&#99;&#110;">global_name@beijing.test.com.cn</a>;&nbsp; <br>返回结果为beijing.test.com.cn就对了。&nbsp; <br>②、用repadmin身份登录beijing数据库&nbsp; <br>SQL&gt;create database link shenzhen.test.com.cn connect to repadmin identified by repadmin;&nbsp; <br>测试这个私有的数据库链接&nbsp; <br>SQL&gt;select * from <a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#103;&#108;&#111;&#98;&#97;&#108;&#95;&#110;&#97;&#109;&#101;&#64;&#115;&#104;&#101;&#110;&#122;&#104;&#101;&#110;&#46;&#116;&#101;&#115;&#116;&#46;&#99;&#111;&#109;&#46;&#99;&#110;">global_name@shenzhen.test.com.cn</a>;&nbsp; <br>返回结果为shenzhen.test.com.cn就对了。&nbsp; <br>5、创建或选择实现数据库复制的用户和对象，给用户赋权，数据库对象必须有主关键字。&nbsp; <br>假设我们用ORACLE里举例用的scott用户，dept表。&nbsp; <br>①、用internal身份登录shenzhen数据库，创建scott用户并赋权&nbsp; <br>SQL&gt;create user scott identified by tiger default tablespace users temporary tablespace temp;&nbsp; <br>SQL&gt;grant connect, resource to scott;&nbsp; <br>SQL&gt;grant execute on sys.dbms_defer to scott;&nbsp; <br>②、用scott身份登录shenzhen数据库，创建表dept&nbsp; <br>SQL&gt;create table dept&nbsp; <br>(deptno number(2) primary key,&nbsp; <br>dname varchar2(14),&nbsp; <br>loc varchar2(13) );&nbsp; <br>③、如果数据库对象没有主关键字，可以运行以下SQL命令添加：&nbsp; <br>SQL&gt;alter table dept add (constraint dept_deptno_pk primary key (deptno));&nbsp; <br>④、在shenzhen数据库scott用户下创建主关键字的序列号，范围避免和beijing的冲突。&nbsp; <br>SQL&gt; create sequence dept_no increment by 1 start with 1 maxvalue 44 cycle nocache;&nbsp; <br>(说明：maxvalue 44可以根据应用程序及表结构主关键字定义的位数需要而定)&nbsp; <br>⑤、在shenzhen数据库scott用户下插入初始化数据&nbsp; <br>SQL&gt;insert into dept values (dept_no.nextval,'accounting','new york');&nbsp; <br>SQL&gt;insert into dept values (dept_no.nextval,'research','dallas');&nbsp; <br>SQL&gt;commit;&nbsp; <br>⑥、在beijing数据库那边同样运行以上①，②，③&nbsp; <br>⑦、在beijing数据库scott用户下创建主关键字的序列号，范围避免和shenzhen的冲突。&nbsp; <br>SQL&gt; create sequence dept_no increment by 1 start with 45 maxvalue 99 cycle nocache;&nbsp; <br>⑧、在beijing数据库scott用户下插入初始化数据&nbsp; <br>SQL&gt;insert into dept values (dept_no.nextval,'sales','chicago');&nbsp; <br>SQL&gt;insert into dept values (dept_no.nextval,'operations','boston');&nbsp; <br>SQL&gt;commit;&nbsp; <br>6、创建要复制的组scott_mg，加入数据库对象，产生对象的复制支持&nbsp; <br>①、用repadmin身份登录shenzhen数据库，创建主复制组scott_mg&nbsp; <br>SQL&gt; execute dbms_repcat.create_master_repgroup('scott_mg');&nbsp; <br>说明：scott_mg组名可以根据用户的需求自由命名。&nbsp; <br>②、在复制组scott_mg里加入数据库对象&nbsp; <br>SQL&gt;execute dbms_repcat.create_master_repobject(sname=&gt;'scott',oname=&gt;'dept', type=&gt;'table',use_existing_object=&gt;true,gname=&gt;'scott_mg');&nbsp; <br>参数说明：&nbsp; <br>sname 实现数据库复制的用户名称&nbsp; <br>oname 实现数据库复制的数据库对象名称&nbsp; <br>(表名长度在27个字节内，程序包名长度在24个字节内)&nbsp; <br>type 实现数据库复制的数据库对象类别&nbsp; <br>(支持的类别：表，索引，同义词，触发器，视图，过程，函数，程序包，程序包体)&nbsp; <br>use_existing_object true表示用主复制节点已经存在的数据库对象&nbsp; <br>gname 主复制组名&nbsp; <br>③、对数据库对象产生复制支持&nbsp; <br>SQL&gt;execute dbms_repcat.generate_replication_support('scott','dept','table');&nbsp; <br>(说明：产生支持scott用户下dept表复制的数据库触发器和程序包)&nbsp; <br>④、确认复制的组和对象已经加入数据库的数据字典&nbsp; <br>SQL&gt;select gname, master, status from dba_repgroup;&nbsp; <br>SQL&gt;select * from dba_repobject;&nbsp; <br>7、创建主复制节点&nbsp; <br>①、用repadmin身份登录shenzhen数据库，创建主复制节点&nbsp; <br>SQL&gt;execute dbms_repcat.add_master_database&nbsp; <br>(gname=&gt;'scott_mg',master=&gt;'beijing.test.com.cn',use_existing_objects=&gt;true, copy_rows=&gt;false, propagation_mode =&gt; 'asynchronous');&nbsp; <br>参数说明：&nbsp; <br>gname 主复制组名&nbsp; <br>master 加入主复制节点的另一个数据库&nbsp; <br>use_existing_object true表示用主复制节点已经存在的数据库对象&nbsp; <br>copy_rows false表示第一次开始复制时不用和主复制节点保持一致&nbsp; <br>propagation_mode 异步地执行&nbsp; <br>②、确认复制的任务队列已经加入数据库的数据字典&nbsp; <br>SQL&gt;select * from user_jobs;&nbsp; <br>8、使同步组的状态由停顿(quiesced )改为正常(normal)&nbsp; <br>①、用repadmin身份登录shenzhen数据库，运行以下命令&nbsp; <br>SQL&gt; execute dbms_repcat.resume_master_activity('scott_mg',false);&nbsp; <br>②、确认同步组的状态为正常(normal)&nbsp; <br>SQL&gt; select gname, master, status from dba_repgroup;&nbsp; <br>③、如果这个①命令不能使同步组的状态为正常(normal)，可能有一些停顿的复制，运行以下命令再试试(建议在紧急的时候才用)：&nbsp; <br>SQL&gt; execute dbms_repcat.resume_master_activity('scott_mg',true);&nbsp; <br>9、创建复制数据库的时间表，我们假设用固定的时间表：10分钟复制一次。&nbsp; <br>①、用repadmin身份登录shenzhen数据库，运行以下命令&nbsp; <br>SQL&gt;begin&nbsp; <br>dbms_defer_sys.schedule_push (&nbsp; <br>destination =&gt; 'beijing.test.com.cn',&nbsp; <br>interval =&gt; 'sysdate + 10/1440',&nbsp; <br>next_date =&gt; sysdate);&nbsp; <br>end;&nbsp; <br>/&nbsp; <br>　&nbsp; <br>SQL&gt;begin&nbsp; <br>dbms_defer_sys.schedule_purge (&nbsp; <br>next_date =&gt; sysdate,&nbsp; <br>interval =&gt; 'sysdate + 10/1440',&nbsp; <br>delay_seconds =&gt; 0,&nbsp; <br>rollback_segment =&gt; '');&nbsp; <br>end;&nbsp; <br>/&nbsp; <br>　&nbsp; <br>②、用repadmin身份登录beijing数据库，运行以下命令&nbsp; <br>SQL&gt;begin&nbsp; <br>dbms_defer_sys.schedule_push (&nbsp; <br>destination =&gt; ' shenzhen.test.com.cn ',&nbsp; <br>interval =&gt; 'sysdate + 10 / 1440',&nbsp; <br>next_date =&gt; sysdate);&nbsp; <br>end;&nbsp; <br>/&nbsp; <br>　&nbsp; <br>SQL&gt;begin&nbsp; <br>dbms_defer_sys.schedule_purge (&nbsp; <br>next_date =&gt; sysdate,&nbsp; <br>interval =&gt; 'sysdate + 10/1440',&nbsp; <br>delay_seconds =&gt; 0,&nbsp; <br>rollback_segment =&gt; '');&nbsp; <br>end;&nbsp; <br>/&nbsp; <br>10、添加或修改两边数据库的记录，跟踪复制过程&nbsp; <br>如果你想立刻看到添加或修改后数据库的记录的变化，可以在两边repadmin用户下找到push的job_number，然后运行：&nbsp; <br>SQL&gt;exec dbms_job.run(job_number);&nbsp; <br>三、异常情况的处理&nbsp; <br>1、检查复制工作正常否，可以在repadmin 用户下查询user_jobs&nbsp; <br>SQL&gt;select job,this_date,next_date,what, broken from user_jobs;&nbsp; <br>正常的状态有两种：&nbsp; <br>任务闲——this_date为空，next_date为当前时间后的一个时间值&nbsp; <br>任务忙——this_date不为空，next_date为当前时间后的一个时间值&nbsp; <br>异常状态也有两种：&nbsp; <br>任务死锁——next_date为当前时间前的一个时间值&nbsp; <br>任务死锁——next_date为非常大的一个时间值，例如：4001-01-01&nbsp; <br>这可能因为网络中断照成的死锁&nbsp; <br>解除死锁的办法：&nbsp; <br>$ps &#8211;ef|grep orale&nbsp; <br>找到死锁的刷新快照的进程号ora_snp*，用kill &#8211;9 命令删除此进程&nbsp; <br>然后进入repadmin 用户SQL&gt;操作符下，运行命令：&nbsp; <br>SQL&gt;exec dbms_job.run(job_number);&nbsp; <br>说明：job_number 为用select job,this_date,next_date,what from user_jobs;命令查出的job编号。&nbsp; <br>2、增加或减少复制组的复制对象&nbsp; <br>①、停止主数据库节点的复制动作，使同步组的状态由正常(normal)改为停顿(quiesced )&nbsp; <br>用repadmin身份登录shenzhen数据库，运行以下命令&nbsp; <br>SQL&gt;execute dbms_repcat.suspend_master_activity (gname =&gt; 'scott_mg');&nbsp; <br>②、在复制组scott_mg里加入数据库对象，保证数据库对象必须有主关键字。&nbsp; <br>SQL&gt;execute dbms_repcat.create_master_repobject(sname=&gt;'scott',oname=&gt;'emp', type=&gt;'table',use_existing_object=&gt;true,gname=&gt;'scott_mg');&nbsp; <br>对加入的数据库对象产生复制支持&nbsp; <br>SQL&gt;execute dbms_repcat.generate_replication_support('scott','emp','table');&nbsp; <br>③、在复制组scott_mg里删除数据库对象。&nbsp; <br>SQL&gt;execute dbms_repcat.drop_master_repobject ('scott','dept','table');&nbsp; <br>④、重新使同步组的状态由停顿(quiesced )改为正常(normal)。&nbsp; <br>SQL&gt; execute dbms_repcat.resume_master_activity('scott_mg',false); <br>
<img src ="http://www.cnitblog.com/ashleyshuo/aggbug/40039.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ashleyshuo/" target="_blank">张朔</a> 2008-02-22 14:41 <a href="http://www.cnitblog.com/ashleyshuo/articles/40039.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>oracle数据库（转载）</title><link>http://www.cnitblog.com/ashleyshuo/articles/40037.html</link><dc:creator>张朔</dc:creator><author>张朔</author><pubDate>Fri, 22 Feb 2008 06:38:00 GMT</pubDate><guid>http://www.cnitblog.com/ashleyshuo/articles/40037.html</guid><wfw:comment>http://www.cnitblog.com/ashleyshuo/comments/40037.html</wfw:comment><comments>http://www.cnitblog.com/ashleyshuo/articles/40037.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/ashleyshuo/comments/commentRss/40037.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ashleyshuo/services/trackbacks/40037.html</trackback:ping><description><![CDATA[<div class=cnt>
<div>在两台oracle数据库之间测试成功，下面就总结一下吧：<br><strong>需求：</strong><br>现有两台oracle数据库服务器A和B(A,B可以是在同一内网，也可以是在互联网上的两台独立机器)。A和B里有都有testable表，结构一样，现需要当A库中的testable表变化时，B库里的testable也相应变化数据
<p><strong>我的解决方案：</strong><br>在A中建立到B库的链接，然后对要同步的表做一个同义synonym，最后建一个触发器，就可以完成了。当然，你所用的当前的用户要有相应的权限去执行这些操作。</p>
<p>当从A向B同步数据时，应该在A上做所有的设置:<br>1,为保证连接到另一台远程服务器的数据库，你需要建立一个DB Link，但是，这里要注意语法格式，using +"connect string"，这个connect string应该是存在于oracle服务器的TNSNAMES.ORA文件里，监听程序将从这里获取远方服务器<br>的ip地址等信息,我定义了一个'251'的connect string如下:<br>
<table cellSpacing=0 cellPadding=0 width="100%">
    <tbody>
        <tr>
            <td bgColor=#e1f1ff>251 =<br>(DESCRIPTION =<br>(ADDRESS_LIST =<br>(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.251)(PORT = 1521))<br>)<br>(CONNECT_DATA =<br>(SERVICE_NAME = mychoice)<br>)<br>)<br></td>
        </tr>
    </tbody>
</table>
把它存到你的TNSNAMES.ORA文件里。</p>
<p>2,然后就可以定义DB Link了：<br>
<table cellSpacing=0 cellPadding=0 width="100%">
    <tbody>
        <tr>
            <td bgColor=#e1f1ff>create public database link TEST2.US.ORACLE.COM<br>connect to 用户名<br>identified by "密码"<br>using '251';<br></td>
        </tr>
    </tbody>
</table>
</p>
<p>3,建立synonym(同义)</p>
<table cellSpacing=0 cellPadding=0 width="100%">
    <tbody>
        <tr>
            <td bgColor=#e1f1ff>
            <p>create or replace synonym TEST01<br>for MYCHOICE.TESTABLE@TEST2.US.ORACLE.COM;</p>
            </td>
        </tr>
    </tbody>
</table>
<p>建立完了以后，你可以通过:<br>select * from test01 <br>上面的语句相当于在B服务器上执行：<br>select * from testable　</p>
<p>４，封建触发器：<br>当A中的testable表变化时（这里只考虑插入操作），就会触发向远程的B库的testable也插入相应的数据：<br>
<table cellSpacing=0 cellPadding=0 width="100%">
    <tbody>
        <tr>
            <td bgColor=#e1f1ff>create or replace trigger rtest<br>after insert on testable<br>for each row<br>begin
            <p>&#160;</p>
            <p>insert into test01 (something) values (:new.something);<br>end;</p>
            </td>
        </tr>
    </tbody>
</table>
</p>
<p>ok,现在我们可以测试一下，你在Ａ库中往testable表中插入一条记录，看看Ｂ库中是不是也相应的增加了</p>
</div>
</div>
<img src ="http://www.cnitblog.com/ashleyshuo/aggbug/40037.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ashleyshuo/" target="_blank">张朔</a> 2008-02-22 14:38 <a href="http://www.cnitblog.com/ashleyshuo/articles/40037.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>oracle数据同步（转载）</title><link>http://www.cnitblog.com/ashleyshuo/articles/40036.html</link><dc:creator>张朔</dc:creator><author>张朔</author><pubDate>Fri, 22 Feb 2008 06:36:00 GMT</pubDate><guid>http://www.cnitblog.com/ashleyshuo/articles/40036.html</guid><wfw:comment>http://www.cnitblog.com/ashleyshuo/comments/40036.html</wfw:comment><comments>http://www.cnitblog.com/ashleyshuo/articles/40036.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/ashleyshuo/comments/commentRss/40036.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/ashleyshuo/services/trackbacks/40036.html</trackback:ping><description><![CDATA[<p>oracle数据同步&nbsp; <br>&nbsp;搞了几天数据同步的程序，写完之后觉得很浪费，因为oracle自身的数据库联机备份功能已经相当强大。不过关键一点是我们应用场景跟联机备份有很大差别。</p>
<p>虽然这次没用上oracle自身备份功能，但是有一个通过建立db link 来实现同步倒也很简单，查询了网上资料之后，自己也实现了这一功能。</p>
<p>具体步骤如下：</p>
<p>场景：</p>
<p>数据库版本：oracle9i，默认情况下两个数据库服务器都是刚建好的</p>
<p>局域网或广域网上两台能互联的数据库服务器，一个是main，一个是ass，现在每往main中某一表test增删改一条记录时，ass中test表通过main库中触发器来增删改，哦，对了两个test结构一样</p>
<p>我现在实现的是局域网内的数据同步:</p>
<p>预热：</p>
<p>先建表</p>
<p>*****************************************************************</p>
<p>-- Create table<br>create table TEST<br>(<br>&nbsp; USERNAME VARCHAR2(20),<br>&nbsp; PASSWORD VARCHAR2(10)<br>)<br>tablespace SYNTEST<br>&nbsp; pctfree 10<br>&nbsp; initrans 1<br>&nbsp; maxtrans 255<br>&nbsp; storage<br>&nbsp; (<br>&nbsp;&nbsp;&nbsp; initial 64K<br>&nbsp;&nbsp;&nbsp; minextents 1<br>&nbsp;&nbsp;&nbsp; maxextents unlimited<br>&nbsp; );</p>
<p>*****************************************************************</p>
<p>1：先构建一下ass数据库实例的连接符，先备份一个%oracle_home%--ora92--network--admin--&gt;tnsnames.ora，然后用UltraEdit打开，添加如下</p>
<p>*****************************************************************</p>
<p>ass100 =<br>&nbsp; (DESCRIPTION =<br>&nbsp;&nbsp;&nbsp; (ADDRESS_LIST =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.0.100)(PORT = 1521))<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp;&nbsp;&nbsp; (CONNECT_DATA =<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (SERVICE_NAME = syntest)<br>&nbsp;&nbsp;&nbsp; )<br>&nbsp; )</p>
<p>*****************************ok************************************</p>
<p>注意:ass100是用于网络连接用的名称，2中要用到</p>
<p>2：建立db_link</p>
<p>*****************************************************************</p>
<p>create public database link syntest100<br>connect to syntest<br>identified by syntest<br>using 'ass100'</p>
<p>*****************************************************************</p>
<p>注意：syntest100是db link 名称，可以随便起名，</p>
<p>ass100就是1中的建立的连接符名称</p>
<p>3：如果2执行通过的话，就可以建立同义synonym</p>
<p>*****************************************************************</p>
<p>create or replace synonym syn100 for</p>
<p><a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#121;&#110;&#116;&#101;&#115;&#116;&#46;&#116;&#101;&#115;&#116;&#64;&#115;&#121;&#110;&#116;&#101;&#115;&#116;&#49;&#48;&#48;">syntest.test@syntest100</a></p>
<p>*****************************************************************</p>
<p>注意：syntest100就是2中的db link 名称，syntest.test是ass中的实例名.表名</p>
<p>小提示：执行完3后，可以在本地执行select * from syn100,这时得出来的结果是ass数据库中表test的数据。</p>
<p>4：如果以上都正确的话，现在可以为本地表test建触发器，我写了3个触发器，增加，删除，修改</p>
<p>*****************************************************************</p>
<p>--增加</p>
<p>create or replace trigger synins<br>after insert on test<br>for each row<br>begin<br>&nbsp;insert into syn100 (USERNAME,Password) values (:NEW.USERNAME,:NEW.password);<br>end;</p>
<p>--执行完，可以测试一下</p>
<p>/*</p>
<p>添加本机一条记录</p>
<p>insert into test (username,password) values ('murphy','1234')</p>
<p>查看ass表test</p>
<p>select * from syn100</p>
<p>可以吧，我这里是ok的。</p>
<p>*/</p>
<p>&nbsp;</p>
<p>--修改</p>
<p>create or replace trigger synupd<br>&nbsp; after update on test <br>&nbsp; for each row<br>declare<br>begin<br>&nbsp; update syn100 set password = :new.password where username = :old.username;<br>end synupd;</p>
<p>--执行完，可以测试一下</p>
<p>/*</p>
<p>修改本机一条记录</p>
<p>update test set password = '123' where username = 'murphy'</p>
<p>查看ass表test</p>
<p>select * from syn100</p>
<p>*/</p>
<p>&nbsp;</p>
<p>--删除</p>
<p>create or replace trigger syndel</p>
<p>&nbsp; after delete on test <br>&nbsp; for each row<br>declare<br>&nbsp; -- local variables here<br>begin<br>&nbsp; delete from syn100 where username=:old.username;<br>end syndel;</p>
<p>--执行完，可以测试一下</p>
<p>/*</p>
<p>删除一条本地记录</p>
<p>delete from test where username = 'murphy'</p>
<p>查看ass表test</p>
<p>select * from syn100</p>
<p>*/</p>
<p>****************************全文完*************************************</p>
<p>感觉很方便。<br>&nbsp;<br></p>
<img src ="http://www.cnitblog.com/ashleyshuo/aggbug/40036.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/ashleyshuo/" target="_blank">张朔</a> 2008-02-22 14:36 <a href="http://www.cnitblog.com/ashleyshuo/articles/40036.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>