Mysql遷移達夢實踐

0、前言

前幾天因爲要搭建一個達夢的開發環境,數據從mysql中遷移,所以做了一次mysql到達夢的遷移工作(數據量不大,所以也沒碰到太多問題)。看了一下網上好像也沒有比較詳細的mysql遷移達夢的文章,於是我把這次遷移總結一下供大家參考。對於oracle遷移達夢可以參考我之前的兩篇文章:
oracle遷移達夢7手順及注意事項
oracle遷移達夢常見問題彙總

1、概述

mysql遷移達夢主要需要完成以下工作:

  1. 分析待移植系統,確定移植對象。
  2. 通過數據遷移工具 DTS 完成常規數據庫對象及數據的遷移。
  3. 通過人工完成 視圖、函數、過程等對象的遷移。
  4. 移植完成後對移植的結果進行校驗,確保移植的完整性和正確性。
  5. 對應用系統進行移植、測試和優化。

2、遷移準備

2.1、版本選擇
首先需要選擇合適的數據庫版本,優先選擇完整安裝版本(無完整安裝版本的平臺例外),避免數據庫客戶端和服務 器端存在版本不匹配帶來的額外工作量,達夢在不同平臺的不同版本上,安裝包都會有差異, 一定要採用嚴格匹配的原則,除非得到達夢原廠技術人員的允許,儘量減少干擾性的問題出現。

2.2、數據庫參數選擇
初始化庫,關鍵的點在於對初始化參數的設置,從mysql遷移到oracle推薦參數設置如下:
1、頁大小page_size,在達夢中,頁大小默認是8k,如果數據庫中表存在較多大字段,建議設置成16k或者32k,否則導入數據時會報錯。
2、mysql是大小寫不敏感的,大小寫敏感參數CASE_SENSITIVE設置成0。
3、簇大小 EXTENT_SIZE。數據文件使用的簇大小,即每次分配新的段空間時連續的頁數,只能是16頁或32頁,缺省使用16頁,從 mysql移植到 DM 使用默認值就可。
4、在dm.ini中將COMPATIBLE_MODE參數設置成4,表示兼容mysql。
5、在dm.ini中將CALC_AS_DECIMAL修改爲1,表示整數相除保留小數。

2.3、其它準備事項
1、遷移前在達夢中創建對應的用戶,默認會將數據遷移到SYSDBA用戶下。
2、遷移前在達夢中創建對應的表空間,默認遷移到MAIN表空間下。
3、對於dts機器的內存推薦至少要大於4g,否則會出現"java heap space"之類內存不足的報錯。
4、機器的CPU要多核,開12個以上的並行速度會快很多。
5、儘量考慮上SSD,機械盤的速度比較慢

3、遷移過程

3.1、制定遷移計劃
1、遷移建議按照以下順序進行:先遷移序列,再遷移表,最後遷移視圖、函數等對象。
2、對於表的遷移,建議先遷移表結構(不包含索引、約束),再遷移表數據,最後遷移索引和約束。
3、數據量大的表單獨進行遷移
4、對於大字段較多的表,需要修改批量的行數,以免造成遷移工具內存溢出。

3.2、建立遷移工程
使用達夢的dts遷移工具創建遷移工程,在遷移創建mysql到達夢的遷移工程即可,但是需要注意dts工具連接mysql要自己手動指定驅動包,如下圖:
在這裏插入圖片描述
相關的驅動包在toot/dropins/com.dameng/plugins/com.dameng.jdbc.drivers目錄下面,指定完驅動後配置好驅動類名(如上圖)和URL即可。

不過這裏我配置完後出現了這個報錯:
在這裏插入圖片描述
當時反覆檢查後也沒查出是什麼原因,後來換了個版本的dts工具就搞定了。。。

3.3、遷移表
遷移表我們按照表結構=》表數據=》表約束這個順序來。
首先是表結構:
在這裏插入圖片描述
在這裏插入圖片描述
如上圖所示,這裏要注意***在遷移工具的選項中要選擇“保持對象名大小寫”!***

