Wednesday, March 2, 2011

Oracle10.2.0.4 replicate to mysql5.1

I want to realize the oracle replicate to mysql,oracle as a master DB which provide write and mysql as a slave which provide read,how to realize it?

First, we install the oracle database gateway(11g,DG4ODBC) on oracle DB server,we can download from oracle company and get the install document from oracle metalink;

Second,we install unixodbc and mysqlodbc on the oracle DB server.
Thirdly,we configure the ODBC and DG4ODBC.

thirdly, we create trrigers on all tables and create a table to save the replication data.

the trigger code example as following:

CREATE OR REPLACE TRIGGER tr_repl_test_repl_mysql
   AFTER INSERT OR UPDATE OR DELETE
   ON test_repl_mysql
   FOR EACH ROW
   DECLARE
   v_errm              VARCHAR2 (512);
   v_dml_type          VARCHAR2 (16);
   v_bind_where_str    VARCHAR2(2000);
   v_unique_where_str  VARCHAR2(2000);
   v_unique_where_str2 VARCHAR2(2000);
   v_table_name        VARCHAR2 (30);
  
   PROCEDURE sp_insert_data (
      p_table_name         VARCHAR2,
      p_dml_type           VARCHAR2,
      p_unique_where_str   VARCHAR2,
      p_bind_where_str     VARCHAR2
      )AS
      v_id               VARCHAR2 (18);
      v_seq              NUMBER;
   BEGIN
      SELECT seq_repl_channel.NEXTVAL
       INTO v_seq
      FROM DUAL;
      v_id := TO_CHAR (SYSDATE, 'yyyymmdd') || LPAD (v_seq, 10, 0);
      EXECUTE IMMEDIATE 'INSERT INTO repl_channel(ID,table_name, dml_type,unique_where_str, bind_where_str,status,modify_time,create_time)
               VALUES(:1,:2,:3,:4,:5,:6,:7,:8)'
       USING v_id,p_table_name,
       p_dml_type,p_unique_where_str,
       p_bind_where_str,'I',
       SYSTIMESTAMP,SYSTIMESTAMP;
   END sp_insert_data;
BEGIN
   v_bind_where_str:=' id=? ';
   v_table_name := 'test_repl_mysql';
   IF INSERTING
   THEN
      v_dml_type := 'INSERTING';
      v_unique_where_str := '  id='||:new.id||'';
      sp_insert_data (v_table_name,
      v_dml_type,v_unique_where_str,v_bind_where_str);
   ELSIF UPDATING
      THEN
      v_dml_type := 'UPDATING';
      v_unique_where_str := '  id='||:old.id||'';
      v_unique_where_str2 := '  id='||:new.id||'';
      IF ( :old.siteid <>:new.siteid )
      THEN
         sp_insert_data (v_table_name,
         'DELETING',v_unique_where_str,v_bind_where_str);
         sp_insert_data (v_table_name,
         'INSERTING',v_unique_where_str2,v_bind_where_str);
      ELSE
         sp_insert_data (v_table_name,
         v_dml_type,v_unique_where_str,v_bind_where_str);
      END IF;
   ELSIF DELETING
      THEN
      v_dml_type := 'DELETING';
      v_unique_where_str := '  id='||:old.id||'';
      sp_insert_data (v_table_name,
      v_dml_type,v_unique_where_str,v_bind_where_str);
   END IF;
EXCEPTION WHEN OTHERS THEN
      v_errm := SUBSTR (SQLERRM, 1, 512);
      INSERT INTO repl_log
       (ID,err_msg, create_time)
       VALUES (seq_wbx_repl_log.NEXTVAL,v_errm, SYSTIMESTAMP);
 END tr_repl_test_repl_mysql;
 /

Finally, we use a java damon to execute the the middle table replication data by status.


Build oracle repliacte mysql environment and give the testing report as following:











The design architecture as following:

No comments:

Post a Comment