接着遷移表數據:
在這裏插入圖片描述
最後是約束:
在這裏插入圖片描述
至此,表就已經遷移完成了!

這裏說一下遷移表時碰到的問題:
–問題1:

錯誤號:   -2670

錯誤消息: 第10 行附近出現錯誤:
對象[is_show]DEFAULT約束表達式無效

---------------------------------
CREATE TABLE "FFCS"."c_table_tbl"
(
 "id" INT IDENTITY(1,1) NOT NULL,
 "title" VARCHAR(300) NULL,
 "param_out" VARCHAR(765) NULL,
 "param_in" VARCHAR(765) NULL,
 "data_source" VARCHAR(300) NULL,
 "is_show" VARCHAR(6) DEFAULT Y
 NOT NULL
)

解決方案:

CREATE TABLE "FFCS"."c_table_tbl"
(
 "id" INT IDENTITY(1,1) NOT NULL,
 "title" VARCHAR(300) NULL,
 "param_out" VARCHAR(765) NULL,
 "param_in" VARCHAR(765) NULL,
 "data_source" VARCHAR(300) NULL,
 "is_show" VARCHAR(6) DEFAULT 'Y'
 NOT NULL
)

–問題2:分區表問題
本次遷移因爲表較少沒碰到該問題,但這是在遷移表時十分典型的一個問題。因爲達夢中要求分區表的分區列必須是主鍵。對於這個類型的錯誤需要使用複合主鍵,將分區鍵添加到主鍵中。

–問題3:‘0000-00-00 00:00:00’ 插入失敗
因爲mysql 中有’0000-00-00’ ‘0000-00-00 00:00:00’ 這樣無效的日期類型數據,遷移時會報錯,需要先在mysql中修改掉。

3.4、其它對象遷移
對於視圖、函數、包這類對象,建議用腳本到達夢管理工具中直接執行。這裏舉幾個本次遷移碰到的問題例子。

–問題1:視圖創建報錯

CREATE VIEW "FFCS"."aas_user_resource_view" ("resource_type","resource_key","operation_code","aas_user_id","aas_usergroup_id","is_direct_relate")  
AS 
select "r1"."resource_type"    AS "resource_type",
       "r1"."resource_key"     AS "resource_key",
       "r1"."operation_code"   AS "operation_code",
       "r1"."aas_user_id"      AS "aas_user_id",
       "r1"."aas_usergroup_id" AS "aas_usergroup_id",
       1                       AS "is_direct_relate"
  from "FFCS"."aas_resource_tbl" "r1"
 where "r1"."aas_user_id" is not null
union
select "r2"."resource_type"    AS "resource_type",
       "r2"."resource_key"     AS "resource_key",
       "r2"."operation_code"   AS "operation_code",
       "u"."aas_user_id"       AS "aas_user_id",
       "r2"."aas_usergroup_id" AS "aas_usergroup_id",
       0                       AS "is_direct_relate"
  from "FFCS"."aas_resource_tbl" "r2"
  join "FFCS"."aas_user_tbl" "u"
 where "u"."org_id" = "r2"."aas_usergroup_id" or
       "u"."aas_user_id" in
       (select "uug"."aas_user_id"
           from "FFCS"."aas_user_usergroup_tbl" "uug"
          where "uug"."aas_usergroup_id" = "r2"."aas_usergroup_id")

解決方案:
這裏是因爲mysql中支持select * from t1 join t2這種寫法,而在達夢中要改成select * from t1,t2或着select * from t1 join t2 on(xxx)

CREATE VIEW "FFCS"."aas_user_resource_view" ("resource_type","resource_key","operation_code","aas_user_id","aas_usergroup_id","is_direct_relate")  
AS 
select "r1"."resource_type"    AS "resource_type",
       "r1"."resource_key"     AS "resource_key",
       "r1"."operation_code"   AS "operation_code",
       "r1"."aas_user_id"      AS "aas_user_id",
       "r1"."aas_usergroup_id" AS "aas_usergroup_id",
       1                       AS "is_direct_relate"
  from "FFCS"."aas_resource_tbl" "r1"
 where "r1"."aas_user_id" is not null
union
select "r2"."resource_type"    AS "resource_type",
       "r2"."resource_key"     AS "resource_key",
       "r2"."operation_code"   AS "operation_code",
       "u"."aas_user_id"       AS "aas_user_id",
       "r2"."aas_usergroup_id" AS "aas_usergroup_id",
       0                       AS "is_direct_relate"
  from "FFCS"."aas_resource_tbl" "r2",
   "FFCS"."aas_user_tbl" "u"
 where "u"."org_id" = "r2"."aas_usergroup_id" or
       "u"."aas_user_id" in
       (select "uug"."aas_user_id"
           from "FFCS"."aas_user_usergroup_tbl" "uug"

–問題2:函數創建報錯

CREATE DEFINER=`ffcs`@`192.168.37.%` FUNCTION `judge_usergroup_is_center`(v_usergroup_id int) RETURNS int(11)
begin
  declare v_result int DEFAULT(-1);
  declare n_parent int;
  declare n_no_parent int;
  declare n_childs int;
  select count(1)
    from aas_usergroup_tbl gg
   where gg.aas_usergroup_id =
         (select g.parent_id
            from aas_usergroup_tbl g
           where g.aas_usergroup_id = v_usergroup_id) and gg.parent_id is null
     into n_parent;
     select count(1)
    from aas_usergroup_tbl gg
   where gg.aas_usergroup_id in (
    select parent_id
    from aas_usergroup_tbl gg
   where gg.aas_usergroup_id =
         (select g.parent_id
            from aas_usergroup_tbl g
           where g.aas_usergroup_id = v_usergroup_id)) into n_no_parent;
  select count(1)
    from aas_usergroup_tbl g
   where g.parent_id = v_usergroup_id  into n_childs;
   
  IF n_parent>0 THEN SET
        v_result =0;
   ELSEIF (n_no_parent >0 and n_childs>0) THEN SET
     v_result =1; 
   ELSEIF (n_no_parent >0 and n_childs=0) THEN SET
     v_result =2;
  END IF; 
  return v_result;
end

解決方法:
函數報錯是因爲語法區別導致的,改成如下:

CREATE or replace FUNCTION "FFCS"."judge_usergroup_is_center"(v_usergroup_id int) RETURN int is
   v_result int DEFAULT(-1);
   n_parent int;
   n_no_parent int;
   n_childs int;
 begin
  select count(1)
    into n_parent
    from aas_usergroup_tbl gg
   where gg.aas_usergroup_id =
         (select g.parent_id
            from aas_usergroup_tbl g
           where g.aas_usergroup_id = v_usergroup_id) and gg.parent_id is null
     ;
     select count(1)
     into n_no_parent
    from aas_usergroup_tbl gg
   where gg.aas_usergroup_id in (
    select parent_id
    from aas_usergroup_tbl gg
   where gg.aas_usergroup_id =
         (select g.parent_id
            from aas_usergroup_tbl g
           where g.aas_usergroup_id = v_usergroup_id));
  select count(1)
  into n_childs
    from aas_usergroup_tbl g
   where g.parent_id = v_usergroup_id ;
  IF n_parent>0 THEN SET
        v_result =0;
   ELSEIF (n_no_parent >0 and n_childs>0) THEN SET
     v_result =1; 
   ELSEIF (n_no_parent >0 and n_childs=0) THEN SET
     v_result =2;
  END IF; 
  return v_result;
end;

3.5、收尾工作
至此,遷移基本都已經完成了,最後我們需要對全庫收集一次統計信息:

DBMS_STATS.GATHER_SCHEMA_STATS( 'FFCS', --FFCS 爲模式名
100, FALSE,
       'FOR ALL COLUMNS SIZE AUTO');
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章